iOS Question Multiple BLE Managers?

techknight

Well-Known Member
Licensed User
Longtime User
Is it possible to have multiple blemanager objects?

I am quickly running into a problem that I am not entirely sure how to manage. the iPad totally hates being run as both a peripheral and a central.

So, I am trying to keep the iPad as a central only.

But I ran into a brickwall there too, because when manager_disconnect fires, there is no way to keep track of which device disconnected.

Basically, I am connecting to a BLE serial device using the Nordic profile, which works fine. But at the same time, I am trying to connect to a phone as a peripheral as well. so somehow the iPad has to be able to juggle both connections which seems easy at first, but things get hairy when one of the two disconnects.

No way to know which one disconnected.... So I cant run the appropriate routines. If I leave it in SCAN mode all the time for the Phones, thats fine. but then if the BLE Serial device disconnects im screwed as I will have no idea that it did.

Even this wouldnt be too big of an issue, except I am sending very different data to the two different peripherals. So I have to have some way to keep track of which peripheral is connected, and if a disconnect event fires, knowing which one disconnected.

any ideas?
 

techknight

Well-Known Member
Licensed User
Longtime User
Nope. tried having 2 managers, thats a big fail.

it will allow a peripheral to connect through a 2nd manager, but it gets caught into this connect/disconnect loop of death.

If I comment out the 1st manager (the one which controls my serial), of course now it works fine.

Any thoughts here?

Edit nope: Still does it with only a single manager. Maybe a bug somewhere.. here is my code:

B4X:
Sub EnableBT
    If manager2.IsInitialized = False Then 
        manager2.Initialize("manager2") 'Initialize and enable the BT Peripheral mode.
    Else
        manager2.StopScan
        StartScan
    End If
    'If connectedCentrals.IsInitialized = False Then connectedCentrals.Initialize 'Initialize the container that will hold all the incoming connected remotes. 
End Sub
Sub Manager2_StateChanged (State As Int)
    If State <> manager2.STATE_POWERED_ON Then
        Main.hd.ToastMessageShow("Bluetooth is not enabled", True)
    Else
        'Peripheral.Start("MD Controller")
        StartScan    'Look for remotes that are currently broadcasting services.
    End If
End Sub
Private Sub StartScan
    If manager2.State = manager2.STATE_POWERED_ON Then
        manager2.Scan2(Array(RemoteServiceID), False)
    End If
End Sub
Private Sub Manager2_DeviceFound (Name As String, DeviceId As String, AdvertisingData As Map, RSSI As Double)
    Log("DeviceFound: " & Name & " " & DeviceId)
    manager2.Connect(DeviceId)
End Sub
Private Sub Manager2_Connected (Services As List)
    Log("Remote Connected")
    firstread = True
    manager2.ReadData(RemoteServiceID) 'must call once to discover the characteristics
End Sub
Private Sub Manager2_DataAvailable (SId As String, Characteristics As Map)
    If firstread Then
        manager2.SetNotify(RemoteServiceID, RemoteReadChar, True)
        firstread = False
        Return
    End If
    Dim b() As Byte = Characteristics.Get(RemoteReadChar)
    Dim RXData As String
    RXData = BytesToString(b, 0, b.Length, "UTF8")
    Log(RXData)
End Sub
Private Sub Manager2_Disconnected
    Log("Remote Disconnected")
    StartScan
End Sub
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I know it does, because there are HVAC smart probes out there that work with iOS, and there are mutliple probes at a time, making multiple connections over bluetooth, so I know its possible.

But for some reason, I am stuck in this connect/disconnect loop initiated by the code above. (The code above is the 2nd manager, and its connecting to the bone stock B4A Peripheral chat app for testing.)

Here is the log:
B4X:
DeviceFound: B4APeripheral 2BCD4305-C579-5FE6-3B10-6322749B6A1B
DeviceFound: B4APeripheral 2BCD4305-C579-5FE6-3B10-6322749B6A1B
Remote Disconnected
Discovering services
Services discovery completed.
Remote Connected
Notification state changed for characteristic: 1001
DeviceFound: B4APeripheral 2BCD4305-C579-5FE6-3B10-6322749B6A1B
Remote Disconnected
Discovering services
Services discovery completed.
Remote Connected
Notification state changed for characteristic: 1001
DeviceFound: B4APeripheral DB6FA57B-DC78-6C05-0F14-295E25385773
DeviceFound: B4APeripheral DB6FA57B-DC78-6C05-0F14-295E25385773
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
DeviceFound: B4APeripheral A44B9469-BDC4-9C71-AB3C-3FB9E948F54A
Remote Disconnected
Remote Disconnected
Application_Inactive
Application_Background

any thoughts?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Well the above is occuring when I am not even using or connecting to one of the peripherals. This is just from Android phone to iOS Tablet.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I am. One is my bluetooth BLE device, and the other peripheral is my phone (used as a remote). The iPad is acting as a central.
 
Upvote 0
Top