B4A Library [B4X] BLE 2 - Bluetooth Low Energy

Status
Not open for further replies.
This library replaces the previous BLE library.
It is simpler to use and it is more powerful. Its API is based on B4i iBLE library which makes it easy to reuse B4i code.

See the iBLE tutorial: https://www.b4x.com/android/forum/threads/ble-bluetooth-low-energy-library.46099/#content

Tips & Notes

- You can call Manager.Scan2 with AllowDuplicates set to True if you want to monitor the state of an unconnected device.
- The AdvertisingData map in DeviceFound event holds pairs of integers as keys (data types) and bytes arrays as values.
- Call ReadData2 if you are only interested in a single characteristic.
- BLE is only supported on Android 4.3+.

See the attached example. Note that the important code in in the Starter service module.

Edit:

A new version of BLE_Example was uploaded. targetSdkVersion is now set to 29
Setting the targetSdkVersion to 29 requires some changes:

1. Add the fine location permission in the manifest editor.
2. Request this permission with RuntimePermissions.

Otherwise scanning will fail with a message visible in the unfiltered logs.

BLE2 is an internal library now. It is included in the IDE.

Example is based on B4XPages and will work with B4A and B4i.

Updates:
- Example updated with targetSdkVersion = 31. Note the permissions in the manifest editor:
B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH_SCAN)
AddPermission(android.permission.BLUETOOTH_CONNECT)
And requesting at runtime:
B4X:
Dim Permissions As List
Dim phone As Phone
If phone.SdkVersion >= 31 Then
    Permissions = Array("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT", rp.PERMISSION_ACCESS_FINE_LOCATION)
Else
    Permissions = Array(rp.PERMISSION_ACCESS_FINE_LOCATION)
End If
For Each per As String In Permissions
    rp.CheckAndRequest(per)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then
        ToastMessageShow("No permission: " & Permission, True)
        Return
    End If
Next
 

Attachments

  • BLEExample.zip
    181.6 KB · Views: 1,885
Last edited:

KY Leng

Active Member
Licensed User
Longtime User
Hi,
i also work with HM-10 and i can send/receive data. On other side i have connected PIC P18F45K22 and also without any problem.
I'ts true, 24 bytes per frame is max.
Good to hear that you can do it.
However, could you please share some piece of code to read/send 24 byte data to PIC ?
Thank you in advance.
 

stari

Active Member
Licensed User
Longtime User
Good to hear that you can do it.
However, could you please share some piece of code to read/send 24 byte data to PIC ?
Thank you in advance.
Hi, yes i can. This is taken from another sample here on B4A communitty. I'ts only a working example, but i think i'ts Ok.
Example is for HM-10 module (BLE). At the end i send "*", because HM-10 don't send any terminating character.
 

Attachments

  • A_PIC.ZIP
    424.6 KB · Views: 911

KY Leng

Active Member
Licensed User
Longtime User
Hi, yes i can. This is taken from another sample here on B4A communitty. I'ts only a working example, but i think i'ts Ok.
Example is for HM-10 module (BLE). At the end i send "*", because HM-10 don't send any terminating character.
Thank you very much stari,
I will check it...
Best regards,
 

appie21

Active Member
Licensed User
Longtime User
Hello

is BLE2 same as Bluetooth 4.0?

Fir example if a headset support Bluetooth 4.0 I can work with this library
 

stari

Active Member
Licensed User
Longtime User
Hello

is BLE2 same as Bluetooth 4.0?

Fir example if a headset support Bluetooth 4.0 I can work with this library

The HM-10 is a readily available Bluetooth 4.0 module based on the Texas Instruments
CC2540 or CC2541 Bluetooth low energy (BLE) System on Chip (SoC).
I work with this module without problems.
 

appie21

Active Member
Licensed User
Longtime User
The HM-10 is a readily available Bluetooth 4.0 module based on the Texas Instruments
CC2540 or CC2541 Bluetooth low energy (BLE) System on Chip (SoC).
I work with this module without problems.

Hij

Thank you

I looking for a good headset
 

postasat

Active Member
Licensed User
Longtime User
Hi,
is it possible to connect to two or more of the same type of devices and read (with SetNotify) all of data and after that, assigning every data received at the right devices ?
(How to know who is sending the data)

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

ncabilis

Member
Licensed User
Longtime User
Hello.
Is it possible to get rssi value after bluetooth ble connected?
Thanks
 
Last edited:

ncabilis

Member
Licensed User
Longtime User
Thanks for your answer. I found a similar solution in BleExt library using the Manager.StartRssiTracking routine.
 

mczubel

Member
Licensed User
Longtime User
Good to hear that you can do it.
However, could you please share some piece of code to read/send 24 byte data to PIC ?
Thank you in advance.

Hi
I need to send AT commands to HM-10, this will be used as beacon and need write and rewrite
AT+IBE094900001

AT+IBE100374360
It is possible with ble2 or BLEext?
1000 thanks
 

ncabilis

Member
Licensed User
Longtime User
Hi
I need to send AT commands to HM-10, this will be used as beacon and need write and rewrite
AT+IBE094900001

AT+IBE100374360
It is possible with ble2 or BLEext?
1000 thanks

Assuming that you are using Arduino: From B4A, using BLEEXT, you can send a string to Arduino HM-10 BT.
Sub Button1_Click
Dim HMString As String
HMString = "1" & CRLF ' 20 characters max
If ConnectState = cstateConnect Then
Log("Write -> " & HMString)
writeCharacteristic.SetStringValue(HMString)
Manager.WriteCharacteristic(writeCharacteristic)
End If
End Sub

Now, on the Arduino side, you can read this character and to send the command: bluetooth.write('AT+IBE094900001');

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(5, 6); // RX, TX
int info;
void setup()
{
Serial.begin(9600);
bluetooth.begin(9600);
}

void loop()
{
if (bluetooth.available()>0)
{
info = bluetooth.read();
if (info=='1') //READ STRING FROM B4A
{
bluetooth.write('AT+IBE094900001');
}

} //bt available
} //loop

If you have trouble, I can send you a general code in both languages (Arduino C, B4A)
 
Last edited:

mczubel

Member
Licensed User
Longtime User
Assuming that you are using Arduino: From B4A, using BLEEXT, you can send a string to Arduino HM-10 BT.
Sub Button1_Click
Dim HMString As String
HMString = "1" & CRLF ' 20 characters max
If ConnectState = cstateConnect Then
Log("Write -> " & HMString)
writeCharacteristic.SetStringValue(HMString)
Manager.WriteCharacteristic(writeCharacteristic)
End If
End Sub

Now, on the Arduino side, you can read this character and to send the command: bluetooth.write('AT+IBE094900001');

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(5, 6); // RX, TX
int info;
void setup()
{
Serial.begin(9600);
bluetooth.begin(9600);
}

void loop()
{
if (bluetooth.available()>0)
{
info = bluetooth.read();
if (info=='1') //READ STRING FROM B4A
{
bluetooth.write('AT+IBE094900001');
}

} //bt available
} //loop

If you have trouble, I can send you a general code in both languages (Arduino C, B4A)


yes, is arduino, next week hardware will be ready and I'll test it.
I'll let you know how it works
sorry my eng and
1000 thanks again!!!
 

coslad

Well-Known Member
Licensed User
Longtime User
Hi,

is it possible to connect two smartphone with BLE ?
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Assuming that you are using Arduino: From B4A, using BLEEXT, you can send a string to Arduino HM-10 BT.
Sub Button1_Click
Dim HMString As String
HMString = "1" & CRLF ' 20 characters max
If ConnectState = cstateConnect Then
Log("Write -> " & HMString)
writeCharacteristic.SetStringValue(HMString)
Manager.WriteCharacteristic(writeCharacteristic)
End If
End Sub

Hi,

I am as well using BLE ext to write to the HM-10 and it works.

HOwever I would like to use only BLE2 to do the same thing.

How could we do to replace these lines to write to the HM10 with BLE2 ?

B4X:
  writeCharacteristic.SetStringValue(HMString)
        Manager.WriteCharacteristic(writeCharacteristic)
 
Status
Not open for further replies.
Top