Bluetooth Low Energy (BLE) library

Thuong

Member
Licensed User
Longtime User
What is the output of Log("Services = " & Services)?

Services = (MyMap) {00001800-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41da0c28, 00001801-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41da1208, 00002d0d-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41da17a0, 0000180a-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41da1d38}
 

Thuong

Member
Licensed User
Longtime User
When i used
Dim s As BleService = Services.GetValueAt(0)
Log("Services = " & Services)
'Read the manufacturer characteristic
Dim c As BleCharacteristic = s.GetCharacteristics.Get("00001800-0000-1000-8000-00805f9b34fb")
manager.ReadCharacteristic(c)

error in log
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
ECG, 6C:EC:EB:15:54:AA
ECG, 6C:EC:EB:15:54:AA
Discovering services.
Discovering services.
Services = (MyMap) {00001800-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41d73f60, 00001801-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41d74540, 00002d0d-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41d74ad8, 0000180a-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattService@41d75070}
Error occurred on line: 33 (main)
java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
at anywheresoftware.b4a.objects.BleManager.ReadCharacteristic(BleManager.java:93)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
 

Thuong

Member
Licensed User
Longtime User
Service found, but can not readCharacteristic
Error on line 33 :manager.ReadCharacteristic(c)

My code focus to read characteristic at 00002d37
service of it at 00002d0d

Erel help me
 

Thuong

Member
Licensed User
Longtime User
Sub BLE_Connected (Services As Map)
cs.Initialize
Dim s As BleService = Services.GetValueAt(2)
Dim c As BleCharacteristic
c = s.GetCharacteristics.Get("00002d37-0000-1000-8000-00805f9b34fb")
manager.ReadCharacteristic(c)
Log(s.GetCharacteristics)

End Sub

Log:
** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
ECG, 6C:EC:EB:15:54:AA
ECG, 6C:EC:EB:15:54:AA
Discovering services.
Discovering services.
(MyMap) {00002d37-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d846a8, 00002d38-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d850e8, 00002d39-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d865e0, 00002d3a-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d87a90}
(MyMap) {00002d37-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d846a8, 00002d38-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d850e8, 00002d39-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d865e0, 00002d3a-0000-1000-8000-00805f9b34fb=android.bluetooth.BluetoothGattCharacteristic@41d87a90}
 

Thuong

Member
Licensed User
Longtime User
but not goto (nothing on log)
Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)

If Success Then
Log("CR: " & Success & ": " & Characteristic.Uuid)
Log("cr: " & Characteristic.GetStringValue(0))

End If
End Sub
 

Thuong

Member
Licensed User
Longtime User
I write code
Sub BLE_Connected (Services As Map)
cs.Initialize
Dim s As BleService = Services.GetValueAt(2)
Dim c As BleCharacteristic
c = s.GetCharacteristics.Get("00002d37-0000-1000-8000-00805f9b34fb")
manager.ReadCharacteristic(c)
Log(s.GetCharacteristics)

End Sub
Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)

If Success Then
Log("CR: " & Success & ": " & Characteristic.Uuid)
Log("cr: " & Characteristic.GetStringValue(0))

End If
End Sub

1/ Log(s.GetCharacteristics) is Ok
2/ sub BLE_CharacteristicRead nothing, no Log("CR: " & Success & ": " & Characteristic.Uuid)
 

Thuong

Member
Licensed User
Longtime User
Call it before you call manager.ReadCharacteristic (which throws an error if I understand correctly).
Dim c As BleCharacteristic
c = s.GetCharacteristics.Get("00002d37-0000-1000-8000-00805f9b34fb")
Log(s.GetCharacteristics)
manager.ReadCharacteristic(c)

and log is ok
 

Thuong

Member
Licensed User
Longtime User
I think error on manager.ReadCharacteristic(c) therefore sub BLE_CharacteristicRead is not run
 

Thuong

Member
Licensed User
Longtime User
I found my error: properties of characteristic at 0x2d37 is NOTIFY
can not read direct
This is correct Erel?
 

Thuong

Member
Licensed User
Longtime User
hmm, still. The characteristics property type are of type [notify]. I can't read them (that's why it's returning false).
Dear merlin2049er
you are ready to read characteristic with property notify?
I have a problem same you, can't read
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
oh, I haven't looked at it in a while. But that was the case when I had been working on it several months ago.
 

walterf25

Expert
Licensed User
Longtime User
Android 4.3 adds support for Bluetooth Low Energy. This library allows you to access the new API. It allows you to search for BLE devices, connect to a device and read its services and characteristics.

Writing is currently not supported though it should be simple to add this feature.

From my testings BLE is not yet ready for production :(
It is unreliable. I also tested it with Google example app and the results were the same.

Seems like others have encountered such issues as well:
http://code.google.com/p/android/issues/detail?id=58381
http://stackoverflow.com/questions/17870189/android-4-3-bluetooth-low-energy-unstable


In order to use this library you will need to download platform level 18 from Android SDK and configure the IDE (Tools - Configure Paths) to use it.

Simple code that starts searching when you click on the activity:
B4X:
Sub Process_Globals
   Dim manager As BleManager
   Private cs As List
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     manager.Initialize("ble")
     Log("Supported: " & manager.BleSupported)
   End If
End Sub
Sub Activity_Click
   Log("Searching for devices")
   manager.Scan(10000, Null)
End Sub
Sub ble_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ", " & MacAddress)
   manager.Connect(MacAddress, True)
End Sub

Sub BLE_Connected (Services As Map)
   cs.Initialize
   Dim s As BleService = Services.GetValueAt(0)
'Read the manufacturer characteristic
   Dim c As BleCharacteristic = s.GetCharacteristics.Get("00002a00-0000-1000-8000-00805f9b34fb")
   manager.ReadCharacteristic(c)
End Sub

Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)
   Log("CR: " & Success & ": " & Characteristic.Uuid)
   If Success Then Log("cr: " & Characteristic.GetStringValue(0)) 
End Sub
Sub ble_Disconnected
   Log("Disconnected")
End Sub

Sub ble_DiscoveryFinished
   Log("DiscoveryFinished")
End Sub
Hi Erel, i just started playing around with Bluetooth BLE, and i'm having some issues trying to read some of the characteristics on this device I have. The device is a Glucose Monitor, the Model is GL50 Evo and is manufactured by Beurer Medical.

I followed your example on the first post, and when i run it it works fine, i can see the model and the mac address of the device, unfortunately when I try to read any of the following characteristics i get the following error.

** Activity (main) Create, isFirst = true **
Supported: true
** Activity (main) Resume **
Searching for devices
Beurer GL50EVO, F4:04:4C:0C:D6:58
Discovering services.
services map size: 5
00001800-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattService@3bcefae6
00001801-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattService@3b771827
0000180a-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattService@303b5ad4
00001808-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattService@32ea747d
0000fff0-0000-1000-8000-00805f9b34fb android.bluetooth.BluetoothGattService@32ffdf72
s: (BluetoothGattService) android.bluetooth.BluetoothGattService@3bcefae6
main_timer1_tick (B4A line: 109)
manager.ReadCharacteristic(c)
java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.BleManager.ReadCharacteristic(BleManager.java:142)
at com.genesis.evo.main._timer1_tick(main.java:608)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
java.lang.RuntimeException: Object should first be initialized (BleCharacteristic).

I read the on another thread where the BLE Extended version of this library was posted, that someone else was seeing the same problem, that person added a delay before this line:
B4X:
manager.ReadCharacteristic(c)
and he claims that it worked fine after that, however i have tried doing the same thing but i still get the same error, do you have any idea what may be going on?

This is my code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   Dim manager As BleManager
   Private cs As List
   Dim timer1 As Timer
   Dim map1 As Map
    Dim c As BleCharacteristic
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    If FirstTime Then
     manager.Initialize("ble")
     Log("Supported: " & manager.BleSupported)
   End If
  
   timer1.Initialize("timer1", 1000)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_Click
   Log("Searching for devices")
   manager.Scan(10000, Null)
End Sub
Sub ble_DeviceFound (Name As String, MacAddress As String)
   Log(Name & ", " & MacAddress)
   manager.Connect(MacAddress, True)
End Sub

Sub BLE_Connected (Services As Map)
   cs.Initialize
   Log("services map size: " & Services.Size)
  
   map1 = Services
  
   For i = 0 To Services.Size - 1
       Log(Services.GetKeyAt(i) & " " & Services.GetValueAt(i))
   Next
  
   Dim s As BleService = Services.GetValueAt(0)

'get the glucose readings
  c = s.GetCharacteristics.Get("00001800-0000-1000-8000-00805f9b34fb")
   timer1.Enabled = True 
End Sub

Sub BLE_CharacteristicRead (Success As Boolean, Characteristic As BleCharacteristic)
   Log("CR: " & Success & ": " & Characteristic.Uuid)
   If Success Then
   Log("cr0: " & Characteristic.GetStringValue(0)) 
   End If
End Sub
Sub ble_Disconnected
   Log("Disconnected")
End Sub

Sub ble_DiscoveryFinished
   Log("DiscoveryFinished")
End Sub

Sub timer1_Tick
   manager.ReadCharacteristic(c)
   timer1.Enabled = False
End Sub

Any help of tips will be greatly appreciated.

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Top