Android Tutorial Android Beam Tutorial

Discussion in 'Tutorials & Examples' started by Erel, Nov 25, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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:
    Sub Process_Globals
    End Sub

    Sub Globals
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    Private nfc As NFC
    End Sub

    Sub nfc_CreateMessage As List
    Return Array (nfc.CreateUriRecord(""))
    End Sub
    The message sent is handled like any other NDEF tag. See this tutorial for more information about these tags:

    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:
    Sub nfc_CreateMessage As List
    Dim n1 As Name
       n1.First = txtFirst.Text
       n1.Last = txtLast.Text
    Return Array (nfc.CreateMimeRecord("application/vnd.b4a.example", _
    End Sub
    And in the manifest editor:
    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" />
    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.

    Attached Files:

    Last edited: Nov 16, 2016
  2. Lectos

    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(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(
    at anywheresoftware.b4a.ShellBA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent2(
    at anywheresoftware.b4a.BA.raiseEvent(
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(
    at android.view.View.performClick(
    at android.view.View$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(

    Thanks for your help.
    Last edited: Nov 17, 2016
  3. Erel

    Erel Administrator Staff Member Licensed User

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice