53

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
  • 33,281
  • 23
  • 160
  • 191
Yogesh Kulkarni
  • 859
  • 1
  • 10
  • 19
  • 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
  • For anyone trying to solve the same problem for flutter (flutter_blue_plus package) looking for hints here, you need to set up a stream subscription for [bluetooth state](https://pub.dev/documentation/flutter_blue_plus/latest/flutter_blue_plus/BluetoothDevice/state.html) – Shawn Ashton Jun 30 '23 at 19:18

4 Answers4

83

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,168
  • 5
  • 43
  • 68
Etan
  • 17,014
  • 17
  • 89
  • 148
  • 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
4

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,168
  • 5
  • 43
  • 68
oOEric
  • 1,069
  • 1
  • 10
  • 25
3

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
  • 16,304
  • 7
  • 99
  • 130
-1

turn on iphone Bluetooth resolve my problem. after turn on bluetooth not getting below warning. CoreBluetooth[API MISUSE] can only accept commands while in the powered on state

Yogesh Rathore
  • 147
  • 1
  • 7
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30642714) – CodeChanger Dec 24 '21 at 06:52