Fix an issue where iOS couldn't reconnect to a G2 arm because it already had an ANCS connection open to it

This commit is contained in:
James Babcock committed 2026-11-17 17:38:11 -08:00
1 parent ab692741f7
commit d1e424b7e1
6 files changed
+164 -6

No files matched your search

@@ -12,6 +12,12 @@ import kotlin.jvm.JvmStatic
*/
class BleProtocol {
companion object {
/** The service holding [WRITE_CHAR_UUID] and [NOTIFY_CHAR_UUID]. */
const val PROTOCOL_SERVICE_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5450"
/** The service holding [RENDER_NOTIFY_UUID]. */
const val RENDER_SERVICE_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e6450"
const val WRITE_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5401"
const val NOTIFY_CHAR_UUID: String = "00002760-08c2-11e1-9073-0e8ac72e5402"
@@ -29,6 +29,7 @@ import platform.CoreBluetooth.CBPeripheralDelegateProtocol
import platform.CoreBluetooth.CBPeripheralStateConnected
import platform.CoreBluetooth.CBPeripheralStateDisconnected
import platform.CoreBluetooth.CBService
import platform.CoreBluetooth.CBUUID
import platform.Foundation.NSData
import platform.Foundation.NSError
import platform.Foundation.NSNumber
@@ -47,6 +48,9 @@ interface IosBleScanListener {
fun onAdvertisement(identifier: String, name: String, manufacturerDataHex: String, rssi: Int, connectable: Boolean)
}
/** A peripheral the system holds a connection to; see [IosBleCentral.systemConnectedG2Arms]. */
class IosConnectedPeripheral(val identifier: String, val name: String)
/** ANCS authorization changes for a connected peripheral; delivered on the BLE queue. */
fun interface IosAncsAuthorizationListener {
fun onAncsAuthorization(identifier: String, authorized: Boolean)
@@ -214,6 +218,25 @@ class IosBleCentral(private val platform: ProtocolPlatform = IosProtocolPlatform
manager.scanForPeripheralsWithServices(null, mapOf(CBCentralManagerScanOptionAllowDuplicatesKey to true))
}
/**
* G2 arms the system is connected to, through this app, another app or iOS itself. A
* connected arm stops advertising, and iOS keeps the right arm's ANCS link open after a
* session ends, so a scan alone never hears it. Empty until Bluetooth is powered on.
*/
fun systemConnectedG2Arms(): List<IosConnectedPeripheral> {
val found = ArrayList<IosConnectedPeripheral>()
dispatch_sync(queue) {
val manager = central ?: return@dispatch_sync
if (manager.state != CBManagerStatePoweredOn) return@dispatch_sync
val services = listOf(BleProtocol.PROTOCOL_SERVICE_UUID, BleProtocol.RENDER_SERVICE_UUID).map { CBUUID.UUIDWithString(it) }
for (item in manager.retrieveConnectedPeripheralsWithServices(services)) {
val peripheral = item as? CBPeripheral ?: continue
found.add(IosConnectedPeripheral(peripheral.identifier.UUIDString.uppercase(), peripheral.name ?: ""))
}
}
return found
}
/** Known peripheral identifiers from this process's scans/retrievals (uppercase). */
fun knownIdentifiers(): List<String> = cond.withLock { peers.keys.toList() }