12 March, 2014

NFC beam

To make beam work, items below are required:
1. Android Beam from the settings of Android OS must be turned on in both mobile phones.
2. The application on the mobile phone that wants to beam must be running in the foreground.
3. The screen of the mobile phone that will receive the beam must not be locked.
4. The user must touch to the screen of the mobile phone that will beam.

What's the min SDK version required:

API Level 14

What's the permission we need to ask for:

<uses-permission android:name="android.permission.NFC" />

How to push beam message:

We have two ways: static / dynamical:
setNdefPushMessage(): Accepts an NdefMessage to set as the message to beam. Automatically beams the message when two devices are in close enough proximity.
setNdefPushMessageCallback(): Accepts a callback that contains a createNdefMessage() which is called when a device is in range to beam data to. The callback lets you create the NDEF message only when necessary.
An activity can only push one NDEF message at a time, so setNdefPushMessageCallback() takes precedence over setNdefPushMessage() if both are set.
Normally we use the second one to get a flexible way to handle the message.

How to disable the beam push:

there are serveral ways: return null message in CreateNdefMessageCallback, or set message null in setNdefPushMessage, or set message null in setNdefPushMessageCallback.

@Override
public void onResume() {
super.onResume();
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
@Override
public void onNewIntent(Intent intent) {
// onResume gets called after this to handle the intent
setIntent(intent);
}