B4i Library BLE (Bluetooth Low Energy) library

B4i BLE library is now available. This library allows you to connect and read data from BLE peripheral devices.

Using this library is quite simple. There are three main steps:
1. Search for devices (assuming that the state is STATE_POWERED_ON).
2. Connect to a device.
3. Read data from the device.

There are several events which you need to handle:
StateChanged (State As Int) - This event is raised when you initialize BleManager and when the BLE adapter state changes. Only if the state is STATE_POWERED_ON then you can use this feature.

DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double) - Raised when a new device is discovered (after you call BleManager.Scan). The event parameters include the device Id, name, RSSI value and a Map with additional advertised data.

Connected (Services As List) - Raised when a device is connected. The Services list holds the device service ids. The data on the device is stored in a tree like structure made of services that hold characteristics.

DataAvailable (Service As String, Characteristics As Map) - Raised after you call Manager.ReadData. Service is the service that was read and Characteristics is a Map that holds the characteristics ids and values. The values are arrays of bytes.

Disconnected - Raised after a failed connection or after a device has disconnected.

Make sure to see the video in HD mode (click on the small gear button).


iBLE is now included as a preinstalled library.

Example was updated with a new requirement:
B4X:
#PlistExtra: <key>NSBluetoothAlwaysUsageDescription</key><string>Bluetooth used to connect to ...</string>
You should change the description as needed.

B4XPages example: https://www.b4x.com/android/forum/threads/b4x-ble-2-bluetooth-low-energy.59937/
 

Attachments

  • BleExample.zip
    4.1 KB · Views: 413
Last edited:

ilan

Expert
Licensed User
Longtime User
V1.10 is now available. It adds support for writing. It is a beta version.

There is one new method named WriteData.
This method expects three parameters: the service name, the characteristic uuid and the data.

You must call ReadData once and wait for the DataAvailable event to be ready before you can call WriteData.

Library installation

The zip file contains three files with the following extensions: .xml, .a and .h.
You need to copy the xml file to the internal libraries folder on the Windows computer.
If you are using a local Mac builder then you need to copy the .a and .h files to the Mac Libs folder.


sorry for my ignorance but what means "support writting"? is it now possible to send strings between 2 connected devices??
 

andreahaku

Member
Licensed User
Longtime User
sorry for my ignorance but what means "support writting"? is it now possible to send strings between 2 connected devices??

The previous version of the BLE library couldn't write but only read. Now it should allow writing as well as reading.
 

ilan

Expert
Licensed User
Longtime User
It is not possible to use this library to communicate between two devices. You can now write and read from peripheral BLE devices.

so what means write to peripheral BLE devices? i can write to my bluethoot earphones?? :confused:
 

mdehrnsb

Member
Licensed User
Longtime User
Erel,

I am a newbie to B4A. I have tried your BLE V1.0 library snippet code but found that I cannot get it to operate at all. It does not find my BLE module and just exits after the scan time. Is there something that I am missing. Does it rely on the standard Bluetooth administrator? I have tried the Bluetooth.zip example and it does find the module when doing a discovery. Also, I cannot find any links for the new BLE V1.10 library to download. I see that you have a B4i full example. Can you provide a full B4a example similar to a BLE scanner? Thanks.
 

MTailor

Member
Licensed User
Longtime User
Yes. Normal developers (like us) cannot access the SPP profile which is the one usually used for custom Bluetooth communication. Only BLE is exposed. You can write and read to BLE devices. You cannot open an input or ouput stream like you do in Android.

To communicate via SPP you need an authentication chip from Apple in your peripheral and use the iAP protocol.
If you just want to send streaming data, it is possible to use a virtual serial port service in many BLE modules.
For example try the module www.lairdtech.com/bl600
 

MTailor

Member
Licensed User
Longtime User
In other words I wouldn't be able to write an app to control any light switches etc... through a bluetooth module connected to a microcontroller????

Thanks,
Walter

If you use BLE rather than classic BT, then you will be able to
 

MTailor

Member
Licensed User
Longtime User
Currently it is not possible with this library.

Even when all of the BLE features will be implemented it will still not be so useful for communication between devices unless you need to send very small messages.

Another option is to use virtual serial services in BLE devices. That will allow you to send and receive data as a stream.
 

Ph1lJ

Member
Licensed User
Longtime User
Does anyone have an example of how to use the WriteData feature, I don't understand what is required ?
 

Ph1lJ

Member
Licensed User
Longtime User
Thanks a lot Erel!
I'll test it this week and let you know how it works.
What about having write supported on BLE library for B4A?
Hi andreahaku
Did you get this to work using the Write data function, I'm not at all sure what is required - can you help ?
 

Ph1lJ

Member
Licensed User
Longtime User
Top