B4A Library BLE 2 - Bluetooth Low Energy

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
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: http://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.
 

Attachments

Last edited:

EApreda

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

AndOrNot

Well-Known Member
Licensed User
Great job! Can i read files that are storage on device, or read messages that are sended by arduino?
 

AndOrNot

Well-Known Member
Licensed User
You can read any data that is sent over the BLE connection.
How i can this? I connect correctly the device and in the example when i click on read data i can read the specific of the device which is connected, but i don't understand how read messages that are send by connected device.. Can you help me?
 

AndOrNot

Well-Known Member
Licensed User
You will only be able read data that is being sent from the device. Which device are you asking about?
I use an s6 edge to connect with an Arduino nano with Bluetooth service. I ve configured Arduino to send data (it sends example text). With android studio I ve connected the device and I can read example text that is sended by Arduino, but I don't know how to intercept this text with b4a. I only intercept the Arduino Bluetooth characteristics
 

EApreda

Member
Licensed 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
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
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:

stari

Active Member
Licensed 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 **
 
Status
Not open for further replies.
Top