Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web NFC support #75

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Web NFC support #75

wants to merge 10 commits into from

Conversation

v1lling
Copy link

@v1lling v1lling commented Dec 24, 2021

implements #72

@v1lling
Copy link
Author

v1lling commented Dec 24, 2021

feel free to test and give some feedback :)

@BhKent
Copy link

BhKent commented May 22, 2023

Any idea if this will be merged? I need to use NFC in a PWA app.

@BhKent BhKent mentioned this pull request May 22, 2023
@v1lling
Copy link
Author

v1lling commented May 22, 2023

not sure you can fork my branch and try if it works for your use-case though.

Any idea if this will be merged? I need to use NFC in a PWA app.

not sure
you can fork my branch and try if it works for your use-case though.

@BhKent
Copy link

BhKent commented May 22, 2023

Thanks. Did as you suggested. Next problem I'm facing is that I only need the uid indentifier of the tag. Any help in getting this from the cached message would be appreciated.
I used to use:
uid = await (NfcA.from(tag)?.identifier ?? NfcB.from(tag)?.identifier ?? NfcF.from(tag)?.identifier ?? NfcV.from(tag)?.identifier ?? Uint8List(0)).toHexString();
But this is now returning null

Thanks

@v1lling
Copy link
Author

v1lling commented May 23, 2023

Are you testing on web or native?

Have a look at this file, where I used the WebNFC plugin:
Landing_viewmodel.dart

Specifically look at function activateNFCScan().
You need to use FlutterNfcWeb.instance.startNFCRead() with its callback function onTagDiscovered().
you will get a List records, where you should be able to read the record[id].

Also make sure to use my fork on branch web-nfc, instead of master.

@BhKent
Copy link

BhKent commented May 24, 2023

I am testing on WEB

Did as you suggested but I see that the id field within the records is blank. I assume the return records are only from the user memory pages of the tag.

image

I'm trying to get at the UID (serial number) which is stored in memory page 0h to first byte of 02h. User memory (where the Ndef records are kept is from 04h onwards)

image

On the android version of the package the callBack to the .startSession() function returned an NfcTag object which contained the UID (aka serial)

If we are successfully reading the Ndef records it feels like we are close to getting the tag UID

@v1lling v1lling closed this May 24, 2023
@v1lling v1lling reopened this May 24, 2023
@v1lling
Copy link
Author

v1lling commented May 24, 2023

Ah, I see.
I think for this to work you have to create your own fork and edit my branch. Currently it seems like the serial number is not passed from JS-side to Dart side.

Have a look at the example of web-nfc:
https://googlechrome.github.io/samples/web-nfc/
You can see that JS ndef is able to get the serialnumber in event "reading":

ndef.addEventListener("reading", ({ message, serialNumber }) => {
      log(`> Serial Number: ${serialNumber}`);
      log(`> Records: (${message.records.length})`);
    });

So you would have to change the js file of my fork:
https://github.com/v1lling/flutter-nfc-manager/blob/web-nfc/assets/flutter_nfc.js

In function startNDEFReaderJS(), the plugin starts JS Web NFC and reads the NDEF records and translates them into Dart Object.
Specifically in line 265, I define the onreading event mentioned above. In line 267 I get the NDEF records via "event.message.records;". Looking at the Web nfc example above, this event should also have an attribute "event. serialNumber". Theoretically you should be able to retrieve it there.
If you retrieve it succesfully, you have to add it to nativeNDEFMessage in line 275 by changing function getNativefromWebNDEFMessage() and adding the serialNumber into this object. Afterwards you should be able to retrieve it on Dart side.

After changing the flutter_nfc.js File, make sure to minify it. The plugin uses flutter_nfc_min.js instead of flutter_nfc.js for performance reasons. In the comment header at the top of the file flutter_nfc.js you can see how to do it.

Sorry, due to a setup change I'm currently not able to implement and test it myself.
Have a try and let me know if it works.

@BhKent
Copy link

BhKent commented May 25, 2023

This works perfectly. Thank you so much for the help.
I have butchered the code so badly I don't want to create a pull request.
But I just implemented your above suggestion and it worked.

@v1lling
Copy link
Author

v1lling commented May 25, 2023

This works perfectly. Thank you so much for the help. I have butchered the code so badly I don't want to create a pull request. But I just implemented your above suggestion and it worked.

Nice! I'm glad I could help.

@kevtechi
Copy link

Any possibility to get this merged? Any help needed with testing or fixes to the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants