Android Tutorial Android Beam Tutorial

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:

upload_2015-11-25_15-50-15.png


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
And in the manifest editor:
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

  • Beam.zip
    8.2 KB · Views: 980
Last edited:

Lectos

Member
Licensed User
Hello Erel:

I'm trying to use Android beam code and I'm getting an error when nfc.PreparePushMessage("nfc") is called. You can see the error below:

java.lang.NullPointerException: Attempt to read from field 'java.lang.ref.WeakReference anywheresoftware.b4a.BA$SharedProcessBA.activityBA' on a null object reference
at anywheresoftware.b4a.objects.NFC.PreparePushMessage(NFC.java:125)
at test.watch.main._send_test(main.java:1269)
at test.watch.main._imageviewimagen_click(main.java:1459)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)

Thanks for your help.
 
Last edited:
Top