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

EApreda

Member
Licensed User
Longtime User
Hi Erel
I have a problem: My application on a ble device sends an Advertisement Packet with few bites of payload. Now i need to read this payload on my smartphone, whithout making connection.
I have tried to read the advertising map value as your example, but i received only strange numbers:

Found: Adafruit Bluefruit LE, F8:61:A6:3D:39:AD, RSSI = -59, (MyMap) {75=[B@41cb8ed8, 9=[B@41cb8f48}

Can you help me please?
thanks
Enrico
 

EApreda

Member
Licensed User
Longtime User
Looks good. You received a map with two items:
B4X:
Dim data1() As Byte = AdvertisingData.Get(75)
Dim data2() As Byte = AdvertisingData.Get(9)
'you can use byte converter to convert to hex if you like:
Dim bc As ByteConverter
Log(bc.HexFromBytes(data1))
Log(bc.HexFromBytes(data2))

Ok it works perfectly!!!
My data are in:
AdvertisingData.Get(75)

thank you very much
 

EApreda

Member
Licensed User
Longtime User
Hi Erel
I have a problem. I need to read data sent from a ble by uart. Using old bleextended library, it works perfectly, but using ble2 when calling ReadData i have: No matching characteristic found. Can you help me? thanks
 

Turbo3

Active Member
Licensed User
Longtime User
Why does the information displayed by the B4A BLE_Example code look nothing like the information displayed by the B4i BLE_Example code for the exact same BLE device?

The information read from the device should be the same, right?

For example the first Services shown on the B4i app is "180A" but on the B4A app it is "00001800-0000-1000-8000-00805f9b34fb".

The B4i service of 180A is a valid service but the one by B4A is not.

The B4i version shows 2 services while the B4A version shows 3 services.

==============================================

Just ran a test with another Android BLE app and get the same strange (i.e. not matching what I get on iOS with B4i) results. So it seems the data you get depends on the operating system and not totally on the device.
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
Now that I have my app working with BLE2 is there any way to stop getting the "Writing data to: ..." message to the log every time I write some data? It is going to make it very hard to see any message I write to debug a problem if I get hundreds of these messages filling the log.

Writing data to: 0000ffe1-0000-1000-8000-00805f9b34fb, success = true

There are also a large number of these messages too at the start.

Ignoring characteristic: 00002a01-0000-1000-8000-00805f9b34fb

Basically can we get rid of all these library debug messages now that the library is released?
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
Erel, Thanks, much better only the single "Discovering services." message left.
 

stari

Active Member
Licensed User
Longtime User
OK, i trayed but program searches for devices and search is never ended. In log i have:
Installing file.
PackageAdded: package:BluetoothExample.com
Copying updated assets files (1)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Class not found: b4a.example.customlistview, trying: BluetoothExample.com.customlistview
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
 

Turbo3

Active Member
Licensed User
Longtime User
Can you add the features from iBLE into BLE2? (read Characteristic properties, report more than one characteristic)

Ok, That's embarrassing. I did not read the above post.

(I am going to claim I was looking at a cached copy of the page that did not show the above post when I made mine.)
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
I don't see a WriteData with Response like we have in iBLE.

I have one adapter working but the other fails do to not getting any data back from the first write so seems to be a similar problem to the one you fixed in iBLE.
 
Last edited:
Status
Not open for further replies.
Top