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: 333
Last edited:
D

Deleted member 103

Guest
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.
 
D

Deleted member 103

Guest
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? :(
 
D

Deleted member 103

Guest
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 by a moderator:

Erel

B4X founder
Staff member
Licensed User
Longtime User
D

Deleted member 103

Guest
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.
 
D

Deleted member 103

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

Fabio Campanella

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