android device in homescreen as NFC target - android

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.

Related

NFC interaction between Android devices triggers Android Beam instead of HCE

I'm developing payment terminal simulator that acts as a standalone POS terminal. It works ok with chip contactless cards (it can communicate with them on proper protocol), initiate transaction, read data from them etc.
I also wanted to handle transactions done by Google Pay (POS terminals recognize Google Pay as normal contactless card - same protocol). But when I have one Android device with my POS terminal app running, and another Android device with Google Pay, when I touch both devices together, my POS app is minimized and "Touch to send beam" is shown. I would have expected that instead the onNewIntent method is called in the POS terminal app.
How to handle this? The expected behaviour is that my app (POS terminal) is not minimized, and on the other phone the Google Pay app is triggered.
Should I send something at the begginng, so that Android with Google Pay can recognize that this is a payment transaction? Or maybe some specific intent-filter to handle this on the POS terminal Android device?
I also observed that in one of three to four tries, I seem to get the expected behaviour - onNewIntent is called in my app. This looks like some race condition beetween my app and the default Android Beam behaviour.
You can't use the regular tag dispatch mechanisms (intent filter or foreground dispatch system) to interact with another Android device that operates in card emulation (HCE) mode. The problem is that the two Android devices will typically automatically discover their peer-to-peer mode capabilities. Hence, they connect in P2P mode and trigger Android Beam (SNEP on top of LLCP on top of P2P). Note that even if you disable Android Beam on both devices, they will still choose P2P mode.
To overcome this, you will need to use the reader mode API. This allows you to disable P2P mode and selectively enable reader/writer mode functionality (e.g. different technologies) while your activity is in the foreground.

NFC card emulation with closed reader application

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.

How can my app find out which NFC mode is enabled?

I've found that Android devices support three NFC modes: reader/writer, P2P, and card emulation.
Is it possible for my app to check which mode is currently enabled?
No, your app can't really check what mode is currently enabled. The point is that, by default, an Android device will periodically check for all three modes (actually it's a bit more complicated than that: those three modes are themselves split into several parts, each polling for a certain technology). So, while the screen is on, you can expect the Android device to cyclically switch between listening for other readers and polling for different types of tag technologies and peer-to-peer mode devices. This is the case regardless of whether you registered to send a message over peer-to-peer mode, an app is registered for HCE, or your app registered for NFC discovery intents. Even if you disable Android Beam in settings, the device will remain to poll for peer-to-peer mode.
Only if you explicitly disable card emulation and P2P using the reader mode API (available on Android 4.4+), your app can control that only reader/writer mode is active (and it can even control which tag technologies should be polled for).
So your app can know what modes it enabled itself and can do some guessing, e.g.
if the screen is on and reader/writer mode was not explicitly forced using the reader mode API, all three modes are likely to be active.
if the screen is off, only card emulation mode may be active (though this is device dependent and HCE is usually not available).

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.

Share tag NFC between two device Android

It is possible?
I state that NFC is enabled on my phone and everything looks correct
I tried with an app called NFC TagWriter by NXP but don't work.
I create a tag with this app and then I tried to listen with other device but don't work, then I installed same app on the other device but don't work.
Please help me or suggested to me another way to do(share tag NFC).
THANKS!
The Android Beamâ„¢ feature allows a device to push an NDEF message onto another device by physically tapping the devices together. This interaction provides an easier way to send data than other wireless technologies like Bluetooth, because with NFC, no manual device discovery or pairing is required. The connection is automatically started when two devices come into range. Android Beam is available through a set of NFC APIs, so any application can transmit information between devices. For example, the Contacts, Browser, and YouTube applications use Android Beam to share contacts, web pages, and videos with other devices.
Reference from Developer Documentation
Also check this for Blog , it explains how to communicate between devices.
You can have (indepedent) p2p communication in 2 directions, and enableForegroundNdefPush is deprecated now; please, use setNdefPushMessage

Categories

Resources