Android Question HotSwap to nearest BLE Device

lip

Active Member
Licensed User
Longtime User
I have a number of custom-made devices with nrf chip with BLE. I can connect and talk to them but would like to keep connected to the nearest device (best RSSI).

I have two BLEManager2 objects, one connected for comms, and a second one scanning for devices to find the nearest one.

2 Managers - Initialised with different Event names:
    BLEManagerMate1.Initialize("BLEManagerMate1")
    BLEManagerMate2.Initialize("BLEManagerMate2")

I tried initialising them to the same Event name and using 'Sender' to handle the events for each manager, but this proved difficult as I need to handle events differently depending on whether they are for the connected or just a candidate device.

When a new device is detected by Manager2 records it in a map, then checks the Rssi of the connected device on Manager1, and if there is a better one it connects. This all works well...

Compare RSSI:
Sub BLEManagerMate1_RssiAvailable(Succsess As Boolean,Rssi As Double)
    If Succsess=True Then
        Dim ConnectedMate As String = Subs.GetParamStringTablet("LastMate")
        Log("BLE Connected to " & ConnectedMate & " " & Rssi & "dB.  Available devices are:")
        Dim BestMate As String = ConnectedMate
        Dim BestRSSI As Int = Rssi
        For Each myID As String In BLEMateAvailableMap.Keys
            Dim AvailableRssi As Int = BLEMateAvailableMap.Get(myID)
            Log("    " & myID & " " & AvailableRssi & "dB")
            If AvailableRssi > BestRSSI Then
                BestMate = myID
                BestRSSI = AvailableRssi
            End If
        Next
        Log("BestBate is " & BestMate & " at " & BestRSSI & "dB")
        If BestMate <> ConnectedMate Then
            BLEManagerMate2.Connect(BestMate)
        End If
    End If   
End Sub

I then try to swap the Managers over so that I am now using the nearer device on Manager 1. This does not happen on _Connect as I need to set up the device first by setting notify, negotiating MTU etc first, but once this is done I try to set Manager1 to point to Manager2

Hot swapping the device:
        BLEManagerMate1 = BLEManagerMate2

This works in that Manager1.WriteData now uses the newly connected device, but the responses come back on the event Manager2_DataAvailable.

Is there any way I can change the Event name for DataAvailable? Or a different way of switching between devices?
 
Solution
Solved: I keep two BLE Managers (BLE1 and BLE2 as BLEManager2) with almost identical code and event handlers (but different event names). At any time, one is doing the comms, the other is searching for nearer devices. I have a global variable ActiveBLEManager (0, 1 or 2). 0 for no connection, 1 for BLE1 and 2 for BLE2. If a nearer device is found then it connects, authenticates, sets notify, negotiates mtu, THEN changes ActiveBLEManager. Just before WriteData I select the BLEManager based on the ActiveBLEManager global.

lip

Active Member
Licensed User
Longtime User
Solved: I keep two BLE Managers (BLE1 and BLE2 as BLEManager2) with almost identical code and event handlers (but different event names). At any time, one is doing the comms, the other is searching for nearer devices. I have a global variable ActiveBLEManager (0, 1 or 2). 0 for no connection, 1 for BLE1 and 2 for BLE2. If a nearer device is found then it connects, authenticates, sets notify, negotiates mtu, THEN changes ActiveBLEManager. Just before WriteData I select the BLEManager based on the ActiveBLEManager global.
 
Upvote 0
Solution
Top