【问题标题】:iOS 核心蓝牙:获取 API 误用警告
【发布时间】:2014-06-13 20:42:00
【问题描述】:

我正在使用 Core Bluetooth API 在 iOS 7 中编写一个测试应用程序。当我测试应用程序时,我发现我收到以下警告消息:

TestBluetooth[626:60b] CoreBluetooth[API MISUSE] 只能在开机状态下接受命令

后来我调试了应用程序,发现警告来自以下代码行:

[manager scanForPeripheralsWithServices:array options:scanOptions];

那么谁能告诉我为什么我会在控制台中收到此消息?

我周围有蓝牙 4.0 的安卓设备,但这个应用程序没有发现它们作为外围设备。那么为什么它没有发现蓝牙 4.0 LE Android 设备作为外围设备呢?

【问题讨论】:

标签: ios bluetooth bluetooth-lowenergy core-bluetooth ios7.1


【解决方案1】:

您必须等到[-CBCentralManagerDelegate centralManagerDidUpdateState:] 回调被调用。然后,在开始扫描外围设备之前验证状态是否为 PoweredOn

【讨论】:

【解决方案2】:

请使用以下代码解决警告:

(可以参考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;
        }
    }
}

【讨论】:

    【解决方案3】:

    蓝牙开机时进行扫描:

    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)
            }
        }
    

    【讨论】:

      【解决方案4】:

      只需打开我的 iphone 蓝牙即可解决我的问题。打开蓝牙后没有低于警告。 CoreBluetooth[API MISUSE] 只能在开机状态下接受命令

      【讨论】:

      猜你喜欢
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多