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

Deleted member 103

Guest
Disable the writing code for now. Is the reading code works properly?
Yes, the reading works properly.
That you can see the logfile, the errors come from the write.
 

Intek

Member
Licensed User
Longtime User
Hi,
is it possible to have the same methods that I can found in the ble extended library in B4A?
I would need to be able to read a single characteristic id or activate notify.

Thanks anyway
 
D

Deleted member 103

Guest
Hi Erel,

the writing of B4i works. :)

The writing of arduino but not yet.
The Sub "void read" is ok, the SUB "void write" not. With "Serial2.print (7)" comes in B4i nothing.

Here is my Arduino code:
B4X:
void write() {
  if (button0.fallingEdge()) {
    Serial2.print(7);
  }
  if (button1.fallingEdge()) {
    Serial2.print(6);
  }
}

void read() {
  if (Serial2.available()) {
    int c = Serial2.read();

    if( c == 1 )               // if 'H' was received
    {
      digitalWrite(ledpin, HIGH);  // turn ON the LED
      millisec = 0;
    }
  }
}
 

Intek

Member
Licensed User
Longtime User
Hi Erel,
sorry if I'm boring but I have a device that I connect and it works perfectly but I have a service with a lot characteristics id, and now I can only read them all together and so i have a delay of 8-9 seconds to complete the reading of service to update the data.

I'd just like to know if it will be extended library IBLE or is it possibly personalize the library ible?
 

Ph1lJ

Member
Licensed User
Longtime User
Errors.jpg Errors2.jpg Hi

I've been trying create a B4i app to send/receive text from a TinySine BLE Module, everything works well with B4A, but I cannot find the appropriate service (at least I think thats the issue. I know the device can be used in this way with the iOS, as I can do exactly what I want with an app called LightBlue. I've tried both FFE0 & FFE1 as the service

Has anyone an idea - this seems to be a hot topic judging by the number of viewers :-
 
D

Deleted member 103

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

Deleted member 103

Guest
So I can send from my Arduino everything I want, it comes with B4i just not on. :(
The receiver works, I get all the bytes I have sent.
 
D

Deleted member 103

Guest
Is it normal that I get only this info from "TinySine BLE modules"?
ble_1.png
 

juvanum

Active Member
Licensed User
Longtime User
Hello,
with this library I could detect tags ibeacons?
thank you
 
D

Deleted member 103

Guest
Hi Erel,

these Library without notifications is not useful for me. :(
When this function is added?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
iBLE v1.20 BETA is attached.
Two new methods were added:

ReadData2 (Service As String, Characteristic As String)
- Similar to ReadData. However it only reads the value of a single characteristic. The DataAvailable event will be raised when the data of this characteristic is available. Note that the Map returned in the event will include the old values of the other characteristics as well.

SetNotify (Service As String, Characteristic As String, Notify As Boolean) - Registers (or removes) a notification listener for the given characteristic. The DataAvailable event will be raised when the data changes.

You must call ReadData once and wait for DataAvailable event before you can call these two methods (the same is true for WriteData).

This is a beta version.
 

Attachments

  • iBLE.zip
    35.5 KB · Views: 319
Last edited:

Intek

Member
Licensed User
Longtime User
iBLE v1.20 BETA is attached.
Two new methods were added:

ReadData2 (Service As String, Characteristic As String)
- Similar to ReadData. However it only reads the value of a single characteristic. The DataAvailable event will be raised when the data of this characteristic is available. Note that the Map returned in the event will include the old values of the other characteristics as well.

SetNotify (Service As String, Characteristic As String, Notify As Boolean) - Registers (or removes) a notification listener for the given characteristic. The DataAvailable event will be raised when the data changes.

You must call ReadData once and wait for DataAvailable event before you can call these two methods (the same is true for WriteData).

This is a beta version.


Thank you very much Erel,
works fine for me.
 
Top