Android Beam is a feature introduced in Android 4 that allows one device to transfer a message to another device by placing the devices back to back. The communication is based on NFC.
The main advantage of this feature is that it works with almost no configuration required by the user. Just put the two devices together and click on the screen to send the data:
The push message is tied to a specific activity. This activity should be in the foreground for this to work (on the device that is sending the data).
The other device screen should be turned on and the device should be unlocked.
NFC library v1.50 adds support for Android Beam.
Two steps are required in order to allow sending a message from the current device:
1. Call NFC.PreparePushMessage in Activity_Create. Note that it should always be called. Not just when FirstTime is true.
2. Handle the CreateMessage event and return a list with the records that will be sent.
For example the following code will cause the other device to open the browser with the given url:
The message sent is handled like any other NDEF tag. See this tutorial for more information about these tags: https://www.b4x.com/android/forum/threads/reading-ndef-data-from-nfc-tags.14931/#content
Together with B4XSerializator which was added in RandomAccessFile v2.10, it is simple to transfer more complicated objects. In the second example we send a custom message. Our app is installed on both devices.
The important code is:
And in the manifest editor:
Note that the app doesn't need to run on the other device. It will start automatically.
NFC is quite slow, which makes this solution good for small messages (probably up to 50k or 100k bytes).
Android Beam is available from Android 4.0.
The CreateMimeRecord is available from Android 4.1
It is recommended to set the minSdkVersion to 16 in the manifest editor.
The main advantage of this feature is that it works with almost no configuration required by the user. Just put the two devices together and click on the screen to send the data:
The push message is tied to a specific activity. This activity should be in the foreground for this to work (on the device that is sending the data).
The other device screen should be turned on and the device should be unlocked.
NFC library v1.50 adds support for Android Beam.
Two steps are required in order to allow sending a message from the current device:
1. Call NFC.PreparePushMessage in Activity_Create. Note that it should always be called. Not just when FirstTime is true.
2. Handle the CreateMessage event and return a list with the records that will be sent.
For example the following code will cause the other device to open the browser with the given url:
B4X:
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Private nfc As NFC
nfc.PreparePushMessage("nfc")
End Sub
Sub nfc_CreateMessage As List
Return Array (nfc.CreateUriRecord("https://www.b4x.com"))
End Sub
The message sent is handled like any other NDEF tag. See this tutorial for more information about these tags: https://www.b4x.com/android/forum/threads/reading-ndef-data-from-nfc-tags.14931/#content
Together with B4XSerializator which was added in RandomAccessFile v2.10, it is simple to transfer more complicated objects. In the second example we send a custom message. Our app is installed on both devices.
The important code is:
B4X:
Sub nfc_CreateMessage As List
Dim n1 As Name
n1.Initialize
n1.First = txtFirst.Text
n1.Last = txtLast.Text
Return Array (nfc.CreateMimeRecord("application/vnd.b4a.example", _
serializator.ConvertObjectToBytes(n1)))
End Sub
B4X:
AddActivityText(main, <intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.b4a.example" />
</intent-filter>)
Note that the app doesn't need to run on the other device. It will start automatically.
NFC is quite slow, which makes this solution good for small messages (probably up to 50k or 100k bytes).
Android Beam is available from Android 4.0.
The CreateMimeRecord is available from Android 4.1
It is recommended to set the minSdkVersion to 16 in the manifest editor.
Attachments
Last edited: