NFC card emulation with closed reader application - android

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 an NDEF_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.

Related

Host-based Card Emulation - Trigger a reader app when HCE is read on reader device

NFC tags can trigger an NFC reader app available on the phone, but HCE does not trigger any reader app. However, if I first open the reader app, then it works normally and, hence, can read the HCE emulated tag.
I need a solution where HCE can open or prompt to choose the reader app.
Is this possible with HCE? Why does Android not prompt to open an NFC reader app when detecting a HCE device?
No, a HCE app on one Android device cannot be used to trigger (launch) a reader app on another Android device. The reason for this is that Android devices also implement peer-to-peer mode. Thus, by default, two Android devices will automatically communicate in peer-to-peer mode and the reader side won't see the HCE emulated tag.
Only if a reader app is started on the reader side that uses the reader-mode API to disable peer-to-peer mode, the two devices start communicating in reader/writer mode + card emulation mode. Thus, only then the HCE emulated tag becomes visible to the reader side.
Note that disabling Beam from the settings menu won't change anything about this because peer-to-peer mode stays enabled even if Beam (currently the only application protocol used by Android devices on top of peer-to-peer mode) is disabled.
First of all you must ensure you use Kitkat version of android to have HCE Support. Next, since HCE routs your Application ID to CPU and not to eSE or UICC Sim card, any app which depends on eSE or UICC sim card doesnt work with HCE. So you must be using HCE supported app to be identified by the reader. Next, the reader should send out Application ID such that any installed HCE app on your phone is able to respond to it.

NFC Reader <-> Android communication and Android listener

I have some questions about the communication between a NFC reader and an Android phone.
The first subject is the Android / NFC reader modus:
What is the best way to pass a unique identifier from the NFC reader (ACR122U) to an Android device? ACR122U in peer-to-peer, card emulation, or reader/writer mode? Wich modus are possible? And what is the best mode if you want bi-directional communication?
The second subject is about the listener on the android device:
How can a Android reader trigger an Android application? When you tap your phone on the NFC reader, do you need to have the Android application in the front or do you need to write a NFC listener or something while your application is running on the background?
Many thanks.
What is the best way to pass a unique identifier from the NFC reader (ACR122U) to an Android device?
That depends on what you want to achieve.
Using the ACR122U in reader/writer mode will basically limit you to using Android 4.4 and later (as Android HCE -- for putting the phone into host-based card emulation mode -- is only supported from 4.4 onwards).
Peer-to-peer mode will work but will effectively limit you to sending one data packet (or one message) into a single direction.
Using the ACR122U in card emulation mode will permit you to perform true bidirectional communication (i.e. to send multiple data packets into both direction).
See this answer for a list of further literature.
Which modes are possible?
All three modes are possible but give you different degrees of freedom.
And what is the best mode if you want bi-directional communication?
See above, using the ACR122U in card emulation mode and the phone in reader/writer mode will permit bi-directional communication on all Android versions. The phone has the role of the interrogator (in a command-response protocol) in this case.
If you only want/need to support Android 4.4 and later, you could just as well use the phone in HCE (host-based card emulation) mode and the ACR122U in reader/writer mode.
When you tap your phone on the NFC reader, do you need to have the Android application in the front or do you need to write a NFC listener or something while your application is running on the background?
If you are using Android HCE (phone as emulated card): In this case, the communication is handled by a background service. No user interface is needed on the device. If you want to start a user interface upon scanning the ACR122U with a phone, I suggest that you stick with using the Android in reader/writer or peer-to-peer mode (though you could start a UI through the HCE service too).
If you are using the Android in reader/writer mode: You need an app running in the foreground to communicate with an NFC tag/smartcard/emulated card. However, the
app can be started automatically upon detection of the tag. You would
use an intent filter that matches either the tag's technology or
(better yet) matches an NDEF record that is stored on the tag. See
this for further details.

a few basic questions about NFC, NDEF and NFC enabled android phones

I want to start android programming to be able to transfer data between a NFC enabled smartphone and a custom device, but I am not able to find clear information about a few things
For example, my custom device has to be able to send data to the smartphone as well as receive from it. But from what I have read it is not currently possible for two way communication using NFC
How to send data between two devices using NFC in android?
or the more recent,
NFC enabled phone act as a NFC tag
But the Android developers website itself says that we can use Beam to perform peer to peer communication between two android phones, so two way communication should be possible shouldn't it?
Android Beam allows simple peer-to-peer data exchange between two
Android-powered devices. The application that wants to beam data to
another device must be in the foreground and the device receiving the
data must not be locked. When the beaming device comes in close enough
contact with a receiving device, the beaming device displays the
"Touch to Beam" UI. The user can then choose whether or not to beam
the message to the receiving device.
http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#p2p
Secondly, can I use an android NFC phone as a tag writer (instead of just a reader)? That is, just send data to my custom device (or a passive NFC tag)?
The keyword in "simple peer-to-peer data exchange" is simple. Currently (Android 4.2), apps cannot do 2-way communication over Beam. However, a custom device can send multiple messages over Beam. All of these will be received by the Android device without problems, but the Android app can only send one message. You can trick the Android device, however, by resetting the RF field in your custom device. It will appear to the Android device as if a new device has appeared and it will show the "Touch to Beam" UI again.
Android NFC devices can act as both tag reader and writer.

Bi-Directional Android Beam

Here's what I'm trying to do with 2 Galaxy Nexuses running Android 4.0 with NFC capabilities:
Device A and B are in close enough to initiate an Android Beam UI.
Device A sends a beam to Device B
Device B received beam, does some processing, and then sends a response beam back to device A
I have the initial beam from A -> B working fine but not the communication back. I do want to send something more than just an 'ACk', like a small payload of data. Is there any way to do this over NFC with the current APIs or will I have to look towards WiFi/BT p2p communication?
I don't think this is possible unless you pull the devices apart then put them together again and explicitly beam data from Device B to A with the touch to beam UI. You can use NFC to initiate a BT connection though and get the job done.
For now, (unfortunately) peer-to-peer mode is only accessible on Android through Android Beam, which can't be used for bi-directional communication.
If you want to archieve true bi-directional communication, you can better use reader/writer & card-emulation modes.
When emulate a ISO/IEC 14443-4 tag, you can use APDU commands (see ISO/IEC 7816-4) to send bi-directional information.
while the two devices are close to each other you can show a toast at B device to ensure it receive the NDEf message
I have not tried that myself, but you might be able to acheive a form of bi-directional communication by using one phone in reader/writer mode and the other phone in host based card emulation mode (HCE). See also this thread: NFC reader/writer mode - is it only for tags?.

android device in homescreen as NFC target

When an android device with NFC is in home screen (screen on of course, otherwise the NFC won't work), does it act as an NFC target? If so, what data does it show?
In other words, can I write an app for one phone that can identify other phones (without any propriety app)?
you can communicate with other NFC phone in the so-called NFC p2p mode (see the example here). In this case must be on both devices your application installed.
The other option for the device is to act as a NFC tag reader/writer - but this is not an option for your application, because in this mode phone communicates with the passive NFC tags.
The third option is to enable the phone to work in card emulation mode. In this mode phone mode emulates the contactless smart card. This is not possible with current Android OS version API and it is anyway intended only for e.g. banking and payment applications.
See http://developer.android.com/reference/android/nfc/Tag.html
First, if any app enabled foreground dispatch, those will get notified. In your case - it's at the home screen, so this does not applies.
Afterwards, it basically goes through the intents list to see if any app is interested at any tag.
In the other word, when the screen is on and at the home screen, the behavior is like you have QR code scanner always scanning for QR code, and Browser/Phone Contact/Whatever app when a corresponding code is scanned - except is this case it's about NFC tag, not QR code.
The phone never act as tag (passive device) except in specific payment mode (Google checkout) - aka card emulation mode. However, the API does not expose this mode for developer.

Categories

Resources