Skip to content

Commit c9c0993

Browse files
committed
ios: only detect the device in the backend once paired
The device first connects. Pairing happens later when a secured characteristic is used. We assume the device is paired when we can read the product string, at which point the backend register the device. Otherwise, the app would launch into the device workflows in the UI and get overlaid with the pairing code suddenly, which looks broken.
1 parent 500470d commit c9c0993

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

frontends/ios/BitBoxApp/BitBoxApp/Bluetooth.swift

+24-14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
4949
var pWriter: CBCharacteristic?
5050
var pReader: CBCharacteristic?
5151
var pProduct: CBCharacteristic?
52+
53+
private var isPaired: Bool = false
5254

5355
// Peripherals in this set will not be auto-connected even if previously paired.
5456
// This is for failed connections to not enter an infinite connect loop.
@@ -66,7 +68,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
6668
}
6769

6870
func isConnected() -> Bool {
69-
return connectedPeripheral != nil && pReader != nil && pWriter != nil;
71+
return isPaired && connectedPeripheral != nil && pReader != nil && pWriter != nil;
7072
}
7173

7274
func connect(to peripheralID: UUID) {
@@ -100,13 +102,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
100102
restartScan()
101103
case .poweredOff, .unauthorized, .unsupported, .resetting, .unknown:
102104
print("BLE: unavailable or not supported")
103-
connectedPeripheral = nil
104-
pReader = nil
105-
pWriter = nil
106-
pProduct = nil
107-
state.discoveredPeripherals.removeAll()
108-
state.connecting = false
109-
updateBackendState()
105+
handleDisconnect()
110106
@unknown default:
111107
print("BLE: Unknown Bluetooth state")
112108
}
@@ -242,22 +238,36 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
242238
}
243239
if characteristic == pProduct {
244240
print("BLE: product changed: \(String(describing: parseProduct()))")
241+
// We can only read the product characteristic when paired.
242+
isPaired = true
245243
// Invoke device manager to scan now, which will make it detect the device being connected
246244
// (or disconnected, in case the product string indicates that) now instead of waiting for
247245
// the next scan.
248246
MobileserverUsbUpdate()
249247
}
250248
}
249+
250+
func handleDisconnect() {
251+
connectedPeripheral = nil
252+
pReader = nil
253+
pWriter = nil
254+
pProduct = nil
255+
state.discoveredPeripherals.removeAll()
256+
state.connecting = false
257+
isPaired = false
258+
updateBackendState()
259+
260+
// Have the backend scan right away, which will make it detect that we disconnected.
261+
// Otherwise there would be up to a second of delay (the backend device manager scan interval).
262+
MobileserverUsbUpdate()
263+
264+
restartScan()
265+
}
251266

252267
// This method gets called if the peripheral disconnects
253268
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
254269
print("BLE: peripheral disconnected")
255-
connectedPeripheral = nil;
256-
pReader = nil;
257-
pWriter = nil;
258-
pProduct = nil;
259-
260-
restartScan()
270+
handleDisconnect()
261271
}
262272

263273
func readBlocking(length: Int) -> Data? {

0 commit comments

Comments
 (0)