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: 243
Last edited:

Filippo

Expert
Licensed User
Did it make reading faster?
For me, it is just as fast as the Serial Bluetooth. ;)
 

Intek

Member
Licensed User
About WriteData I write the following code and I have no error but then it did not write the value, someone has tried it?
Am I doing something wrong?

state = state + 1
sff(1) = Bit.AND(state, 255)
sff(0) = ((state - Unsigned(sff(1))) / 256
manager.WriteData("FF00", "FFF1", sff)
 

Intek

Member
Licensed User
Now appears in the log the successful writing and now I can write in characteristic.
I checked the code again and maybe something was wrong, anyway now works.
Thank you very much again. :)
 

henrywood

Active Member
Licensed User
Can the BLEManager be used with iBeacons so that events are fired when a beacon region is entered or exited (like the easiBeacon library for B4A ?
 

henrywood

Active Member
Licensed User
This may be dumb question, but here goes:

If the Iphone is sleeping or my app is not in the foreground/running, will periodically scanning for beacons in my app (to raise the DeviceFound event) still work ?
I need to react in my app when new iBeacons are in range and from what I've gathered thus far I need to react to the DeviceFound event but will that event even be raised if the app is not in the foreground / running ? According to Apple, iOS 7.1+ supports notifying apps of iBeacons nearby even when the app is closed / not in the foreground, but will it work like this in the BLEManager also ?
 

Filippo

Expert
Licensed User
Hi,

Is there an event if no BLE device is found or a timeout?
B4X:
Sub btnScan_Click
    manager.Scan(Null)
End Sub
 

Filippo

Expert
Licensed User
There is no event in this case. You can use a timer to implement a timeout (call StopScan after x seconds).
Thanks :)
 

Ph1lJ

Member
Licensed User
Hi
I just tried to install v1.20 and it continues to show 1.11, I have unchecked and re-checked, to force a refresh

Any one got any ideas - I'm using B41 1.8
 
Top