Android Question (Solved) BLE - SetNotify NordicModule

f0raster0

Well-Known Member
Licensed User
Longtime User
Hi guys,

I'm trying to read enviroment data from a module called thingy52,
the log below mean the module is not sending any data

Found: AntuTech, FF:B0:19:AF:F0:9F, RSSI = -45, (MyMap) {1=[B@42a20ab8, 6=[B@42a20b20, 8=[B@42a20b70, -1=[B@42a20bb8, 0=[B@42a20660}
Este = 1: 06
Este = 6: 420074A9FF52109B3349359B000168EF
Este = 8: 416E747554656368
Este = -1: 5900623EC2FA
Este = 0: 0201061106420074A9FF52109B3349359B000168EF0908416E74755465636807FF5900623EC2FA0000000000000000000000000000000000000000000000
Discovering services.
Connected
Key: 00002a05-0000-1000-8000-00805f9b34fb, value:
Key: 8ec90003-f315-4f60-9fb8-838830daea50, value:
Key: 00002a00-0000-1000-8000-00805f9b34fb, value: 416E7475546563683532
Key: 00002a01-0000-1000-8000-00805f9b34fb, value: 0000
Key: 00002a04-0000-1000-8000-00805f9b34fb, value: 0600180000004001
Key: 00002aa6-0000-1000-8000-00805f9b34fb, value: 01
Key: ef680101-9b35-4933-9b10-52ffa9740042, value: 416E7475546563683532
Key: ef680102-9b35-4933-9b10-52ffa9740042, value: 6002B4
Key: ef680104-9b35-4933-9b10-52ffa9740042, value: 0600180000004001
Key: ef680105-9b35-4933-9b10-52ffa9740042, value: 03676F6F2E676C2F704957646972
Key: ef680106-9b35-4933-9b10-52ffa9740042, value:
Key: ef680107-9b35-4933-9b10-52ffa9740042, value: 020100
Key: ef680108-9b35-4933-9b10-52ffa9740042, value: 001700
Key: ef680206-9b35-4933-9b10-52ffa9740042, value: D007D007D007DC0502674E1D
Key: ef680201-9b35-4933-9b10-52ffa9740042, value:
Key: ef680202-9b35-4933-9b10-52ffa9740042, value:
Key: ef680203-9b35-4933-9b10-52ffa9740042, value:
Key: ef680204-9b35-4933-9b10-52ffa9740042, value:
Key: ef680205-9b35-4933-9b10-52ffa9740042, value:
Key: ef680401-9b35-4933-9b10-52ffa9740042, value: E803F401F4010A0001
Key: ef680402-9b35-4933-9b10-52ffa9740042, value:
Key: ef680403-9b35-4933-9b10-52ffa9740042, value:
Key: ef680404-9b35-4933-9b10-52ffa9740042, value:
Key: ef680405-9b35-4933-9b10-52ffa9740042, value:
Key: ef680406-9b35-4933-9b10-52ffa9740042, value:
Key: ef680407-9b35-4933-9b10-52ffa9740042, value:
Key: ef680408-9b35-4933-9b10-52ffa9740042, value:
Key: ef680409-9b35-4933-9b10-52ffa9740042, value:
Key: ef68040a-9b35-4933-9b10-52ffa9740042, value:
Key: ef680301-9b35-4933-9b10-52ffa9740042, value: 020512AC0D
Key: ef680303-9b35-4933-9b10-52ffa9740042, value: 00000000
Key: ef680302-9b35-4933-9b10-52ffa9740042, value:
Key: ef680501-9b35-4933-9b10-52ffa9740042, value: 0101
Key: ef680502-9b35-4933-9b10-52ffa9740042, value:
Key: ef680503-9b35-4933-9b10-52ffa9740042, value:
Key: ef680504-9b35-4933-9b10-52ffa9740042, value:
Key: 00002a19-0000-1000-8000-00805f9b34fb, value: 43

I think because I have to active the notify in the module, I tried this:

B4X:
Public Sub writeData1
    manager.SetNotify("EF680200-9B35-4933-9B10-52FFA9740042","EF680201-9B35-4933-9B10-52FFA9740042",True)
    Log("Set Notify")
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
    Dim bc As ByteConverter
    Dim KeyCode As String
   
    Dim bc As ByteConverter
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log("Key: " & key &", value: " & bc.HexFromBytes(b))
        'Log(BytesToString(b, 0, b.Length, "utf8") 'useful if the data is actually a string
        If key = "" Then
        End If    
    Next

'    https://nordicsemiconductor.github.io/Nordic-Thingy52-FW/documentation/firmware_architecture.html#fw_arch_ble_services
'    Environment Service:
'    Base UUID: EF68xxxx-9B35-4933-9B10-52FFA9740042
'    Temperature characteristic: EF680201-9B35-4933-9B10-52FFA9740042
'    Pressure characteristic : EF680202-9B35-4933-9B10-52FFA9740042
'    Humidity characteristic: EF680203-9B35-4933-9B10-52FFA9740042
   
End Sub

I got this error: "Caused by: java.lang.RuntimeException: Service not found"
line 79 is: manager.SetNotify("EF680200-9B35-4933-9B10-52FFA9740042","EF680201-9B35-4933-9B10-52FFA9740042",True)


Error occurred on line: 79 (Starter)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.keywords.Common.CallSubDebug(Common.java:1026)
at b4a.example3.main._button1_click(main.java:570)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4475)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:333)
at anywheresoftware.b4a.debug.Debug.CallSubNew(Debug.java:279)
... 26 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:315)
... 27 more
Caused by: java.lang.RuntimeException: Service not found
at anywheresoftware.b4a.objects.BleManager2.getService(BleManager2.java:375)
at anywheresoftware.b4a.objects.BleManager2.setNotify(BleManager2.java:301)
at anywheresoftware.b4a.objects.BleManager2.SetNotify(BleManager2.java:298)
at b4a.example3.starter._writedata1(starter.java:210)
... 30 more
 
Last edited:

f0raster0

Well-Known Member
Licensed User
Longtime User
Full code BLE example
B4X:
#Region  Project Attributes
    #ApplicationLabel: Thingy B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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
    Private btnReadData As Button
    Private btnDisconnect As Button
    Private btnScan As Button
    Private lblDeviceStatus As Label
    Private lblState As Label
    Private pbReadData As ProgressBar
    Private pbScan As ProgressBar
    Private clv As CustomListView
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
   
    StateChanged
End Sub

Public Sub StateChanged
    lblState.Text = Starter.currentStateText
    If Starter.connected Then
        lblDeviceStatus.Text = "Connected: " & Starter.ConnectedName
    Else
        lblDeviceStatus.Text = "Not connected"
    End If
    btnDisconnect.Enabled = Starter.connected
    btnScan.Enabled = Not(Starter.connected)
    pbReadData.Visible = False
    pbScan.Visible = False
    btnReadData.Enabled = Starter.connected
    btnScan.Enabled = (Starter.currentState = Starter.manager.STATE_POWERED_ON) And Starter.connected = False
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnScan_Click
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result = False Then Return
    pbScan.Visible = True
    CallSub(Starter, "StartScan")
End Sub

Sub DataAvailable (Service As String, Characteristics As Map)
    pbReadData.Visible = False
    clv.Add(CreateServiceItem(Service), "")
    For Each id As String In Characteristics.Keys
        clv.Add(CreateCharacteristicItem(id, Characteristics.Get(id)), "")
    Next
End Sub

Sub btnDisconnect_Click
    CallSub(Starter, "Disconnect")
End Sub

Sub btnReadData_Click
    pbReadData.Visible = True
    clv.Clear
    CallSub(Starter, "ReadData")
End Sub

Sub CreateServiceItem (service As String) As Panel
    Dim pnl As Panel
    pnl.Initialize("")
    pnl.Color = 0xFF808080
    pnl.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 30dip)
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = service
    lbl.Gravity = Gravity.CENTER
    lbl.Typeface = Typeface.DEFAULT_BOLD
    pnl.AddView(lbl, 0, 0, clv.AsView.Width, 30dip)
    Return pnl
End Sub

Sub CreateCharacteristicItem(Id As String, Data() As Byte) As Panel
    Dim pnl As Panel
    pnl.Initialize("")
    pnl.SetLayoutAnimated(0, 0, 0, clv.AsView.Width, 40dip)
    pnl.Color = Colors.White
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Text = Id
    pnl.AddView(lbl, 0, 0, clv.AsView.Width, 20dip)
    Dim lbl2 As Label
    lbl2.Initialize("")
    Try
        lbl2.Text = BytesToString(Data, 0, Data.Length, "UTF8")
    Catch
        Log(LastException)
        lbl2.Text = "Error reading data as string"
    End Try
    lbl2.TextColor = 0xFF909090
    lbl2.TextSize = 14
    pnl.AddView(lbl2, 0, 20dip, clv.AsView.Width, 20dip)
    Return pnl
End Sub

Sub Button1_Click
    CallSub(Starter, "writeData1")
End Sub

B4X:
#Region  Service Attributes

    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public manager As BleManager2
    Public currentStateText As String = "UNKNOWN"
    Public currentState As Int
    Public connected As Boolean = False
    Public ConnectedName As String
    Private ConnectedServices As List
    Public rp As RuntimePermissions
End Sub

Sub Service_Create
    manager.Initialize("manager")
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Public Sub ReadData
    For Each s As String In ConnectedServices
        manager.ReadData(s)
    Next
End Sub

Public Sub Disconnect
    manager.Disconnect
    Manager_Disconnected
End Sub

Sub Manager_StateChanged (State As Int)
    Select State
        Case manager.STATE_POWERED_OFF
            currentStateText = "POWERED OFF"
        Case manager.STATE_POWERED_ON
            currentStateText = "POWERED ON"
        Case manager.STATE_UNSUPPORTED
            currentStateText = "UNSUPPORTED"
    End Select
    currentState = State
    CallSub(Main, "StateChanged")
End Sub

Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData)
    'print all the advertising data map:
    Dim bc As ByteConverter
    ConnectedName = Name
   
    If Name = "AntuTech" Then
        For Each k As Int In AdvertisingData.Keys
            Log("Este = " & $"${k}: ${bc.HexFromBytes(AdvertisingData.Get(k))}"$)
        Next
    End If

    manager.StopScan
    manager.Connect2(Id, True) 'disabling auto connect can make the connection quicker
End Sub

Public Sub StartScan
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on.")
    Else If rp.Check(rp.PERMISSION_ACCESS_COARSE_LOCATION) = False Then
        Log("No location permission.")
    Else
        manager.Scan2(Null, False)
    End If
End Sub

Public Sub writeData1
    'Enable battery
    'manager.SetNotify("0000180d-0000-1000-8000-00805f9b34fb","00002a19-0000-1000-8000-00805f9b34fb",True)

    'Enable battery
    manager.SetNotify("EF680200-9B35-4933-9B10-52FFA9740042","EF680203-9B35-4933-9B10-52FFA9740042",True)
'    Log("Set Notify")
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
    Dim bc As ByteConverter
    Dim KeyCode As String
   
    Dim bc As ByteConverter
    For Each key As String In Characteristics.Keys
        Dim b() As Byte = Characteristics.Get(key)
        Log("Key: " & key &", value: " & bc.HexFromBytes(b))
        'Log(BytesToString(b, 0, b.Length, "utf8") 'useful if the data is actually a string
        If key = "" Then
        End If    
    Next

'    https://nordicsemiconductor.github.io/Nordic-Thingy52-FW/documentation/firmware_architecture.html#arch_env
'    Then For environment Service:
'    Base UUID: EF68xxxx-9B35-4933-9B10-52FFA9740042
'    Temperature characteristic: EF680201-9B35-4933-9B10-52FFA9740042
'    Pressure characteristic : EF680202-9B35-4933-9B10-52FFA9740042
'    Humidity characteristic: EF680203-9B35-4933-9B10-52FFA9740042
   
End Sub

Sub Manager_Disconnected
    Log("Disconnected")
    connected = False
    CallSub(Main, "StateChanged")
End Sub

Sub Manager_Connected (services As List)
    Log("Connected")
    connected = True
    ConnectedServices = services
    CallSub(Main, "StateChanged")
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
yes, I do.

Screenshot_2018-05-20-19-12-02.png
 
Last edited:
Upvote 0
Top