iOS核心蓝牙:获取API误用警告

mkh04yzy  于 2023-06-25  发布在  iOS
关注(0)|答案(4)|浏览(173)

我正在使用Core Bluetooth API在iOS 7中编写一个测试应用程序。当我测试应用程序时,我发现我收到了以下警告消息:
TestBluetooth[626:60b] CoreBluetooth[API MISUSE]只能在通电状态下接受命令
后来我调试了应用程序,发现警告来自以下代码行:

[manager scanForPeripheralsWithServices:array options:scanOptions];

那么,有没有人能告诉我,为什么我在控制台上看到这个消息?
我身边有蓝牙4. 0 Android设备,但这个应用程序并没有发现它们作为外围设备。那么为什么它没有发现蓝牙4.0 LE Android设备作为外设?

a9wyjsp7

a9wyjsp71#

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

vwhgwdsa

vwhgwdsa2#

请使用以下代码解决警告:
(You可以引用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;
        }
    }
}
nmpmafwu

nmpmafwu3#

蓝牙开机时进行扫描:

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

7d7tgy0s4#

打开iPhone蓝牙解决我的问题.打开蓝牙后没有低于警告。CoreBluetooth[API MISUSE]只能在通电状态下接受命令

相关问题