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?.
Related
I am looking for a way to establish p2p communication between an Android phone (lets call it A) and a PN532+microcontroller (lets call them B).
By p2p I mean a scenario in which:
1. A sends some application specific data to B
2. B receives data and sends a response containing application specific data to A.
I have seen similar questions but most of them date back to 2011, stating that such functionality is not yet supported by the API. Looking at the latest API I can still only see a solution in which B is treated as a TAG.
Am I missing something or is the kind of communication I am looking for is still not supported?
When you want to specifically use NFC peer-to-peer mode, then the same answers are valid today: Android only supports Android Beam for P2P communication. Hence, you can, in general, only transmit one message into one direction and you need to tap the Beam UI each time you want to send data. However, if your interaction is exactly 1 message from Android to PN532 and then 1 message from PN532 to Android, and you are okay with tapping the Beam UI when sending from Android, you could use some tricks to first receive the message from Android, then disconnect and reconnect to send the response to Android.
When you are open to support other modes, then you have some options:
If the Android device has Android 4.4 or later and supports host card emulation (HCE): Use the PN532 in reader/writer mode and implement a HCE service on the Android side.
Otherwise, use the PN532 in host card emulation mode and access it from the Android side using the standard NFC reader/writer API (e.g. IsoDep / NfcA or NfcF).
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.
I want to create an application to exchange information between 2 devices via NFC.
I know how to make one mobile send information to the other using Android Beam. What I don't know is how two phones could send data to each other with just one touch.
Is this possible? If yes, how?
That depends on what you are trying to achieve and what Android version(s) you are using:
Both devices with Android < 4.0
Both devices can send one NDEF message each with no user interaction required. The messages cannot depend on each other (i.e. it's not possible that one device sends a message and the other one sends an answer to this). You would use a combination of enableForegroundNdefPush() and enableForegroundDispatch() to achieve this.
At least one device with Android < 4.4
Both devices can (theoretically) send one NDEF message per touch, but user interaction is required on both devices (i.e. the user needs to touch the Beam UI). Moreover the Beam UI on both devices needs to be touched pretty much at the same time. Otherwise, the Beam UI on the other device will get interrupted due to the received NDEF message. Thus, this "solution" is not really usable. You would use a combination of setNdefPushMessage*() and enableForegroundDispatch() to achieve this.
Both devices with Android 4.4+
Starting with version 4.4, Android has two new features:
NFC reader mode and
Host-based Card Emulation (HCE).
When you combine those feature (i.e. you have a HCE on-host card emulation service on one device and put the second device into reader mode), both devices can communicate with each other (real bi-directional communication) using ISO 7816-4 APDUs.
This is possible, as explained online here. There is also an API demo in the API demos provided with the SDK that discusses this.
However, keep in mind that NFC has a very small payload size, and you're unlikely to be able to transfer any sizable data using it. NFC should instead be used to quickly setup bluetooth connections, or another form of wireless transfer like WiFi direct, which can then be used to transfer larger amounts of data.
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.
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.