简体   繁体   English

Android响应PN532 InListPassiveTarget

[英]Android responds to PN532 InListPassiveTarget

I have a weird situation where I'm trying to toggle my PN532 between tag emulation and tag reader mode. 我有一个奇怪的情况,我试图在标签模拟和标签阅读器模式之间切换我的PN532。 The reader goes for ~70ms and the tag emulation runs for ~600ms. 读者持续约70ms,标签仿真运行约600ms。

What I want to happen is that when I put my Android device near the antenna, that it reads the emulated tag. 我想要发生的是,当我将我的Android设备放在天线附近时,它会读取模拟标签。 When I hold an NFC tag to the PN532, it's supposed to read the tag. 当我将PN标签保存到PN532时,它应该读取标签。

The solution I have is somewhat working but I'm seeing a weird situation where when I call readPassiveTargetID to test for the presence of passive tags, Android is responding. 我的解决方案有点工作,但我看到一个奇怪的情况,当我调用readPassiveTargetID来测试是否存在被动标签时,Android正在响应。 This would be fine but I think that Android is spinning up a service which blocks it from reading the emulated tag. 这样会很好,但我认为Android正在启动一项服务,阻止它阅读模拟标签。

When testing this out, sometimes the devices don't communicate at all with the PN532 but when they do it's most often with the reader mode. 在测试时,有时设备根本不与PN532通信,但是当他们这样做时,最常用的是读卡器模式。 Here is the output I get when this happens: 以下是发生这种情况时得到的输出:

Nexus 5x (Android 6): Nexus 5x(Android 6):

<< 4A 1 0
>> 4B 1 1 0 4 60 4 1 2 3 4 5 78 80 71 0

Galaxy S4 (Android 5.0.1): Galaxy S4(Android 5.0.1):

<< 4A 1 0
>> 4B 1 1 0 4 20 4 8 87 F6 62 5 78 B3 70 2

The 5x will often read the emulated tag after a second or so but the S4 rarely reads the tag when I am toggling between the two modes. 5x通常会在一秒左右后读取模拟标签,但是当我在两种模式之间切换时,S4很少读取标签。 When I just run tag emulation both devices work flawlessly. 当我只运行标签模拟时,两个设备都可以正常运行。

I'm thinking that there might be a few problems with what I'm doing. 我在想我可能会遇到一些问题。

  1. Do I need to do something special to toggle between the modes? 我是否需要做一些特殊的事情来切换模式? I don't actually reset or reinitialize the PN532 when I toggle. 切换时,我实际上没有重置或重新初始化PN532。
  2. Is there a command I can send to tell the devices to stop listening or a setting I can send that will fix this? 是否有一个命令我可以发送告诉设备停止收听或我可以发送的设置将解决这个问题?

The solution I have is somewhat working but I'm seeing a weird situation where when I call readPassiveTargetID (InListPassiveTarget) Android is responding. 我的解决方案有点工作,但我看到一个奇怪的情况,当我调用readPassiveTargetID (InListPassiveTarget)时,Android正在响应。

That's not a weird situation but something that is to be expected when you try to access an NFC device from any ISO/IEC 14443A reader. 这不是一种奇怪的情况,但是当您尝试从任何ISO / IEC 14443A阅读器访问NFC设备时,这是预期的。 An NFC device typically supports NFC设备通常支持

  • reader/writer mode (ie it polls for passive tags), 读/写模式(即轮询无源标签),
  • peer-to-peer mode (ie it polls and listens for other peer-to-peer mode targets), and 点对点模式(即轮询和侦听其他点对点模式目标),以及
  • (sometimes) card emulation mode (ie it listens for activation by other reader/writer devices). (有时)卡仿真模式(即它监听其他读写器设备的激活)。

When the NFC device polls for tags, this is usually similar to what InListPassiveTarget does, with the difference that it will typically test for various different tag technologies (NFC-A = ISO/IEC 14443A, NFC-B = ISO/IEC 14443B, NFC-F = JIS X 6319-4, and NFC-V = ISO/IEC 15693). 当NFC设备轮询标签时,这通常类似于InListPassiveTarget所做的,不同之处在于它通常会测试各种不同的标签技术(NFC-A = ISO / IEC 14443A,NFC-B = ISO / IEC 14443B,NFC -F = JIS X 6319-4,和NFC-V = ISO / IEC 15693)。 Since passive peer-to-peer mode is based on ISO/IEC 14443A and JIS X 6319-4, it will also automatically discover passive peer-to-peer targets with the tests for NFC-A and NFC-F. 由于被动对等模式基于ISO / IEC 14443A和JIS X 6319-4,它还将通过NFC-A和NFC-F测试自动发现被动对等目标。 Moreover, it will test for other peer-to-peer devices (ISO/IEC 18092 active mode). 此外,它还将测试其他对等设备(ISO / IEC 18092活动模式)。

When the NFC device listens for activation by other devices (peer-to-peer or reader/writer mode), it will allow activation with the various options defined in ISO/IEC 18092. Since these sub-protocols are based on NFC-A and NFC-F, an NFC device that supports both passive peer-to-peer mode and card emulation mode will typically combine the listening "phases" for both into a single activatable target. 当NFC设备通过其他设备(对等或读写器模式)监听激活时,它将允许使用ISO / IEC 18092中定义的各种选项进行激活。由于这些子协议基于NFC-A和NFC-F是支持被动对等模式和卡仿真模式的NFC设备,通常将两者的收听“阶段”组合成单个可激活目标。

And that is exactly what you see: A device that that supports ISO/IEC 14443A and ISO/IEC 18092 passive mode at 106kbps. 这正是您所看到的:支持ISO / IEC 14443A和ISO / IEC 18092被动模式的设备,速率为106kbps。 Both of these protocols use the anticollision and selection procedure from ISO/IEC 14443A. 这两种协议都使用ISO / IEC 14443A的防冲突和选择程序。 In order to simplify discovery, the NFC device combines both, card emulation mode and peer-to-peer mode into one single visible target (ie one "card" with one UID). 为了简化发现,NFC设备将卡仿真模式和对等模式两者组合成一个单个可见目标(即具有一个UID的一个“卡”)。

You can see this clearly from the parameter values received during activation (specifically the SAK/SEL_RES byte). 您可以从激活期间接收的参数值(特别是SAK / SEL_RES字节)中清楚地看到这一点。 This byte is 0x60 for the Nexus 5X which indicates that this target supports ISO/IEC 14443-4 (Type A) with bit 5 set and ISO/IEC 18092 passive at 106kbps with bit 6 set. 对于Nexus 5X,此字节为0x60 ,表示此目标支持ISO / IEC 14443-4(类型A),第5位设置,ISO / IEC 18092无源,速率为106kbps,第6位设置。

Note that Samsung on their Galaxy S4 decided not to announce peer-to-peer mode support in the SAK byte ( 0x20 ), probably due to problems with some credit card payment terminals. 请注意,三星在他们的Galaxy S4上决定不宣布SAK字节( 0x20 )中的对等模式支持,可能是由于某些信用卡支付终端的问题。

This would be fine but I think that Android is spinning up a service which blocks it from reading the emulated tag. 这样会很好,但我认为Android正在启动一项服务,阻止它阅读模拟标签。

It's actually not a service spinning up but Android intentially introducing a timeout. 它实际上并不是一项服务,而是Android在内部引入了超时。 Once an Android device was activated in peer-to-peer or card emulation mode, an Android device will wait for approximately 4 seconds after the external reader turned off its HF field, before it re-starts polling for tags. 一旦Android设备在点对点或卡仿真模式下激活,Android设备将在外部阅读器关闭其HF字段后等待大约4秒,然后重新开始轮询标签。 This is at least what we measured for Nexus devices using libnfc-nci. 这至少是我们使用libnfc-nci测量的Nexus设备。 Samsung is known for extensively modifying the NFC stack implementation (usually introducing a lot of additional bugs (features?)), thus this might be slightly different on the S4. 三星以广泛修改NFC堆栈实现而着称(通常会引入许多额外的错误(功能?)),因此在S4上可能略有不同。

Do I need to do something special to toggle between the modes? 我是否需要做一些特殊的事情来切换模式? I don't actually reset or reinitialize the PN532 when I toggle. 切换时,我实际上没有重置或重新初始化PN532。

You are probably doing what you should/could do. 你可能正在做你应该/可以做的事情。 It's an intentional timeout (4 seconds after field-off) on the Android side that causes what you experience. 这是Android方面的故意超时(场外关闭后4秒)导致您体验的内容。

Is there a command I can send to tell the devices to stop listening or a setting I can send that will fix this? 是否有一个命令我可以发送告诉设备停止收听或我可以发送的设置将解决这个问题?

Unfortunately, that's usually not that easy. 不幸的是,这通常不那么容易。 A few things that you could do: 你可以做的一些事情:

  • Do you really need reader/writer mode on the Android phone? 你真的需要在Android手机上使用读写器模式吗? If you could implement all the functionality that you need on the Android side with host-card emulation (HCE), you could implement your PN532 side to only operate in reader/writer mode. 如果您可以使用主机卡仿真(HCE)在Android端实现所需的所有功能,则可以将PN532端实现为仅在读取器/写入器模式下运行。 This would eliminate the switching between two modes and you would no longer be affected by the timeout. 这将消除两种模式之间的切换,您将不再受超时影响。 Unfortunately, this would limit you to devices that support Android HCE (Android 4.4+ and certain newer NFC chipsets). 不幸的是,这会限制您使用支持Android HCE(Android 4.4+和某些较新的NFC芯片组)的设备。 Moreover, some things like automatically installing the app if missing (through an Android Application Record, AAR) is not possible then. 此外,如果缺少(通过Android应用程序记录,AAR)自动安装应用程序等一些事情是不可能的。 You could still launch an activity of your app when a reader communicates with your HCE service though (see How can I send message from HostApduService to an activity? ). 当读者与您的HCE服务通信时,您仍然可以启动应用程序的活动(请参阅如何从HostApduService向活动发送消息? )。

  • You could increase the time that the PN532 is in card emulation mode to ~5 seconds (some value significantly larger than the timeout mentioned above). 您可以将PN532处于卡仿真模式的时间增加到约5秒(某些值明显大于上面提到的超时)。 Then Android should (quite) reliably detect the emulated tag even if it was activated in card emulation mode/as passive peer-to-peer mode target before. 然后Android应该(非常)可靠地检测模拟标签,即使它在卡模拟模式下被激活/之前被动对等模式目标。 Unfortunately, a 5 second wait time before the PN532 is capable of detecting a passive tag is typically not acceptable in terms of user-experience for the case where the PN532 should read NFC tags/contactless-smartcards. 不幸的是,在PN532能够检测无源标签之前的5秒等待时间在PN532应该读取NFC标签/非接触式智能卡的情况下,在用户体验方面通常是不可接受的。

  • If your app only needs to be able to communicate with the PN532 while an activity of your app is in the foreground (ie when users manually open the app before they try to interact with the PN532 side), you could disable P2P mode using the reader-mode API (see NfcAdapter.enableReaderMode() . This again restricts you to devices running Android 4.4+ (though supported across all NFC chipsets). I doubt that this would be much of a benefit over using HCE on the Android side though since the problems are back there when a user tries to tap the PN532-side with their phone in order to automatically open the app or to get the app installed (through an AAR). 如果您的应用只需要能够与应用程序的活动在前台时(即当用户尝试与PN532端进行交互之前 手动打开应用程序)与PN532进行通信时,您可以使用阅读器禁用P2P模式-mode API(请参阅NfcAdapter.enableReaderMode() 。这再次限制您使用运行Android 4.4+的设备(虽然支持所有NFC芯片组)。我怀疑这比在Android端使用HCE有很多好处,尽管当用户试图用他们的手机点击PN532端以便自动打开应用程序或安装应用程序(通过AAR)时,问题就会出现。

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

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