I have two apps on two Android devices, similar to CardEmulation and CardReader from the samples for NFC.
I want to send data from the host card emulation app running on one Android device to the reader application running on the second Android device. This works when both apps are open in foreground.
But if I close the reader application, I can no longer send data to it. Instead, when the two phones are held together, they activate Beam transmission. However, I would like to have the reader phone automatically start the reader app when the two phones are held together.
So question is:
Can I send data from the HCE app so that the reader receives a
TECH_DISCOVERED
intent when the device are put close to eachother? (We don't want the additional acceptance click for Beam mode.)If that's not possible, can the
HostApduService
send data so that the reader receives anNDEF_DISCOVERED
intent?
Unfortunately, that's not possible. Due to the way the Android NFC stack implements polling for other NFC devices (peer-to-peer mode and NFC tags) it is not possible to launch a reader application on one Android device through a host-based card emulation (HCE) application on another Android device.
The Android NFC stack, by default polls for both, NFC tags and NFC peer-to-peer devices. As a result, when two Android devices are held together, they will automatically communicate through NFC peer-to-peer mode and not through reader/writer mode + card emulation mode.
As a consequence, the "reader" device cannot detect the HostApduService
running on the second device. Therefore, an Android device cannot emulate an NFC tag (that contains an NDEF message) that would trigger an application (that filters for an NDEF_DISCOVERED
intent or even a TECH_DISCOVERED
or TAG_DISCOVERED
intent) on a second Android device.
The only way to communicate between one Android device (that is in reader/writer mode) and an HCE application (HostApduService
) on a second Android device is by using the reader mode API). This disable peer-to-peer polling and consequently allows the reader Android device to discover the card emulation mode of the other device. However, in order to use this API your application must already have a foreground activity running on the reader device.
Note that disabling Android Beam (through the Settings app) won't disable peer-to-peer mode. So doing this won't change anything.
来源:https://stackoverflow.com/questions/37188472/nfc-card-emulation-with-closed-reader-application