3

I created one iOS app which search nearest bluetooth device. But when I run this app on iPhone then below error occure

[CoreBluetooth] API MISUSE: can only accept this command while in the powered on state

What can I do to solve this error and why this error occurs in the app? thanks in advance

func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print(central.state)
    switch (central.state) {
    case CBManagerState.poweredOff:
       print("CoreBluetooth BLE hardware is powered off")
        break;
    case CBManagerState.unauthorized:
        print("CoreBluetooth BLE state is unauthorized")
        break

    case CBManagerState.unknown:
         print("CoreBluetooth BLE state is unknown");
        break

    case CBManagerState.poweredOn:
        print("CoreBluetooth BLE hardware is powered on and ready")
        bluetoothAvailable = true;
        break;

    case CBManagerState.resetting:
         print("CoreBluetooth BLE hardware is resetting")
        break;
    case CBManagerState.unsupported:
          print("CoreBluetooth BLE hardware is unsupported on this platform");
        break
    }
    if bluetoothAvailable == true
    {
        discoverDevices()
    }

}

func discoverDevices() {
    print("Discovering devices")
    manager?.scanForPeripherals(withServices: nil, options: nil)
    //centralManager.scanForPeripheralsWithServices(nil, options: nil)

}
mab
  • 195
  • 2
  • 9
  • possible duplication of https://stackoverflow.com/questions/23338767/ios-core-bluetooth-getting-api-misuse-warning – Taras Chernyshenko Mar 07 '18 at 10:43
  • You need to start the scan only when the `centralManagerDidUpdateState()` is called and the state is `poweredOn`. – Larme Mar 07 '18 at 10:43
  • Possible duplicate of [IOS Core Bluetooth : Getting API MISUSE Warning](https://stackoverflow.com/questions/23338767/ios-core-bluetooth-getting-api-misuse-warning) – Larme Mar 07 '18 at 10:44
  • @Larme But when I write a switch case then it get me powereoff in console. so how do I poweredOn? – mab Mar 07 '18 at 10:46
  • Are you testing on real device? Did you turn on yourself the Bluetooth ? – Larme Mar 07 '18 at 10:50
  • yes on iPhone se and turn device bluetooth myself – mab Mar 07 '18 at 10:51
  • @Larme I gave my centralManagerDidUpdateState() function code above please refer is and tell me where do I mistake in my code Now I got two output 1. CoreBluetooth BLE hardware is powered off 2[CoreBluetooth] API MISUSE: can only accept this command while in the powered on state – mab Mar 07 '18 at 10:59
  • Is `discoverDevices()` called even when it's poweredOff? Did you init `bluetoothAvailable` to false at least? – Larme Mar 07 '18 at 11:02
  • @Larme Yes I took var bluetoothAvailable = false above in a program But did not scan device give me above error. where do I make mistake :( I use bellow article for my bluetooth code as their is any mistake: http://make.analogfolk.com/getting-started-with-ios10-swift-and-bluetooth-4-0-for-wearables-hardware/ – mab Mar 07 '18 at 11:05

0 Answers0