简体   繁体   English

读取和写入 NFC

[英]Reading and writing to NFC

I am writing an Android application to read and write to a tag.我正在编写一个 Android 应用程序来读取和写入标签。 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?当我阅读一切正常时,当我尝试将数据保存在 nfc 标签上时,标签首先读取数据并再次打开活动,如何阻止从意图读取并正确保存数据?

I have an activity that runs 4 fragments, the reading code is in the activity and the mechanism of writing is called from fragment我有一个运行 4 个片段的活动,读取代码在活动中,并且从片段中调用写入机制

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.对于写入,我不会使用enableForegroundDispatch方法,它会误导用户过快地将卡移出范围,导致真实用户使用时出现大量写入错误和数据损坏。

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.我会使用enableReaderMode而不是您可以控制声音提示的地方。

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.使用enabledReaderMode更新注意,您仍然可以将 Intent 过滤器放在清单中,以要求 Android 操作系统在看到某种类型的标签时启动您的应用程序,无论如何,从 Manifest Intent 过滤器生成的 Intent 始终在您的 Activity 的onCreate方法中处理您正在“活动”中处理卡片。

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读取enableForegroundDispatch可靠,因为操作系统在看起来像无窗口的 Activity 中处理 NFC 读取,您的 Activity 将在读取时暂停。

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.我已经使用enableForegroundDispatch进行了真实用户测试,当您的应用程序处于前台时,您的应用程序被暂停,标签超出范围并在您的应用程序有机会恢复之前回到范围内时,标签进入范围因此操作系统认为没有任何东西在等待来自它的意图,因此打开它自己的窗口显示 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.另一个建议是,如果您可以轻松使用 NDEF 格式,则不要使用 NDEF 格式,因为操作系统理解 NDEF,它会尝试使用它来做事,使用您自己的格式意味着操作系统不太可能干扰您正在做的事情。 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.使用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.回答https://stackoverflow.com/a/59397667/2373819 中的低级阅读enableReaderMode示例编写页面在结构上与阅读没有太大区别。

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被触发,就可以更可靠地写入。

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.)您应该将onNewIntent视为标记在您可以从/向标记读取或写入的范围内的通知(在enableReaderMode方法中,它被称为onTagDiscovered因为它确实是这样。)

Update: Added a flow chart on how I handle read/write logic for reliable writing.更新:添加了有关如何处理读/写逻辑以实现可靠写入的流程图。 读/写逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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