49

I am writing a test app in iOS 7 with the Core Bluetooth API. When I am testing the application I found that I am getting the following warning message:

TestBluetooth[626:60b] CoreBluetooth[API MISUSE] can only accept commands while in the powered on state

Later I debugged app and found that, warning is coming from the following line of code:

[manager scanForPeripheralsWithServices:array options:scanOptions];

So can anyone please tell me why I am getting this message in the console?

There are bluetooth 4.0 android devices around me, but this app is not discovering them as peripheral device. So why it is not discovering bluetooth 4.0 LE Android devices as peripherals?

pkamb
  • 26,648
  • 20
  • 124
  • 157
Yogesh Kulkarni
  • 807
  • 1
  • 10
  • 18
  • 2
    You need to check the `CBCentralManager` `state` property before: https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManager_Class/translated_content/CBCentralManager.html#//apple_ref/occ/instp/CBCentralManager/state – Larme Apr 28 '14 at 10:43

3 Answers3

77

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.

viral
  • 4,108
  • 4
  • 38
  • 68
Etan
  • 15,505
  • 17
  • 83
  • 142
  • 1
    I've created a tool to help debug the API Misuse errors: https://github.com/nrbrook/NBCoreBluetoothAPIMisuseGuard – Nick May 03 '16 at 16:30
3

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;
        }
    }
}
viral
  • 4,108
  • 4
  • 38
  • 68
oOEric
  • 959
  • 1
  • 9
  • 21
1

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)
        }
    }
Mohammad Zaid Pathan
  • 14,352
  • 6
  • 84
  • 112