B4A Library BLE Peripheral

Discussion in 'Additional libraries, classes and official updates' started by Erel, Sep 18, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    A BLE connection is made between a central device and a peripheral device. In most cases the Android device will be the central device.
    Beacons, heart rate sensors and other BLE devices are implemented as peripheral device.

    However the Android device can also implement the peripheral role. This is done with this library.
    It is supported by Android 5.0 (API 21+). Not all devices support this feature.
    Unofficial list of devices that support peripheral mode: https://altbeacon.github.io/android-beacon-library/beacon-transmitter-devices.html

    This library is implemented in a similar way to B4i peripheral feature: https://www.b4x.com/android/forum/threads/ble-chat-connecting-android-and-ios.66543/
    It creates a service (UUID = 0001) with two characteristics:
    ReadChar (1001) - Other devices should subscribe for notifications on this characteristics.
    WriteChar (1002) - Other devices can write to this characteristic.

    The NewData event is raised when a device writes to WriteChar.
    Other devices receive a notification when you call Peripheral.Write.

    Setup steps

    It is always recommended to implement communication related code in a service. The Starter service is a good place.
    1. Initialize a BleManager2 object.
    2. The StateChanged event will be raised. Assuming that the state is STATE_POWERED_ON you should initialize the BlePeripheral2 object.
    3. Make sure that peripheral role is supported with the IsPeripheralSupported property.
    4. Call Start to start advertising.

    The Subscribe event will be raised when a central device connects and registers for notifications.

    Multiple central devices can subscribe to a single peripheral.

    [​IMG]

    Example of a chat implementation with Android and iOS devices is attached.

    Updates

    - B4A projects were updated with targetSdkVersion set to 26 and the coarse permission is requested in the central project.
    - BLE2Peripheral v1.12 is attached. It allows overriding the default advertising settings. Example:
    Code:
    peripheral.Start2("B4APeripheral", CreateAdvertiseSettings)

    Private Sub CreateAdvertiseSettings As Object
       
    Dim builder As JavaObject
       builder.InitializeNewInstance(
    "android.bluetooth.le.AdvertiseSettings.Builder"Null)
       builder.RunMethod(
    "setConnectable"Array(True))
       builder.RunMethod(
    "setAdvertiseMode"Array(2)) 'ADVERTISE_MODE_LOW_LATENCY
       builder.RunMethod("setTxPowerLevel"Array(3)) 'ADVERTISE_TX_POWER_HIGH
       Return builder.RunMethod("build"Null)
    End Sub
    https://developer.android.com/reference/android/bluetooth/le/AdvertiseSettings.Builder
     

    Attached Files:

    Last edited: Oct 7, 2018
  2. avalle

    avalle Active Member Licensed User

    Hi Erel, thanks for this new library.
    I started from the sample and installed Central and Peripheral on two Android phones (Samsung Galaxy S6 and S8).

    A couple of issues:
    1. I've been unsuccessful to pair the Peripheral with the app running. The "B4A Peripheral" is visible from the other phone, but if I try to pair I get an error "cannot be paired". I've been able to pair the two phones with the Peripheral app not running.

    2. After the Central app has connected to the Peripheral app I can exchange messages. However messages sent from Central to Peripheral are displayed correctly on both devices, instead messages from Pripheral to Central are truncated to 2 characters on the display of the Central app.

    I've checked the code to look for some error, but I did not find anything evident. As I did not make any changes to your code I'm wondering if you have any ideas why the messages are truncated.
    [compiled with B4A 7.01 Win + JDK 8 v144 + SDK API 24]

    Thanks
    Andrea
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    1. This sounds like an OS issue. However you don't need to pair the devices at all.

    2. The central code limits the messages to 20 bytes. Some devices cannot write more than 20 bytes. You can remove this limit and try it with your devices (it is in a sub named something like TrimMessage).
     
  4. avalle

    avalle Active Member Licensed User

    Thanks Erel.

    For #1 I believed that pairing was necessary because Central and Peripheral did not connect. Now I think the issue is that the Galaxy S6 does not work well as a Peripheral. If I swap the roles they work just fine.

    For #2 I see the TrimMessage function, but I understand the issue with 20 char limit is becasue BLE specification doesn't allow write operations to exceed 20 bytes.

    Thanks
    Andrea
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    V1.11 released. Adds a Close method.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    V1.12 released. See first post.
     
Loading...
  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