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,353
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
The device that works report 0x1A for the property. The one that hangs waiting for a response reports 0x3A for the property. Could that make a difference?

Both these devices work fine with iBLE 1.31.
 

Turbo3

Active Member
Licensed User
Longtime User
Basically the same line of code for both B4i and B4A.
B4X:
B4i: manager.WriteDataWithResponse(CurService,CurCharWrite,ATCmdOut.GetBytes("UTF8"))
B4A: Starter.manager.WriteData(Starter.CurService,Starter.CurCharWrite,ATCmdOut.GetBytes("UTF8"))

Could you add the WriteDataWithResponse to B4A just to see if forcing it makes a difference?
 

Turbo3

Active Member
Licensed User
Longtime User
Erel, Can you please add two additional WriteData commands, one WithResponse and another WithoutResponse. That way I can test and see if either of those writes work. If not then the problem is somewhere else.
 

KY Leng

Member
Licensed User
Longtime User
Good evening Erel,

In the previous version of BleExtEx (version: 1.10), we can read/write data (serial data from/to BLE device) by using

'Read:
Sub Manager_CharacteristicChanged (Characteristic As BleCharacteristic)
Dim StrIn As String
StrIn = Characteristic.GetStringValue(0)
Log(" RxD <" & StrIn & ">")
End Sub

'Wite:
cwrite.SetStringValue(StrOutBT)
Manager.WriteCharacteristic(cwrite)

With this new BLE2 (version: 1.10), could you please tell us how to do it?
Note: The reason I move from BleExtEx to BLE2 is that BleExtEx could not allow Nexus Android Version 6.0.1 to find BLE device.
And it seem that all Android Version 6.0.1 could not find BLE device with EleExtEx library.

Looking forward to hearing from you soon...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
@KY Leng see this tutorial: https://www.b4x.com/android/forum/threads/ble-bluetooth-low-energy-library.46099/#content
For further help please start a new thread.


@Turbo3 Replace the existing jar with the one attached (copy it to the internal libraries folder).
It allows getting the characteristics with JavaObject:
B4X:
Dim jo As JavaObject = BleManager1
Dim ser As JavaObject = jo.RunMethod("getService", Array(ServiceId))
Dim chrc As JavaObject = jo.RunMethod("getChar", Array (ser, CharacteristicId))
chrc.RunMethod("setWriteType", Array (2)) '2 = with response, 1 = no response
'... now  call the regular Write method.
 

Attachments

  • BLE2.jar
    11.6 KB · Views: 567

Turbo3

Active Member
Licensed User
Longtime User
By "internal libraries folder" I assume you mean the Libraries folder under Basic4android where the current BLE2.jar was located.

With the new BLE2.jar JavaObject is an unknown type.

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

Never mind. I needed to enable the JavaObject library.

I will test in the morning.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
By "internal libraries folder" I assume you mean the Libraries folder under Basic4android where the current BLE2.jar was located.
and where the core.jar are located, yes.

The INTERNAL is the Libraries folder in b4a program path
The Additional Libraries folder is the one you setup in b4a Paths.
 

Turbo3

Active Member
Licensed User
Longtime User
No luck. I can see the adapter light flash indicating it has received the command from the WriteData but I never get a response back.

If I switch back to the adapter that works it continues to work for both the 1 and 2 settings for SetWriteType. I expected it to fail with the 1 settings of no response. So that makes me think the 1 settings is not actually working.

Can you add a debug message when you execute the WriteData that says which type it is actually using?
 
Last edited:

Turbo3

Active Member
Licensed User
Longtime User
Then just a log message that the WriteType has been changed by this new "SetWriteType".

What else could be stopping the response from one adapter and not the other? Both adapters work fine in B4i.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Then just a log message that the WriteType has been changed by this new "SetWriteType".
B4X:
log(chrc.RunMethod("getWriteType", Null))


What else could be stopping the response from one adapter and not the other? Both adapters work fine in B4i.
Hard to say. The first step is to try to check it with a different Android device.
 

Turbo3

Active Member
Licensed User
Longtime User
Just checked with two different Android devices. Same problem. One running 4.4.2 the other Fire OS 4.5.5

One adapter (LELink) works for both Type 1 or Type 2.

The other (Vgate iCar 3) flashes when sent a command but I never get any data back. Both a Type 1 or 2 setting fails.

So the SetType makes no difference on Android but it does on iOS.
 
Last edited:

KY Leng

Member
Licensed User
Longtime User
Good evening Erel,

In the previous version of BleExtEx (version: 1.10), we can read/write data (serial data from/to BLE device) by using

'Read:
Sub Manager_CharacteristicChanged (Characteristic As BleCharacteristic)
Dim StrIn As String
StrIn = Characteristic.GetStringValue(0)
Log(" RxD <" & StrIn & ">")
End Sub

'Wite:
cwrite.SetStringValue(StrOutBT)
Manager.WriteCharacteristic(cwrite)

With this new BLE2 (version: 1.10), could you please tell us how to do it?
Note: The reason I move from BleExtEx to BLE2 is that BleExtEx could not allow Nexus Android Version 6.0.1 to find BLE device.
And it seem that all Android Version 6.0.1 could not find BLE device with EleExtEx library.

Looking forward to hearing from you soon...

Now I can solve my problem by using BLE2 instead of BluetoothAdmin to scan BLE device. And use BleExtEx to send/read data to/from BLE device (HM-10). Data that I mentioned here is the same serial data that we send using Serial library with Bluetooth 2 device (HC-05).

However, I want to know if BLE2 can do the same (send/read data to/from HM-10 with TxD/RxD pin) with BLE device as Serial do with BT2 device or not ? With BleExtEx, we can do but with limited byte per frame (maybe 20 byte max).
 
Last edited:

stari

Active Member
Licensed User
Longtime User
Now I can solve my problem by using BLE2 instead of BluetoothAdmin to scan BLE device. And use BleExtEx to send/read data to/from BLE device (HM-10). Data that I mentioned here is the same serial data that we send using Serial library with Bluetooth 2 device (HC-05).

However, I want to know if BLE2 can do the same (send/read data to/from HM-10 with TxD/RxD pin) with BLE device as Serial do with BT2 device or not ? With BleExtEx, we can do but with limited byte per frame (maybe 20 byte max).
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.
 
Status
Not open for further replies.
Top