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:

walterf25

Expert
Licensed User
Longtime User
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).

Library installation:
- Download the zip file, unzip it and copy the xml file to the libraries folder.
Great Job Erel, is it possible to communicate with bluetooth devices in the way its possible with android, I mean, can i connect to a bluetooth module to read and write to it? I did some research a long time ago and from what i understood you need to apply for some ID chip in order to make that possible, do you know anything about that?

If it is possible i will soon be playing around with B4i, that would be the only reason why i would mess with B4i.

I'll wait for your reply.

Thanks,
Walter
 

walterf25

Expert
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.
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
 

tuhatinhvn

Active Member
Licensed User
Longtime User
i dont understand why library only have a file xml , in b4a we have 2 files (jar and xml). In b4i i want to code lỉbrary in xcode and use for my app in b4i,how to do it? Which files i need to create
 

moster67

Expert
Licensed User
Longtime User
@tuhatinhvn

I suspect libraries (and local installation of the same) will be available when the version of B4I which permits compiling on local network will be released. For now, compilation (with libraries) probably takes place remotely and are probably added to the code. This is also easier for Erel in this initial phase, since he can easily fix bugs without publishing new libraries for all users to download.

It is actually a genius way of Erel for beta-testing B4i ;)
 

Angel Maza

Member
Licensed User
Longtime User
I've some problems using the characteristics values:
Adding some code in the Sub Manager_DataAvailable, into the For Each id As String In Characteristics.Keys loop:
For each abc As Byte In Characteristics.Get(id)
When compiling I receive next error:
initializing '__strong id<B4IIterable>' with an expression of incompatible type 'NSObject *'
 

Angel Maza

Member
Licensed User
Longtime User
Solved with iRandomAccessFile lib and ByteConverter
B4X:
Sub Manager_DataAvailable (Service As String, Characteristics As Map)
   ActivityIndicator2.Visible = False
   Dim bc As ByteConverter
   clv.Add(CreateServiceItem(Service), 30, "")
   For Each id As String In Characteristics.Keys
     clv.Add(CreateCharacteristicItem(id, Characteristics.Get(id)), 40, "")
     Log( bc.HexFromBytes( Characteristics.Get(id) ) )
   Next
End Sub
 
Last edited:

ilan

Expert
Licensed User
Longtime User
is it possible to send data to another device via bluetooth with this lib?
would like to send text from one device to another is this possible?
 

ilan

Expert
Licensed User
Longtime User
yes i need to send very small messages.. will be great if this could be possible.
i would like to make a multiplayer game and need to send game progress from one device to other...
 

bubels

New Member
Licensed User
Longtime User
I can see that data read is possible but what about sending ?
I use modules that support BLE and 2.1 (dual). The manufacturer provides a program (ios) that allows the read and send ASCII string.
Anyway, I'm doing the same thing with the android in b4a
 

bubels

New Member
Licensed User
Longtime User
It's good news for me. Can you tell some thing about time? We are holding our project
because we want to work in one platform.
We have ready software for android but we need the same for IoS.
 

Erel

B4X founder
Staff member
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.
 

andreahaku

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?
 
Top