polarofficial / polar-ble-sdk

Repository includes SDK and code examples. More info https://polar.com/en/developers
Other
449 stars 147 forks source link

[CoreBluetooth] API MISUSE: <CBCentralManager: 0x281a001e0> can only accept this command while in the powered on state #326

Closed FeherF closed 1 year ago

FeherF commented 1 year ago

Platform your question concerns:

Device:

I get the following error while I initialize the api variable:

[CoreBluetooth] API MISUSE: <CBCentralManager: 0x281a001e0> can only accept this command while in the powered on state

Here's the interested block of code:

@objc class PolarController: NSObject,
                    PolarBleApiObserver,
                    PolarBleApiPowerStateObserver,
                    PolarBleApiDeviceInfoObserver,
                    PolarBleApiDeviceFeaturesObserver,
                    PolarBleApiDeviceHrObserver {

    // Error on this line
    var api = PolarBleApiDefaultImpl.polarImplementation(DispatchQueue.main, features: Features.allFeatures.rawValue)

    @objc func initAndConnect() {
        api.polarFilter(false)
        api.observer = self
        api.deviceHrObserver = self
        api.powerStateObserver = self
        api.deviceFeaturesObserver = self
        api.deviceInfoObserver = self
        do {
            try self.api.connectToDevice(deviceID)
        } catch {
            print("Error occurred while connecting to device: \(error)")
        }
    }

Sometimes the error doesn't even shows up, but 8 times out of 10 it will.

I'm testing on iPhone 11 with iOS 16, using polar-ble-sdk 3.3.6v. Thanks in advance!

JOikarinen commented 1 year ago

Hi @FeherF,

you probably see the issue when launching your app using Xcode? The logs appear if relaunching the app and there was connection at the time of relaunch of the app. The origin of the problem is the Bluetooth State Preservation and Restoration, which was introduced in release 3.3.4. The Bluetooth State Preservation and Restoration will be removed in release 5.0.0 as it is not working properly.

JOikarinen commented 1 year ago

The Bluetooth State Preservation and Restoration is now removed in release 5.0.0-beta2

FeherF commented 1 year ago

Thank you very much!