Android Question B4A BLE2 Multiple Connection Problem

Lvh

Member
Licensed User
Hi Erel,
I have created a Galaxy S8 Central controlling 2 BLE Devices, using the BLE2 V1.35 Library.
Schematic :
S8 CENTRAL PERIPHERALS
Manager1 Buttons Set ------> Device1
Manager2 Buttons Set ------> Device2


My problem is about the multiple connection :
- When connect with success the manager1 to Device1, I can't connect the manager2 to Device2
- Inversely, when connect with success the manager1 to Device2, I can't connect the manager2 to Device1
- I can connect both manager1 and manager2 to a same device (either Device1 or Device2)

The log of a multiple connection is below :

Manager1 - Connecting to FE:D4:CC:A5:D1:02
Discovering services.
Manager1 Connected
Manager2 - Connecting to D7:6F:F9:2B:0C:73
Discovering services.
Service discovery failed.

My program works as below:
- I use 2 managers.
- My program starts by launching manager1.Scan(Null), sleep(4000) then manager2.Scan(Null).
- For each scanning, the DeviceName and MacAddress of the found devices are stored in m1FoundDevices as List or m2FoundDevices as List.
- The main Activity has 2 buttons CONNECT1 and CONNECT2.
When CONNECTx is pressed, managerX.Connect2(MacAddress,False) is launched to connect to a device having the MacAddress (one of the mXFoundDevices List).

Attached is the source of my Starter Module.
 

Attachments

  • Starter.txt
    8.3 KB · Views: 540

Lvh

Member
Licensed User
Hi,
Sorry, I mix Q&A and forum. Please accept my apologies for having addressed directly to you.
Please find attached the zip file of my project export and the Log file containing the advertising data of my two devices , hoping these files will be useful for you to help me.
In any case, thank you very much for responding so quickly.

Regards
 

Attachments

  • BleSW_Central.zip
    13.7 KB · Views: 486
  • Log1.txt
    4.3 KB · Views: 549
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Create a new project with this code in the starter service:
B4X:
Sub Process_Globals
   Private Manager1, Manager2 As BleManager2
   Private managers As Map
End Sub

Sub Service_Create
   Manager1.Initialize("Manager")
   Manager2.Initialize("Manager")
   managers = CreateMap(Manager1: "m1", Manager2: "m2")
End Sub

Sub Manager_StateChanged (State As Int)
   Dim m As BleManager2 = Sender
   Log("StateChanged: " & managers.Get(m) & ", " & State)
   If State = m.STATE_POWERED_ON Then
     m.Scan(Null)
   End If
End Sub

Sub Manager_DeviceFound (Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)
   Dim m As BleManager2 = Sender
   Log("Manager_DeviceFound: " & managers.Get(m) & ", " & DeviceId & ", " & Name)
   Dim TargetName As String
   If managers.Get(m) = "m1" Then TargetName = "BLE SWITCH 1" Else TargetName = "BLE SWITCH 2"
   If Name = TargetName Then
     Log("Trying to connect")
     m.Connect2(DeviceId, False)
   End If
End Sub

Sub Manager_Connected (Services As List)
   Log("Manager_Connected: " & managers.Get(Sender))
End Sub

Sub Manager_Disconnected
   Log("Manager_Disconnected: " & managers.Get(Sender))
End Sub

What happens when you run it?
 
Upvote 0

Lvh

Member
Licensed User
Hi,
Thank you very much for your quick response.
As asked, I created a new program integrating your Starter Codes as it. The run result is unfortunatly negative - See Log0.txt.
Analyzing the Log0, I notice that there are timming problems : when m1 is trying to Discover Services, there in Device found event for m2 and the later tries to connect etc...
From this observation, I created my version which works as follows:
- I start by launching m1.Scan only.
- In Manager_DeviceFound event, I stop m1.Scan and launch m1.Connect
- In Manager_Connected event, if Sender is "m1" then I launch m2.Scan, if Sender is "m2" then I do nothing.​

The run result of this version is ... VICTORY (see Log1.txt), ... BUT some time after, when I retest it the run result is as bad as from the beginning : m1 connection is always Ok but m2 connection is alway failed (see Log2.txt).
Please note that when the log "m2 - Trying to connect" appears, my Device 2 is connected quickly (my devices have a Led for Connection Monitoring) but it is disconnected some time later becaue the failure of the "Services Discovering" operation.

Do you have an idea ?
Attached is the export of my version in which your original codes are transformed into comments.

Regards.
 

Attachments

  • Log0.txt
    2.6 KB · Views: 441
  • Log1.txt
    499 bytes · Views: 423
  • Log2.txt
    2.1 KB · Views: 436
  • Ble_Remote_Central2.zip
    7.4 KB · Views: 486
Upvote 0

Lvh

Member
Licensed User
Hi,
I took some time to answer you because I had some verification works to do.
As suggested, I wrote an auto reconnect routine : when the Device 2 is disconnected, I wait 5000 ms then reconnect to it. Result : there is no improvement, Device 1 is always connected with success and Device 2 connection is always failed.
Anyway, I do not think it was a low level problem because
- My two devices are identical, hardware and Gatt Profil.
- When I try to connect to the Device 2 first, then Device1, I always have success for the Device 2 connection and failed for the Device 1.​
I think it's rather a problem of timming or cached data. Going into this idea, I launched my program in release mode, instead of debug mode and BINGO: all connections are made with 100% of success.

Best regards.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top