@@ -49,6 +49,8 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
49
49
var pWriter : CBCharacteristic ?
50
50
var pReader : CBCharacteristic ?
51
51
var pProduct : CBCharacteristic ?
52
+
53
+ private var isPaired : Bool = false
52
54
53
55
// Peripherals in this set will not be auto-connected even if previously paired.
54
56
// This is for failed connections to not enter an infinite connect loop.
@@ -66,7 +68,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
66
68
}
67
69
68
70
func isConnected( ) -> Bool {
69
- return connectedPeripheral != nil && pReader != nil && pWriter != nil ;
71
+ return isPaired && connectedPeripheral != nil && pReader != nil && pWriter != nil ;
70
72
}
71
73
72
74
func connect( to peripheralID: UUID ) {
@@ -100,13 +102,7 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
100
102
restartScan ( )
101
103
case . poweredOff, . unauthorized, . unsupported, . resetting, . unknown:
102
104
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 ( )
110
106
@unknown default :
111
107
print ( " BLE: Unknown Bluetooth state " )
112
108
}
@@ -242,22 +238,36 @@ class BluetoothManager: NSObject, ObservableObject, CBCentralManagerDelegate, CB
242
238
}
243
239
if characteristic == pProduct {
244
240
print ( " BLE: product changed: \( String ( describing: parseProduct ( ) ) ) " )
241
+ // We can only read the product characteristic when paired.
242
+ isPaired = true
245
243
// Invoke device manager to scan now, which will make it detect the device being connected
246
244
// (or disconnected, in case the product string indicates that) now instead of waiting for
247
245
// the next scan.
248
246
MobileserverUsbUpdate ( )
249
247
}
250
248
}
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
+ }
251
266
252
267
// This method gets called if the peripheral disconnects
253
268
func centralManager( _ central: CBCentralManager , didDisconnectPeripheral peripheral: CBPeripheral , error: Error ? ) {
254
269
print ( " BLE: peripheral disconnected " )
255
- connectedPeripheral = nil ;
256
- pReader = nil ;
257
- pWriter = nil ;
258
- pProduct = nil ;
259
-
260
- restartScan ( )
270
+ handleDisconnect ( )
261
271
}
262
272
263
273
func readBlocking( length: Int ) -> Data ? {
0 commit comments