Android Question BLE characteristic value is empty

osvaldl

Member
Licensed User
Longtime User
Hi,

I would like to help You.
I would like to read the temperature data from Blue Gecko (BGM111) and indicate the current in the program...but there is a problem.

For some reason,
ArrayBytes = Characteristics.Get("00002a1c-0000-1000-8000-00805f9b34fb")
returns a blank value (Main, row 102).

Furthermore, how can i automate the DATAAVAILABLE (Main, row 66) at every second?

Thanks!

(sorry for my bad English)

Laszlo
 

Attachments

  • B4A_BLE2_modded.zip
    12.5 KB · Views: 153

osvaldl

Member
Licensed User
Longtime User
Hello,

Yes, I can see it in the attached program. But unfortunately I can't read the temperature data.
 
Upvote 0

osvaldl

Member
Licensed User
Longtime User
The problem is that the

ArrayBytes = Characteristics.Get("00002a1c-0000-1000-8000-00805f9b34fb")

does not get value int the previously attached project:

Sub DataAvailable (Service As String, Characteristics As Map)
pbReadData.Visible = False
If Service = "00001809-0000-1000-8000-00805f9b34fb" Then 'Thermometer
For i = 0 To Characteristics.Size - 1
For Each id As String In Characteristics.Keys
If id = "00002a1c-0000-1000-8000-00805f9b34fb" Then
Private tem,hum As Int
Private temp1,temp2,hum1 As Float
Dim ArrayBytes() As Byte
Private Zaehler As Int
Private Twert As Int
Private Fwert As Int
Private BlueATemperatur As Int
Private BlueALuftfeuchtigkeit As Int

ArrayBytes = Characteristics.Get("00002a1c-0000-1000-8000-00805f9b34fb") '<- ARRAYBYTES is EMPTY!!!!!!!!

'Convert to Grad and Fahrenheit
tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff))
hum = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(3), 8), 0x0000ff00), Bit.And(ArrayBytes(2), 0x000000ff))

temp1 = (tem/65536)*165-40 ' = Grad
temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit

Log("Grad = " & NumberFormat(temp1, 1, 2))
Log("Fahrenheit = " & NumberFormat(temp2, 1, 2))
End If
Next
Next
End If
End Sub

Thanks Erel!
 
Upvote 0

osvaldl

Member
Licensed User
Longtime User
I'm sorry Erel...

B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    pbReadData.Visible = False
        For i = 0 To Characteristics.Size - 1
            For Each id As String In Characteristics.Keys
                If id = "00002a1c-0000-1000-8000-00805f9b34fb" Then
                    Private tem As Int
                    Private temp1,temp2 As Float
                    Dim ArrayBytes() As Byte
           
                    ArrayBytes = Characteristics.Get("00002a1c-0000-1000-8000-00805f9b34fb") '<- ArrayBytes is EMPTY!!!!
                   
                    tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff)) '<-ERROR
           
                    temp1 = (tem/65536)*165-40 ' = Grad
                    temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
     
                    Log("Grad = " & NumberFormat(temp1, 1, 2))
                    Log("Fahrenheit = " & NumberFormat(temp2, 1, 2))
                End If
            Next
        Next
End Sub

The whole thing is that the ARRAYBYTES in the code does not get a value...it's empty.

Thanks!
 
Upvote 0

osvaldl

Member
Licensed User
Longtime User
For example post: https://www.b4x.com/android/forum/t...acteristic-uuid-using-the-ble2-library.97797/

JordiCP's code:
B4X:
Sub xxx_DataAvailable (Service As String, Characteristics As Map)   'whatever xxx is
    ' Here we just log what is received
    For Each id As String In Characteristics.Keys
        Dim dataContent() as Byte = Characteristics.Get(id)

        ' Calculate X_axis acceleration if the read characteristic (id) is the 0000ffa3-..., or Y_axis if 0000ffa4, ....
        if id="0000ffa3-......"  Then  '<--fill with correct values 
           Dim X_accel_scaled as Byte = dataContent(0)
           Dim X_accel_final as Float = X_accel_scaled *2*9.8/127     ' Scale conversion : convert [-128..+127] to [-2G..+2G]
           Log( "X_accel value is: "& X_accel_final )
        End if   

    Next
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If it works against the same device with the original BLE2 example (I mean, if you can see the byte content of that characteristic), it is very strange.
Does it only happen the first time after connecting or also when you press the ReadData button?
Just in case that the characteristic is not always filled but sometimes yes, you can make the code more robust by slightly modifying the sub (not tested)
B4X:
Sub DataAvailable (Service As String, Characteristics As Map)
    pbReadData.Visible = False
        'For i = 0 To Characteristics.Size - 1    '<---- this one is not needed!!!!!!
            For Each id As String In Characteristics.Keys
                If id = "00002a1c-0000-1000-8000-00805f9b34fb" Then
                    Private tem As Int
                    Private temp1,temp2 As Float
                    Dim ArrayBytes() As Byte = Characteristics.Get(id) '<- ArrayBytes is EMPTY!!!!
                    if ArrayBytes.Length>=2 Then
                       tem = Bit.Or(Bit.And(Bit.ShiftLeft(ArrayBytes(1), 8), 0x0000ff00), Bit.And(ArrayBytes(0), 0x000000ff)) '<-ERROR
           
                       temp1 = (tem/65536)*165-40 ' = Grad
                       temp2 = ((temp1*9.0)/5.0)+32.0 ' = Fahrenheit
     
                       Log("Grad = " & NumberFormat(temp1, 1, 2))
                       Log("Fahrenheit = " & NumberFormat(temp2, 1, 2))
                   Else
                      Log("Empty array")
                   End if 
               End If
            Next
        'Next
End Sub
 
Upvote 0

osvaldl

Member
Licensed User
Longtime User
Thanks!

I tried the code and it is very strange...because the characteristic value is always empty (also when I pressed the ReadData button)...so processing never runs on the "Then"-post code (tem,...).


(I'm sorry for my bad English)
 
Upvote 0

osvaldl

Member
Licensed User
Longtime User
Can BLE2 have any problem? If I could get the source code, then I could test it. We might face a new thing...I do not know.
Erel?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Can you confirm that you can see the byte array contents of that characteristic using the original BLE2 example or an external BLE app? (nRF connect, from Nordic Semiconductors, or others), and which is the array length?

If I'm not mistaken, the BGM111 is a programmable BLE module, not only a sensor that always reports a value in a characteristic. This can mean that its behavior will depend on the code loaded into it, and (for instance) perhaps a value must be previously written to some other characteristic in order to activate some functionalities.
 
Last edited:
Upvote 0

osvaldl

Member
Licensed User
Longtime User
Erel: No problem! JordiCP thinks understands my problem.
JordiCP: I've alredy created a software in Xamarin that has successfully read the temperature data (Silicon Labs BGM 111 BT module).
But BLE2 does not know...so I stay at Xamarin.
Thanks!
 
Upvote 0
Top