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

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).

Related

Can I access Android HCE application if device is turned off?

I'm currently write Android HCE application. And I want to use my HCE application when the device is turned off. I found a way to "Access HCE when Screen off", but no information of power off status.
Is there any way to use HCE application when phone is turned off?
No you can't access because of security reason
One correction also its not accessible if screen is off as mentioned in provided link.
Current Android implementations turn the NFC controller and the application processor off completely when the screen of the device is turned off. HCE services will therefore not work when the screen is off.
It may work on some devices only if vendor implements the OS customized.

NFC modes of operation confusion

I am trying to understand the technology behind NFC. I have a basic understanding of the protocol, the physics behind the technology and the immense power it brings to the wide range of applications that could be there.
Now I am trying to understand the the different modes in which an NFC enabled device (may be a smart phone) can operate.
I understand that the device may take up the role of a reader/writer or a card (card emulator). However, what I do not understand is what is the need for having the initiator and the target modes of operation ?
My understanding is :-
In a passive communication mode between 2 NFC enabled devices, one device takes up the active role (thus acting as the reader/writer) and the other device takes up the passive role (and thus acting as the card emulator).
And we know that the active device (in a passive mode of communication between 2 NFC devices) is the one that creates an RF field and constantly polls to find out nearby NFC enabled devices that are ready to act as a card emulator.
So is it not just obvious that the the device acting as an active device (taking up the reader/writer role) is the one that would be the initiator of the communication ? Why is a separate initiator and target ** mode required when it can be inferred easily from the **reader/writer or card role ?
You got it right:
The distinction between target and initiator is only of significance in the peer-to-peer communication mode (aka Android Beam). In this mode one device powers the field and acts like a card reader/writer. This is the initiator. The other device takes over the target role and technically acts like a card emulation.
The difference between target and initiator is significant in the LLCP protocol which implements the peer-to-peer functionality. Reason for that is, that the target cannot send data on it's own. It first has to be asked from the initiator if it wants to send something.
The LLCP protocol (among other things) abstracts this difference and lets the initiator poll the target at regular intervals. If you write communication code on top of this protocol (or anywhere higher in the protocol stack) you don't have to worry about the target/initiator mode at all anymore.
For completes sake here is also a operation mode within peer-to-peer where the two devices are powering the field (called active mode), but this is rarely used because support for it is not that great.
To answer your question:
Why is a separate initiator and target ** mode required when it can be
inferred easily from the **reader/writer or card role
The naming is there to avoid confusion. Take peer-to-peer initiator mode for example: Also it is technically 95% identical to reader/writer mode there are still some minor differences. You don't want your peer-to-peer initiator to connect to a real card but only to peer-to-peer targets for example.
Naming this mode initiator instead of reader/writer avoids confusion.

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.

NFC Peer2Peer Mode - Android Beam - ISO 18092

I need to find out how in Android Beam the P2P mode is managed.
I found some general information about P2P:
There is an active P2P mode (not recommended by NFC Forum) and a passive one (recommended by NFC Forum) where the passive device works like a smartcard (card emulation mode).
I also know that an NFC reader device does usually the following steps:
Field on
Check for a card or a P2P passive device
If found then communicate with the device, if not then field off
Field off
Check for external field for around 500ms-1s at least
If external field detected act as a card or P2P passive target. Receive commands and perform the transaction as a target
Wait until no more field detected
Goto 1
Now my question is, in Android Beam what happens when the 2 phones are put together?
I think they both create the field for scanning, and they both recognize them, by showing then the Android Beam interface (the smaller windows with the information for sending)
Then, the user types on the screen, and gets the iniator, while the other one goes to the passive receiver.
They pair by P2P passive and change the information over LLCP
The callback is sent to the iniator and the end.
I don't know if this is correct, does someone have information herefore?
What happens in Android Beam when the 2 smartphones/devices are put together, in which mode are they, and who gets when active/passive and which mode is used (P2P active or passive)?
In LLCP there is no master/slave so both are master and slave what exactly means this?
Thanks for your help!
As far as I remember it is as follows:
The initiator initiates the transmission, i.e., it is the one that begins the communication - comparable with first come first serve - independent of the direction of the data flow.
In a second step, the initiator dictates the kind of transmission mode, i.e., passive or active in peer-to-peer mode, or RFID mode (reader/writer mode). The chosen mode depends what kind of modes the initiator and/or the target supports. The passive mode is used when the initiator has enough power to drive both devices (itself and target). Whereas the active mode can be used if power should be shared among initiator and target device.
When two NFC devices are put together, the device which wants to communicate first, will be the initiator. Therefore, passive devices like tags are always targets. However, there is no peer-to-peer mode between NFC devices and RFID tags.
LLCP is just a top level protocol to transmit data in either way. Therefore, the initiator asks the target to transmit or receive data. The good thing about the NFC peer-to-peer mode is, that the NFC devices can swap its roles after each successful communication.

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