Android Question BLE2 activate DataAvailable routine

Discussion in 'Android Questions' started by Marco Maria Vilucchi, Jul 1, 2019.

  1. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    Hi all,
    after connecting to a chip via ble2, I'm trying to talk to him. In the first instance I printed all the characteristics of the service.
    Then I wrote a command: "E".
    Now I am trying to get an answer, but I have some difficulties.
    To activate the DataAvailable routine I need to set:
    Code:
    manager.SetNotify(ServiceId, C301_risposta, True)
    It's true?
    previously I have defined the characterisctic:
    Code:
    C301_risposta= UUID("C301")
    UUID is the Erel routine that construct UUID.
    The characteristic exists.

    But SetNotify has an error:
    Code:
    starter_manager_connected (java line: 470)
    java.lang.NullPointerException: Attempt 
    to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
        at anywheresoftware.b4a.objects.BleManager2.setNotify(BleManager2.java:322)
        at anywheresoftware.b4a.objects.BleManager2.SetNotify(BleManager2.java:
    311)
        at com.mamavi.slimup.starter._manager_connected(starter.java:
    470)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    196)
        at anywheresoftware.b4a.BA$
    2.run(BA.java:370)
        at android.os.Handler.handleCallback(Handler.java:
    790)
        at android.os.Handler.dispatchMessage(Handler.java:
    99)
        at android.os.Looper.loop(Looper.java:
    164)
        at android.app.ActivityThread.main(ActivityThread.java:
    7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:
    441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1408)
    So far I have always connected with the features of BleCentral (by Erel - ReadChar and WriteChar) and now I need support to use other characteristics.
    Now I have near 25 characteristics on the chip and I need to know how manage read, write and DataAvailable.
    Does anyone have any examples?
    Does anyone know why I get this error?
    I have B4A 9.01 and Ble2 1,38
    Thanks all
    Marcom
     
  2. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    Can someone help me?
     
  3. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    follow part of code.

    on STARTER module
    Code:
    Private Sub Manager_Connected (Services As List)
        
    Log("Connected")
        
    For Each S As String In Services
            
    Log("Service: " & S)
        
    Next

        
    For i=0 To Services.Size-1
            manager.ReadData(Services.Get(i))
        
    Next
        
    Log("Proprietà caratteristica 301 "&manager.GetCharacteristicProperties(ServiceId, C301_risposta))

    '    manager.SetNotify(ServiceId, ReadChar, True)
    '    manager.SetNotify(ServiceId, C301_risposta, True)             THIS LINE IS COMMENTED

        connected = 
    True
        
    CallSub2(Main, "SetState", connected)
        messagesToSend.Clear
    End Sub

    Private Sub Manager_DataAvailable (SId As String, Characteristics As Map)
        
    Log ("Manager_DataAvailable")

        
    Log(SId)
        
    For i = 0 To Characteristics.Size - 1
            
    Log("Key: " & Characteristics.GetKeyAt(i))
            
    Log("Val: " & Characteristics.GetValueAt(i))
            
    Dim BC As ByteConverter
            
    Log("Str: " & BC.StringFromBytes(Characteristics.GetValueAt(i),"utf-8"))
            
            
    Log("Main.tipomessaggiobluetooth " & Main.tipomessaggiobluetooth)
            
    Log("Characteristics.GetKeyAt(i) " & Characteristics.GetKeyAt(i))
            
    If Main.tipomessaggiobluetooth="E" Then
                
    If Characteristics.GetKeyAt(i)=C303_posizionebilancia Then
                    
    Dim BC As ByteConverter
                    MessaggioBluetooth= BC.StringFromBytes(Characteristics.GetValueAt(i),
    "utf-8")
                    LogColor(
    "Main.tipomessaggiobluetooth " & Main.tipomessaggiobluetooth &" Characteristics.GetKeyAt(i) " & Characteristics.GetKeyAt(i) & " MessaggioBluetooth " & MessaggioBluetooth,Colors.blue)
                
    End If
            
    End If   
        
    Next
        
    Log(" ")
        
    '    Dim b() As Byte = Characteristics.Get(C301_risposta)
    '    CallSub2(Main, "NewMessage", b)
    '    NewMessage(b)
    End Sub

    Public Sub SendCommand(msg() As Byte)
       LogColor(
    "Dentro SendCommand",Colors.red)
       messagesToSend.Add(TrimMessage(msg))
       
    If messagesToSend.Size = 1 Then
           
    Do While messagesToSend.Size > 0
               
    Try
                   manager.WriteData(ServiceId, C300_comando, messagesToSend.Get(
    0))
               
    Catch
                   FailedToSend
                   
    Return
               
    End Try
               
    Wait For Manager_WriteComplete (Characteristic As String, Status As Int)
               
    If Status <> 0 Then
                   FailedToSend
               
    End If
               
    If connected = False Or messagesToSend.Size = 0 Then Return
               messagesToSend.RemoveAt(
    0)
           
    Loop
       
    End If
    End Sub
    on another module:
    Code:
    Main.tipomessaggiobluetooth="E"
            Main.chichiededellapedana=
    "MisurazioniAltezza"
            
    Log("misaltezza Main.tipomessaggiobluetooth " & Main.tipomessaggiobluetooth)
    '        Dim s As String = "B4A: " & Main.tipomessaggiobluetooth
            Dim s As String = "E"
            
    CallSub2(Starter, "SendCommand", s.GetBytes("utf8"))
    In this way I see on RealTerm that I wrote "E", but I haven't response and DataAvailable doesn't start.

    upload_2019-7-2_10-30-57.png

    If I uncomment manager.SetNotify(ServiceId, C301_risposta, True)
    I have the previous error
    Code:
    starter_manager_connected (java line: 470)
    java.lang.NullPointerException: Attempt 
    to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
        at anywheresoftware.b4a.objects.BleManager2.setNotify(BleManager2.java:322)
        at anywheresoftware.b4a.objects.BleManager2.SetNotify(BleManager2.java:
    311)
        at com.mamavi.slimup.starter._manager_connected(starter.java:
    470)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    196)
        at anywheresoftware.b4a.BA$
    2.run(BA.java:370)
        at android.os.Handler.handleCallback(Handler.java:
    790)
        at android.os.Handler.dispatchMessage(Handler.java:
    99)
        at android.os.Looper.loop(Looper.java:
    164)
        at android.app.ActivityThread.main(ActivityThread.java:
    7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:
    441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    1408)
    How can I do to have a response from chip?
    Thanks all
    Marco
     
  4. emexes

    emexes Well-Known Member Licensed User

    I feel like this burst of read requests could be too much for the BLE device:
    Code:
    For i=0 To Services.Size-1
        manager.ReadData(Services.Get(i))
    Next
    It is possible that only the first few or last few requests are answered.

    Next, I'd add a Log here to confirm that the SetNotify parameters are what you think they are, eg:
    Code:
    Log("SetNotify [" & ServiceId & "] [" & C301_riposta & "]")
    manager.SetNotify(ServiceId, C301_risposta, 
    True)
    and compare them to what you saw in Nrf Connect, including the upper/lowercaseness.
     
  5. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    The log is:
    SetNotify [0000a002-0000-1000-8000-00805f9b34fb] [0000c301-0000-1000-8000-00805f9b34fb]
    on nrf i see: (attached picture)

    Error is the same

    Is it correct my mode to use SetModify?
     

    Attached Files:

  6. emexes

    emexes Well-Known Member Licensed User

    Characteristic 0000c301-0000-1000-8000-00805f9b34fb is not notifiable.

    upload_2019-7-2_19-24-39.png
     
  7. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    sorry where do you understand it?
    Then chip is misconfigured?
     
  8. emexes

    emexes Well-Known Member Licensed User

    I've found with BLE that step one is to write the simplest possible app that reads some data.

    There is an awful lot that needs to go right, and it only takes one wrong thing for it not to work.

    Do you have such an app?

    If not, I can send you my first pressure-reading app.
     
  9. emexes

    emexes Well-Known Member Licensed User

    The characteristics properties are: READ, WRITE

    You can read it and write it, but it does not do notifications.
     
  10. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    Tes plase send me it. I have difficulties to manage characteristics
     
  11. emexes

    emexes Well-Known Member Licensed User

    Eg, on this BLE sensor here, I can READ and NOTIFY (stream of readings) the temperature, but I cannot write to it. And I can READ and WRITE the measurement interval, but there is no notify facility for it, because the only time it would change is if I wrote to it.

    upload_2019-7-2_19-50-9.png
     
  12. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    There is a method to read without notify?
    If I can have a code to manage these characteristics....
     
  13. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    There is a method to read without DataAvailable?
     
  14. emexes

    emexes Well-Known Member Licensed User

    Which characteristic can you receive data on using nRF?
    ReadData.

    Can you do it from Nrf Connect? Click the single-arrow that points down, next to the characteristic.
     
  15. emexes

    emexes Well-Known Member Licensed User

    Not that I know of, although I've never tried.

    I always need to do at least one ReadData because it comes back with the Characteristic Ids.
     
  16. emexes

    emexes Well-Known Member Licensed User

    Sorry about the delay, just wanted to make sure it works here first, then I thought I'll just make one change to make it better...
     
  17. Marco Maria Vilucchi

    Marco Maria Vilucchi Active Member Licensed User

    I use readdata to read services

    Code:
    For i=0 To Services.Size-1
            manager.ReadData(Services.Get(i))
        
    Next
    but how can I read data from characteristics?
    I see that only DataAvailable has Characteristics as parameter, but how cha I start DataAvaialble routine?

    Please can you give me your base code?
     
  18. emexes

    emexes Well-Known Member Licensed User

    Code:
    For i=0 To Services.Size-1
        manager.ReadData(Services.Get(i))
    Next
    I think that the above code is issuing too many ReadData requests all at once, and it overwhelms the BLE device. At least, that is what would happen with my BLE pressure sensor - it runs off a battery and is very slow.

    When you send a BLE ReadData request, the BLE device should return some data and that will call (start) the DataAvailable routine, with the received data.
     
  19. emexes

    emexes Well-Known Member Licensed User

    Finally! Attached.
     

    Attached Files:

  20. emexes

    emexes Well-Known Member Licensed User

    The following are the only changes made after I tested it with my BLE device here, to (hopefully) make it work with your device:
    Code:
    '''If Id.EndsWith("9D:0A") Then  
    If Id.EndsWith("B9:AA"Then

    '''If S.StartsWith("cc4a6a80") Then
    If S.StartsWith("0000a002"Then
          
    '''If C.StartsWith("835ab4c0") Then
    If C.StartsWith("0000c301"Then
     
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