iOS Core Bluetooth : Getting API MISUSE Warning
You have to wait until the [-CBCentralManagerDelegate centralManagerDidUpdateState:]
callback has been called. And then, verify that the state is PoweredOn
before you start scanning for peripherals.
Please use the following code to solve the warning:
(You can reference to the code in https://github.com/luoxubin/BlueTooth4.0)
if (bluetoothPowerOn) { [self.centralManager scanForPeripheralsWithServices:[serviceIDs copy] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@(NO)}];}-(void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) { case CBManagerStatePoweredOn: { bluetoothPowerOn = YES; //new code [self start]; break; } default: { bluetoothPowerOn = NO; //new code [self stopScan:[NSError hardwareStatusErrorWithMessage:@"Cannot open Bluetooth, please check the setting." hardwareStatus:central.state]]; break; } }}
Do scan when bluetooth is poweredOn:
func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { case .unknown: print("unknown") case .resetting: print("resetting") case .unsupported: print("unsupported") case .unauthorized: print("unauthorized") case .poweredOff: print("poweredOff") centralManager?.stopScan() case .poweredOn: print("poweredOn") centralManager?.scanForPeripherals(withServices: nil, options: nil) } }