简体   繁体   中英

Reading and writing to NFC

I am writing an Android application to read and write to a tag. When I read everything's works fine, when i try to save data on the nfc tag, tag first read data and opens again activity, how to block reading from intent and save correctly data?

I have an activity that runs 4 fragments, the reading code is in the activity and the mechanism of writing is called from fragment

For writing I would not use the enableForegroundDispatch method, it misleads the user to move the card out of range too quickly, leading to lots of write errors and data corruption when used by real users.

This is because the sound prompt happens straight after the OS has read the card before your code has a chance to write to it.

I would use enableReaderMode instead where you get to control the sound prompt.

Update Note with enabledReaderMode you can still put Intent filters in your manifest to ask the Android OS to start your App when it sees a certain type of Tag, Intents generated from Manifest Intent filters are always handled in the onCreate method of your Activity no matter how you are handling cards in your Activity.

Also enableForegroundDispatch is unreliable for reading as well, because the OS handles the NFC reading in what looks like a windowless Activity, your Activity will get Paused while it does the read.

I have had with real user testing with enableForegroundDispatch the tag coming in to range when your App is in the foreground, your App being paused, the tag going out of range and coming back in to range before your App has had a chance to be resumed and thus the OS thinks that nothing is waiting for an Intent from it and thus opens it's own window display a basic screen for NDEF data, instead of trying to pass the Intent to your still paused Activity.

The other suggestion is not to use NDEF format if you can do so easily because the OS understands NDEF it will try and do things with it, using your own format means the OS is less likely to interfere with what you are doing. BUT there is more complication if you want to write more than a page of data, but you do get better error handling capabilities.

With enableReaderMode your App does not get paused when the card is read (instead the card interaction is handled in a separate thread in your app), you get control of the sound, so you can play a sound when you have finished writing instead of when the task is half done.

Example of low level reading enableReaderMode in answer https://stackoverflow.com/a/59397667/2373819 writing pages is not much different in structure to reading.

Update: Re-reading your code, it seems that when you write you are assuming the tag is in range. Because the tag can go in and out or range easily and frequently it is much more reliable to write as soon as onNewIntent is fired.

You should treat onNewIntent as a notification that a tag is in range for you to read or write from/to the tag (in the enableReaderMode method it is called onTagDiscovered because that is really what it is.)

Update: Added a flow chart on how I handle read/write logic for reliable writing. 读/写逻辑

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM