Android Question Get Values from device using BLE

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the Bluetooth Low Energy (BLE) library and trying to read values from the device I am connected to.

I have been able to connect to the device and was able to read the devices name but can't work out how to read the rest of the values.

When I connected to the device I have made it log each key like..

B4X:
Sub BLE_Connected (Services As Map)
   For Each s As BleService In Services.Values
    For Each key As String In s.GetCharacteristics.Keys
     Log(key)
      Dim c As BleCharacteristic = s.GetCharacteristics.Get(key)
      manager.ReadCharacteristic(c)
    Next
   Next
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

It then logs the following in the IDE:

00002a00-0000-1000-8000-00805f9b34fb
00002a01-0000-1000-8000-00805f9b34fb
00002a04-0000-1000-8000-00805f9b34fb
00002a07-0000-1000-8000-00805f9b34fb
00002a06-0000-1000-8000-00805f9b34fb
00002a06-0000-1000-8000-00805f9b34fb
00002a19-0000-1000-8000-00805f9b34fb
CR: true: 00002a00-0000-1000-8000-00805f9b34fb
cr: Alert_Device

I then changed the connected Sub to be:

B4X:
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

and it now logs the following:

CR: true: 00002a00-0000-1000-8000-00805f9b34fb
cr: Alert_Device

I then changed the connected sub to be:
(this should get the battery status)

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

However when I run the app, it now logs the following in the IDE:
B4X:
bluetoothservice_ble_connected (B4A line: 63)
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 b4a.example.bluetoothservice._ble_connected(bluetoothservice.java:170)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA$3.run(BA.java:334)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I can't seem to get the other values.

If I use the 'nRF Master Control' app (the app that come with the SDK board I am using) then it allows me to get this value from the device.

From what I understand it should get the battery status.

upload_2015-8-29_20-39-46.png


I can't work out how to get the values. Have I done something wrong ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
I used the following code:

B4X:
Sub BLE_Connected (Services As Map)
   For Each s As BleService In Services.Values
    For Each key As String In s.GetCharacteristics.Keys
     'Log(key)
     Log(s)
      Dim c As BleCharacteristic = s.GetCharacteristics.Get(key)
      manager.ReadCharacteristic(c)
    Next
   Next
End Sub

and It Logs:

B4X:
(BluetoothGattService) android.bluetooth.BluetoothGattService@7984dac
(BluetoothGattService) android.bluetooth.BluetoothGattService@7984dac
(BluetoothGattService) android.bluetooth.BluetoothGattService@7984dac
(BluetoothGattService) android.bluetooth.BluetoothGattService@28baa75
(BluetoothGattService) android.bluetooth.BluetoothGattService@ee56a0a
(BluetoothGattService) android.bluetooth.BluetoothGattService@bb9627b
(BluetoothGattService) android.bluetooth.BluetoothGattService@129898
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
That logs:

B4X:
** Reading service: 00001800-0000-1000-8000-00805f9b34fb **
Reading char: 00002a00-0000-1000-8000-00805f9b34fb
Reading char: 00002a01-0000-1000-8000-00805f9b34fb
Reading char: 00002a04-0000-1000-8000-00805f9b34fb
** Reading service: 00001801-0000-1000-8000-00805f9b34fb **
** Reading service: 00001804-0000-1000-8000-00805f9b34fb **
Reading char: 00002a07-0000-1000-8000-00805f9b34fb
** Reading service: 00001802-0000-1000-8000-00805f9b34fb **
Reading char: 00002a06-0000-1000-8000-00805f9b34fb
** Reading service: 00001803-0000-1000-8000-00805f9b34fb **
Reading char: 00002a06-0000-1000-8000-00805f9b34fb
** Reading service: 0000180f-0000-1000-8000-00805f9b34fb **
Reading char: 00002a19-0000-1000-8000-00805f9b34fb
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Are you able to give me a example.. This is the first time doing this.

I tried doing the following but not sure if this is what you mean?

B4X:
Sub BLE_Connected (Services As Map)
   Dim s As BleService = Services.GetValueAt(0)
'Read the battery staus
   Dim c As BleCharacteristic = s.GetCharacteristics.Get("0000180f-0000-1000-8000-00805f9b34fb")
   manager.ReadCharacteristic(c)
End Sub
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Ok, just tried that and it now is returning the string as d shouldn't it return 64? (64HEX to DEC = 100)

d(HEX) = 13DEC which doesn't sound right..
(sorry about asking so many questions, I am only new at using BLE and need help in working it out)

B4X:
CR: true: 00002a19-0000-1000-8000-00805f9b34fb
cr: d

B4X:
Sub BLE_Connected (Services As Map)

Dim s As BleService = Services.Get("0000180f-0000-1000-8000-00805f9b34fb")
Dim c As BleCharacteristic = s.GetCharacteristics.Get("00002a19-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
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Worked it out.

I had to convert Characteristic.GetValue to HEX and then convert the HEX to a DEC and now it's displaying 100 which is what I wanted.

Thanks for you help!
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi aaronk.. have you tried to do the same with B4i? I can't work out how to read the values of the rest of sensors like battery level and/or heart rate measurement
here
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Hi aaronk.. have you tried to do the same with B4i? I can't work out how to read the values of the rest of sensors like battery level and/or heart rate measurement
here
I believe it should almost be the same code as what you do in B4A. I haven't tried in B4i yet.
 
Upvote 0
Top