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:

Turbo3

Active Member
Licensed User
Could we get a library version without the debug messages like we did for the Android version?

B4X:
Value written for characteristic: xxxx
 
Last edited:

Filippo

Expert
Licensed User
If my app goes in background remains my BLE device also connected, but the app does not get any more data, is that normal?
Only when the app comes back to foreground, receives back the data.
 

Filippo

Expert
Licensed User
I have been studying last night this side.
If I understood it correctly should be possible to receive and send its data from BLE devices in backgroud, but I do not understand how? :(
 

Filippo

Expert
Licensed User

Filippo

Expert
Licensed User
As I said, if the app is in the backgound connection remains, but data is not received and not sent.

Could this code help?
B4X:
Private Sub Application_Background
    bluetooth.btManager.SetNotify(bluetooth.ConnectedServices.Get(0), "FFE1", True)
End Sub
 
Last edited:

Erel

Administrator
Staff member
Licensed User

Filippo

Expert
Licensed User
Thank Erel, I'll try it tonight. :)
 

Filippo

Expert
Licensed User
Thank Erel, I'll try it tonight. :)
Unfortunately it does not work. :(
What else can I do?
For my app is very important, it should in backgound receive the data so that the distance traveled is detected, as with GPS.
 

Filippo

Expert
Licensed User
Thank you anyway.:)
You do not guilt if it does not work, only Apple is guilty with his OS! :mad:
 

f0raster0

Well-Known Member
Licensed User
trying read the battery level..what should I add to my code below to do it?

B4X:
Sub Manager_DataAvailable (Service As String, Characteristics As Map)
    ActivityIndicator2.Visible = False
   Dim bc As ByteConverter
   Dim KeyCode As String
    'For Each id As String In Characteristics.Keys  
        If Service="180F" Then ' 180F=Battery Service , 2A19=Battery Level
           For Each id As String In Characteristics.Keys
             KeyCode = Characteristics.Get(id)
             Log("keyCode=" & KeyCode&" Hex: "&bc.HexFromBytes(Characteristics.Get(id)))           
           Next
        End If
    'Next
End Sub
my log:
Discovering services
Services discovery completed.
keyCode=<B4IArray: 0x156a5d90> Hex: F64301E7
Unexpected event (missing RaisesSynchronousEvents): manager_dataavailable::
keyCode=<B4IArray: 0x15662000> Hex: F64301E7
keyCode=<B4IArray: 0x156b19f0> Hex: F64301E7
keyCode=<B4IArray: 0x15662000> Hex: F64301E7
keyCode=<B4IArray: 0x156afdd0> Hex: F64301E7
keyCode=<B4IArray: 0x156aa750> Hex: F64301E7
keyCode=<B4IArray: 0x155b2b80> Hex: F64301E7
keyCode=<B4IArray: 0x155a24d0> Hex: F64301E7
keyCode=<B4IArray: 0x155dabd0> Hex: F64301E7
keyCode=<B4IArray: 0x155c6870> Hex: F64301E7
keyCode=<B4IArray: 0x156b4960> Hex: F64301E7
keyCode=<B4IArray: 0x156aaa20> Hex: F64301E7
keyCode=<B4IArray: 0x156b4960> Hex: F64301E7
keyCode=<B4IArray: 0x15581420> Hex: F64301E7
keyCode=<B4IArray: 0x155a24d0> Hex: F64301E7
keyCode=<B4IArray: 0x156bfa30> Hex: F64301E7
keyCode=<B4IArray: 0x156b63d0> Hex: F64301E7
keyCode=<B4IArray: 0x15576480> Hex: F64301E7

Note: If I use the nRF Master Control app. I'm able to read the battery % using that app
 
Last edited:

Fabio Campanella

Member
Licensed User
Hi,
Bluetooth Scan Works in BackgroundMode if you Specify UUID devices that you must scan:

I have Estimote Beacon and I try this

B4X:
   Dim oUUID As List
   oUUID.Initialize
   oUUID.Add("FE9A")
   manager.Scan2(oUUID, True)

FE9A Value is UUID device for estimote beacons. In background mode OS call Manager_DeviceFound Correctly

;)
 
Last edited:
Top