Android Question Get Values from device using BLE

Discussion in 'Android Questions' started by aaronk, Aug 29, 2015.

Similar threads

B4A Library BLE 2 - Bluetooth Low Energy
B4A Library BLE Peripheral
B4A Question BLE2 NOTIFICATIONS NOT ACTIVATED
B4A Tutorial BLE - Heart Rate Monitor
B4A Question BLE Interpretation
  1. aaronk

    aaronk Well-Known Member Licensed 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..

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

    Code:
    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)

    Code:
    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:
    Code:
    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 ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Based on the code and logs this characteristic belongs to a different service. Add Log(s) to the first code snippet you posted.
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    I used the following code:

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

    Code:
    (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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Change the code to:
    Code:
    For Each s As BleService In Services.Values
         
    Log($"** Reading service: ${s.Uuid} **"$)
        
    For Each key As String In s.GetCharacteristics.Keys
         
    'Log(key)
          Dim c As BleCharacteristic = s.GetCharacteristics.Get(key)
          
    Log($"Reading char: ${key}"$)
          manager.ReadCharacteristic(c)
        
    Next
       
    Next
    End Sub
     
  5. aaronk

    aaronk Well-Known Member Licensed User

    That logs:

    Code:
    ** Reading service00001800-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 
    service00001801-0000-1000-8000-00805f9b34fb **
    ** Reading 
    service00001804-0000-1000-8000-00805f9b34fb **
    Reading char: 00002a07-
    0000-1000-8000-00805f9b34fb
    ** Reading 
    service00001802-0000-1000-8000-00805f9b34fb **
    Reading char: 00002a06-
    0000-1000-8000-00805f9b34fb
    ** Reading 
    service00001803-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
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Now you can see that to read 00002a19-0000-1000-8000-00805f9b34fb you need to first get service: 0000180f-0000-1000-8000-00805f9b34fb
     
  7. aaronk

    aaronk Well-Known Member Licensed 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?

    Code:
    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: Aug 30, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    You are taking the first service from the services map.

    You should instead take the correct service:
    Code:
    Dim s As BleService = Services.Get("0000180f-0000-1000-8000-00805f9b34fb")
     
  9. aaronk

    aaronk Well-Known Member Licensed 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)

    Code:
    CR: true: 00002a19-0000-1000-8000-00805f9b34fb
    cr: d
    Code:
    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: Aug 31, 2015
    Daniel-White likes this.
  10. aaronk

    aaronk Well-Known Member Licensed 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!
     
    Daniel-White likes this.
  11. f0raster0

    f0raster0 Active Member Licensed 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
     
  12. aaronk

    aaronk Well-Known Member Licensed User

    I believe it should almost be the same code as what you do in B4A. I haven't tried in B4i yet.
     
    f0raster0 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice