semlette / nfc_in_flutter

Cross-platform flutter plugin for reading and writing NFC tags. Not maintained anymore - not looking for new maintainer, fork instead.
MIT License
120 stars 119 forks source link

How do I print Records #34

Open gautamswostik opened 4 years ago

semlette commented 4 years ago

When you get a NDEFMessage from readNDEF(), you can access the records with the .records property. This is a list of NDEFRecords.

An NDEFRecord has the following properties: property description
id The record's ID
languageCode The language code of the language the payload is written in. This will only be set if the record type is "T" and the .tnf value is set to well_known.
type The type of the record. This could be a MIME type or a "well known" type of "U" or "T".
payload The raw payload as a string
data The intended payload. Some well known types contain metadata in their payloads. For example, the payload of a "T" well known type will have bytes for the character set and the language code. These will be stripped and put in their respective properties. This is probably the property you'll be using to get the payload from the record.
tnf An enumerable list of record types. The value of this property may decide how the payload is formatted.

You can print the records by looping over them like this:

NDEFMessage message = /* you will have gotten this from .readNDEF() */
for (NDEFRecord record in message.records) {
  print("Record payload: ${record.data}");
}

The example app does this.

gautamswostik commented 4 years ago

Hi I figured it out how to print records...but whenever I scan the default scanner from my phone pops ....I already took permission but still cannot stop it... can you please help.

On Fri, Mar 6, 2020, 5:41 PM Andi Semler notifications@github.com wrote:

When you get a NDEFMessage from readNDEF(), you can access the records with the .records property. This is a list of NDEFRecords.

An NDEFRecord has the following properties: property description id The record's ID languageCode The language code of the language the payload is written in. This will only be set if the record type is "T" and the .tnf value is set to well_known. type The type of the record. This could be a MIME type or a "well known" type of "U" or "T". payload The raw payload as a string data The intended payload. Some well known types contain metadata in their payloads. For example, the payload of a "T" well known type will have bytes for the character set and the language code. These will be stripped and put in their respective properties. This is probably the property you'll be using to get the payload from the record. tnf An enumerable list of record types. The value of this property may decide how the payload is formatted.

You can print the records by looping over them like this:

NDEFMessage message = / you will have gotten this from .readNDEF() /for (NDEFRecord record in message.records) { print("Record payload: ${record.data}"); }

The example app does this. https://github.com/semlette/nfc_in_flutter/blob/9470ed31f5522b42d699926792e9977c53fa21f4/example/lib/read_example_screen.dart#L17

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/semlette/nfc_in_flutter/issues/34?email_source=notifications&email_token=AMQMKLZZK53XEFL52WDOND3RGDQFPA5CNFSM4KWBZOP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOBDM5Y#issuecomment-595736183, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMQMKLZXG6AYEX543AVJLJ3RGDQFPANCNFSM4KWBZOPQ .

semlette commented 4 years ago

Could you tell me which phone and operating system version you're using?

gautamswostik commented 4 years ago

Android- Samsung galaxy s8

On Fri, Mar 6, 2020, 7:03 PM Andi Semler notifications@github.com wrote:

Could you tell me which phone and operating system version you're using?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/semlette/nfc_in_flutter/issues/34?email_source=notifications&email_token=AMQMKL2QFPWU5CNLXIFRQCLRGDZ2PA5CNFSM4KWBZOP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOBKDLY#issuecomment-595763631, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMQMKL7ICA37Z26DT7SP2Q3RGDZ2PANCNFSM4KWBZOPQ .

semlette commented 4 years ago

My guess would be that the stream returned from .readNDEF() is closed before you read your tag. nfc_in_flutter is only scanning for tags when a .readNDEF() stream is open.

In order to help more, I'd have to see your code.

gautamswostik commented 4 years ago

https://github.com/swostikg11/NFC-

On Fri, Mar 6, 2020 at 7:10 PM Andi Semler notifications@github.com wrote:

My guess would be that the stream returned from .readNDEF() is closed before you read your tag. nfc_in_flutter is only scanning for tags when a .readNDEF() stream is open.

In order to help more, I'd have to see your code.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/semlette/nfc_in_flutter/issues/34?email_source=notifications&email_token=AMQMKL46RLHBOXCK7T53QR3RGD2T5A5CNFSM4KWBZOP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOBKX2I#issuecomment-595766249, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMQMKL6VFD43NZFD2BZPWN3RGD2T5ANCNFSM4KWBZOPQ .

semlette commented 4 years ago

I have ran your app on my phone (Google Pixel 3a, Android 10) and it works fine for me.

When I press "scan" and scan my tag, it prints to the terminal as expected. If I scan another tag on the same screen it opens the default scanner.

Android continuously scans for NFC tags and whenever it scans a tag, it forwards it to whatever app it listening for tags. If there isn't any apps listening for tags, it opens the default scanner. In your app, when you have scanned a tag, you stop listening for more. This is decided by the once: true argument on line 57 and _stopScanning() call on line 62.

The problem might be that your phone scans the tag twice, giving the tag to your app and then opening the default scanner right after.