第一次掃描后系統才去檢查藍牙狀態,呼叫更新的代理方法
///開始掃描
- (void)startScan: (void(^)(CBPeripheral *))update{
//1.掃描外設 Services: 掃描指定服務的外設 options: 設定 為nil表示默認設定
#pragma mark - 第一步 掃描外設 scanForPeripherals
[self.cbCentralManager scanForPeripheralsWithServices:nil options:nil];
if (self.cbCentralManager.state == CBManagerStateUnknown) {
_isStateUnknown = YES;
}
//保存掃描回呼 self.scanBlock =
self.scanBlock = update;
}
//藍牙狀態更新CBCentrlManager.state
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSLog(@"%zd",central.state);
[self isBluetoothAvailabel];
//如果是打開app后第一次掃描,則需要在確定藍牙狀態是CBManagerStatePoweredOn后再掃描一次
if (_isStateUnknown) {
[self.cbCentralManager scanForPeripheralsWithServices:nil options:nil];
}
}
下面的博客是對我在github上上傳的藍牙工具類的介紹,這個工具類兼容oc和swift, 上面的代碼摘自這個工具類
參考博客:
iOS(oc swift)藍牙框架(工具類 demo)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/203853.html
標籤:其他
