Android Question BLE manager.scan2

Philip Prins

Active Member
Licensed User
Hello

I am trying to connect to a device based on its UUID.
BLE2 library 1.38
Android 8.1.0

When i do manager.scan(Null) the device is discovered and able to connect

When i use manager.Scan2(ScanFilter, True) the device found is never raised.
I retrieved the UUID from another app on the same android device.
B4X:
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
       
        '00001801-0000-1000-8000-00805f9b34fb
        Dim ScanFilter As List
        ScanFilter.Initialize
        ScanFilter.Add(UUID2("89a8591d-bb19-485B-9f59-58492bc33e24"))
        ScanFilter.Add("2320ae58-8394-4652-95f7-0a872ac0958f")
        ScanFilter.Add("41df72e4-9721-41bd-b27f-f507a94d9634")
        ScanFilter.Add(HEART_RATE_SERVICE)
        Log(ScanFilter)
        manager.Scan2(ScanFilter, True)
        'manager.Scan(Null)
   
    End If
End Sub

I have tried several examples .

The manufacturar suggests this:
To access the button status from your app, please perform the following in Android:
 Scan for Blu-PTT in the list of “bonded devices” rather than just under a BTLE scan
 Implement one notify event on button press
 Implement one notify event on button release.

How to achieve this in B4A?
Searched thru all the examples and problems with BLE(a lot of problems) on the forum.
 

emexes

Expert
Licensed User
I haven't used the ScanFilter parameter. All of the devices I have used so far have been identifiable by the Name.

I would expect that the ScanFilter can only work using information available in the AdvertisingData field of:

DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double) - Raised when a new device is discovered (after you call BleManager.Scan). The event parameters include the device Id, name, RSSI value and a Map with additional advertised data.


so perhaps set the Filter to Null to receive all devices, and look at the AdvertisingData Maps to see what info might be available to filter on.

If that doesn't work, then, the first time your program runs and wants to connect to the device, you might need to Connect to each DeviceFound and examine the list of Services returned by Connect, to work out which devices are compatible with your software. Then write the UUIDs of compatible devices to a file, and use that in future for selecting a device UUID to connect to.
 
Upvote 0

Philip Prins

Active Member
Licensed User
The problem was in Scan2 , if you use Scan(filter) it works.
Thanks to Mike , Moster67 i got it working.

The buttons are small BLE pushbuttons , known as BluPTT but also available under other names.
Below the working class:
B4X:
Sub Class_Globals
    Private manager As BleManager2
    Private bc As ByteConverter
    Private sUUID As String = "2320ae58-8394-4652-95f7-0a872ac0958f" ' Scan Service UUID
    Private nUUID As String = "89A8591D-BB19-485B-9F59-58492BC33E24" ' Notify UUID
    
    Private bUUID As String = "894C8042-E841-461C-A5C9-5A73D25DB08E" ' Button characteristic
    
    Private firstRead As Boolean
    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

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    manager.Initialize("Manager")
End Sub


Private Sub LWS(id As String) As String 'To lower string (B4i upper case)
 
    Return  id.ToLowerCase

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) 'ignore
    ConnectedName = Name
    manager.StopScan
    manager.Connect2(Id, False) '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
        
        
        Dim ScanFilter As List
        ScanFilter.Initialize
'
        ScanFilter.Add(LWS(sUUID)) ' Find Device
        manager.Scan(ScanFilter)  ' Find Device with this sUUID
    
    End If
End Sub


Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
    
    If firstRead Then
        firstRead = False
        manager.SetNotify(LWS(nUUID), LWS(bUUID), True)
        Return
    End If
    
    Dim b() As Byte = Characteristics.Get(LWS(bUUID))
    Log(b(0))
    If (b(0)) = 1 Then
        Log("Button pressed")
    End If
    If (b(0)) = 0 Then
        Log("Button released")
    End If
    
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
    firstRead = True
    manager.ReadData(LWS(sUUID))
End Sub
 
Upvote 0
Top