@@ -241,7 +241,6 @@ final class MiaoMiaoManager: NSObject, CBCentralManagerDelegate, CBPeripheralDel
241
241
242
242
override init ( ) {
243
243
super. init ( )
244
- // centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)
245
244
centralManager = CBCentralManager ( delegate: self , queue: nil , options: [ CBCentralManagerOptionShowPowerAlertKey: true , CBCentralManagerOptionRestoreIdentifierKey: " LibreMonitorCoreBluetoothRestaurationKeyString " ] )
246
245
}
247
246
@@ -266,8 +265,8 @@ final class MiaoMiaoManager: NSObject, CBCentralManagerDelegate, CBPeripheralDel
266
265
// First MiaoMiao: A44638E4-B70F-DBE7-A519-C4E4DFED2066
267
266
// Second MiaoMiao: 3DE41921-6747-CDFD-D169-06EE86A81847
268
267
os_log ( " Connect while state %{public}@ " , log: MiaoMiaoManager . bt_log, type: . default, String ( describing: state. rawValue) )
269
- // if let peripheral = peripheral {
270
- if let peripheral = peripheral, let identifier = UUID ( uuidString: " 3DE41921-6747-CDFD-D169-06EE86A81847 " ) , peripheral. identifier == identifier {
268
+ if let peripheral = peripheral {
269
+ // if let peripheral = peripheral, let identifier = UUID(uuidString: "3DE41921-6747-CDFD-D169-06EE86A81847"), peripheral.identifier == identifier {
271
270
peripheral. delegate = self
272
271
centralManager. stopScan ( )
273
272
centralManager. connect ( peripheral, options: nil )
@@ -314,43 +313,24 @@ final class MiaoMiaoManager: NSObject, CBCentralManagerDelegate, CBPeripheralDel
314
313
print ( peripheral. description)
315
314
if peripheral. state == . disconnecting || peripheral. state == . disconnected {
316
315
self . peripheral = peripheral
316
+ peripheral. delegate = self
317
317
self . state = . Disconnected
318
318
connect ( )
319
319
} else {
320
320
self . peripheral = peripheral
321
321
peripheral. delegate = self
322
322
self . state = . Connected
323
323
peripheral. discoverServices ( serviceUUIDs) // good practice to just discover the services, needed
324
-
325
- // peripheral should already be connected
326
324
}
327
325
}
328
326
} else {
329
327
switch central. state {
330
328
case . poweredOff, . resetting, . unauthorized, . unknown, . unsupported:
331
329
state = . Unassigned
332
- case . poweredOn:
333
- scanForMiaoMiao ( ) // power was switched on, while app is running -> reconnect.
330
+ case . poweredOn: // Scanning will not work in background with MiaoMiao (since MiaoMiao does not advertise its services)
331
+ scanForMiaoMiao ( ) // power was switched on, while app is running
334
332
}
335
333
}
336
- // switch central.state {
337
- // case .poweredOff, .resetting, .unauthorized, .unsupported:
338
- // state = .Unassigned
339
- // case .poweredOn, .unknown:
340
- // if let peripheral = dict[CBCentralManagerRestoredStatePeripheralsKey] as? CBPeripheral {
341
- // self.peripheral = peripheral
342
- // // If the peripheral already exists:
343
- // // if disconnected, connect to it. Otherwhise it is already connected: disconnect and reconnect to get through the whole chain of methods.
344
- // if peripheral.state == .disconnected || peripheral.state == .disconnecting {
345
- // connect()
346
- // } else {
347
- // centralManager.cancelPeripheralConnection(peripheral)
348
- // connect()
349
- // }
350
- // } else {
351
- // scanForMiaoMiao()
352
- // }
353
- // }
354
334
}
355
335
356
336
0 commit comments