读写NFC [英] Reading and writing to NFC

查看:455
本文介绍了读写NFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序以读取和写入标签.当我读取所有内容时,一切正常,当我尝试将数据保存在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?

我有一个运行4个片段的活动,活动中包含阅读代码,并从片段中调用书写机制

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

推荐答案

对于写入,我不会使用enableForegroundDispatch方法,它会误导用户将卡移出范围太快,从而导致许多写入错误,并且实际用户使用时会导致数据损坏.

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.

在要控制声音提示的地方,我会用enableReaderMode代替.

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

更新 请注意,使用enabledReaderMode时,您仍然可以在清单中放置Intent过滤器,以要求Android OS在看到某种类型的标签时启动您的应用,从清单Intent过滤器生成的Intent始终在Activity的onCreate方法中进行处理无论您在活动"中如何处理卡片.

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.

此外,enableForegroundDispatch也不可靠读取,因为操作系统在看起来像没有窗口的活动中处理NFC读取,因此您的活动在读取时会暂停.

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.

我已经用enableForegroundDispatch进行了真实的用户测试,当您的应用程序位于前台时,该标签进入范围,您的应用程序暂停,该标签超出范围,然后回到您的应用范围之前的范围机会被恢复,因此操作系统认为没有什么在等待它的意图,因此打开它自己的窗口,显示NDEF数据的基本屏幕,而不是试图将意图传递给仍然暂停的活动.

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.

另一个建议是,如果您可以轻松地使用NDEF格式,则因为操作系统知道NDEF会尝试使用它来做事,因此建议不要使用NDEF格式,使用您自己的格式意味着操作系统不太可能干扰您正在做的事情.但是,如果您要写的数据多于一页数据,则会带来更多的复杂性,但是您确实可以获得更好的错误处理能力.

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.

使用enableReaderMode时,读卡时您的应用不会暂停(相反,卡交互是在应用中的单独线程中处理的),您可以控制声音,因此在拥有完成写作,而不是完成任务的一半.

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.

答案 https://stackoverflow.com/a/59397667/2373819 中的低水平阅读enableReaderMode示例撰写页面的结构与阅读并无太大不同.

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

更新: 重新读取代码后,似乎在编写时就假定该标签在范围内.由于该标签可以轻松,频繁地进入或离开范围,因此,一旦触发onNewIntent,就可以更加可靠地进行写入.

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.

您应该将onNewIntent视为一个通知,告知您标签在范围内,您可以在标签之间进行读写(在enableReaderMode方法中,它被称为onTagDiscovered,因为这确实是它的意思.) )

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.

这篇关于读写NFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆