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

Ph1lJ

Member
Licensed User
Longtime User
Hi Ph1lJ,

I also use the "TinySine BLE modules", and this code works for me.

B4X:
btManager.WriteData(ConnectedServices.Get(0), "FFE1", Array As Byte(1))
Hi Flippo

I orginally had good success with you suggestion, but I recently recieved a new TinySine that works well with the Androids, but I cannot write data out to it using B4i, I'm using the iBLE v1.2 and all I get on the LOG is
"Error writing value for characteristic: FFE1"

I can connect and read at least I think it is as it's identical to what you see in post #71

Really frustrating

Any ideas
 
D

Deleted member 103

Guest
Hi Flippo

I orginally had good success with you suggestion, but I recently recieved a new TinySine that works well with the Androids, but I cannot write data out to it using B4i, I'm using the iBLE v1.2 and all I get on the LOG is
"Error writing value for characteristic: FFE1"

I can connect and read at least I think it is as it's identical to what you see in post #71

Really frustrating

Any ideas
Can it be that the characteristic no longer "FFE1" means?
 

fbritop

Active Member
Licensed User
Longtime User
I have succesfully connected to an iMac, then I call readData with service "108A"

After data is available, how can we transform a B4IArray value to a readable value?

Found: Aldo’s iMac, 37BA9875-6BCB-7778-78FC-153E5BD78F0A, RSSI = -83, (read only map) {
kCBAdvDataIsConnectable = 1;
}
BLEIDD:37BA9875-6BCB-7778-78FC-153E5BD78F0A
Discovering services
Services discovery completed.
BLE HAS BEEN CONNECTED
SERVICE AVAILABLE:<B4IList: (
180A,
"D0611E78-BBB4-4591-A5F8-487910AE4366"
)>
DATA AVAIL:180A
SERVICE CHAR:NSMapTable {
[9] Model Number String -> <B4IArray: 0x146f6dd0>
[13] Manufacturer Name String -> <B4IArray: 0x15f5c2d0>
}

Thanks
FBP
 

foakgul

Member
Licensed User
Longtime User
Hello,

The ID field in:

B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)

yields in my case:
Found: Kontakt, 9EAD3B2A-1F8A-8AFF-0D66-0E8DD5FCF0A6, RSSI = -50, (read only map) {

1) What does the ID (shown in bold) correspond to? It doesn't look like MAC or the UUID.

Also, I know that I cannot scan for UUIDs for ibeacons in ios, they must be specified to do that.

2) In the case of known UUID, how do I scan for major and minor?

3) Can ios scan for UUIDs of other beacons, ie Eddystone (non ibeacon)?
 

foakgul

Member
Licensed User
Longtime User
Thanks Erel.

However, the actual UUID for this device is completely different as scanned by an Android device and also as specified by the company, Kontakt.io.

How's this possible? So I was thinking this reported peripheral UUID is something different?
 

foakgul

Member
Licensed User
Longtime User
Hi Erel,

Advertising data is already being shown (blue text):

Application_Start
Application_Active
Found: Kontakt, 9EAD3B2A-1F8A-8AFF-0D66-0E8DD5FCF0A6, RSSI = -54, (read only map) {
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = Kontakt;
kCBAdvDataServiceData = {
D00D = <6e735779 33315d>;
};

kCBAdvDataTxPowerLevel = "-12";

Here, D00D above seems to correspond to the hardware manufacturer's instance ID below. Below data is directly related to the ibeacon per the manufacturer.
  • MAJOR: 27629
  • MINOR: 60590
  • NAMESPACE: f7826da6bc5b71e0893e
  • INSTANCEID: 6e735779696f
  • PROFILE:IBEACON
  • PROXIMITY UUID:f7826da6-4fa2-4e98-8024-bc5b71e0893e
  • TX POWER:3
  • INTERVAL:350 ms
Here, I cannot obtain the proximity UUID and major and minor values thru this library. Am I missing something?

Thanks!
 

foakgul

Member
Licensed User
Longtime User
Quite useful! Thanks Erel.

It seems B4i is using CoreBluetooth as opposed to CoreLocation for these responses.

Is it possible to initiate CoreLocation with a specific UUID to find out the major and minor, ie

B4X:
manager.Scan("f7826da6-4fa2-4e98-8024-bc5b71e0893e")
 

Turbo3

Active Member
Licensed User
Longtime User
When I move far away from the BTLE device my app crashes with the following messages in the log window while in debug mode.
B4X:
Value written for characteristic: FFE1
Value written for characteristic: FFE1
Value written for characteristic: FFE1
_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15)
_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15)
SignalHandler 11

How can this be handled more gracefully?
 

Turbo3

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