B4A Library BLE Peripheral

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.

SS-2017-09-18_17.36.56.jpg


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:
B4X:
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
 

Attachments

  • BleCentral_B4i.zip
    3.5 KB · Views: 764
  • BleCentral_B4A.zip
    4.8 KB · Views: 1,241
  • BlePeripheral_B4A.zip
    9.8 KB · Views: 1,218
  • BLE2Peripheral.zip
    7.9 KB · Views: 1,166
Last edited:

avalle

Active Member
Licensed User
Longtime 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
 

avalle

Active Member
Licensed User
Longtime 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
 
Top