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


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)


 
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…