Android Question BLE2 Service not found error

Steve-h

Member
Have a BLE module configured with a 128 bit Service UUID and a 128 bit write without reply characteristic, the unit has the name pump4. Using nRF connect I can reliably connect to it and write data to the characteristic.

To establish a connection with BLE2 I use Scan2

B4X:
 Public Sub StartScan
    Dim serv As String
    If manager.State <> manager.STATE_POWERED_ON Then
        Log("Not powered on.")
    Else
        manager.Scan2(Null, False)
        Log (serv)
    End If
End Sub

When Scan2 finds something it calls DeviceFound where I check to see if the device is pump4. If it is then I stop the scan and connect.

B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log(Name & ", " & Id &" " & RSSI)
    ConnectedName = Name
    remoterssi=RSSI
    
        Labname.Text=Name
        Labrssi.Text=remoterssi
        LabRxdata.Text=Id
        
        If Name="pump4" Then
            manager.StopScan
            manager.Connect(Id)
        Else
         StartScan
        End If

End Sub

The logging then shows Connected... Discovering services. Then roughly half of the time immediately disconnects, I am not clear which end is disconnecting. On the occasions where it stays connected a request to read the RSSI is successfull suggesting the remote end is alive and talking.

What I then try to do is write to the remote characteristic using WriteData but I always get a Service not found error on the line containing the WriteData command.

B4X:
    Private Sub ButSend_Click
    Dim serveuuid As String = "f1abd2e4876c11eca8a30242ac120002 "        'UUID  for Pump service
    Dim txuuid As String = "f1abd4ce876c11eca8a30242ac120002"            'UUID for Pump Write characteristic
    Dim dat As String = "7FFF"

    manager.WriteData(serveuuid,txuuid,dat.GetBytes("utf8"))
End Sub

From the device found sub I display the Name the Rssi and the ID, the reported ID is "E8 : EB : 1B : 9A : AF: DE" and I have to confess I have no idea where that number comes from . The real mystery however is having connected with the remote device why does it then report the service as not found ?
 

Steve-h

Member
Thank you, that pointed me in the correct direction although I was confused by your example. BleService doesn't seem to be known by my system and the Log($"** Service: ${s.Uuid} **"$) statement was not immediately obvious, being fairly new to B4A I have yet to figure what the curly brackets are doing.

By starting a scan and then using Connect with the device ID logged along with the name I can reliably connect and then write data using WriteData.

My confusion was two things, I think it is correct to say :-

First you must scan and get a scan result from the device you want to connect to, even if you know the ID you have to scan first and log the device ID.

Secondly it is the device ID not the service UUID you need to enter in the Connect member.

So I can now connect and write to the remote device but not read, I will ask about that in a separate post.
 
Upvote 0
You have a space at the end of Pump Service UUID. Try removing it and Write Data.
The ID "E8 : EB : 1B : 9A : AF: DE" is the Mac Address of your device. nrf app too should show that id.
 
Upvote 0

Steve-h

Member
Spurious space Well spotted, thank you.

Yes, now understand that Connect requires Mac address where read and write require UUID.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…