我是靠谱客的博主 想人陪大米,这篇文章主要介绍can only accept this command while in the powered on state(iOS蓝牙 打开app后的第一次扫描要扫描两次),现在分享给大家,希望可以做个参考。

第一次扫描后系统才去检查蓝牙状态,调用更新的代理方法

///开始扫描
- (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)

最后

以上就是想人陪大米最近收集整理的关于can only accept this command while in the powered on state(iOS蓝牙 打开app后的第一次扫描要扫描两次)的全部内容,更多相关can内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(183)

评论列表共有 0 条评论

立即
投稿
返回
顶部