CoreBluetooth XPC Connection Invalid on dismiss viewcontroller

柔情痞子 提交于 2019-12-04 23:11:18

I placed CBCentralManager to a singleton and the error message is solved.

(CBCentralManager will not be deallocated)

I was getting the following message:

[CoreBlueooth] XPC Connection Invalid

And I wasn't able to scan BLE devices using a quite simple implementation of the following:

NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>

The solution for me was to add a value in my Info.plist for Privacy - Bluetooth Peripheral Usage Description NSBluetoothPeripheralUsageDescription describing what I do with Bluetooth Peripheral.

Looks like this in info.plist:

<key>NSBluetoothPeripheralUsageDescription</key>
<string>Play with BLE Compatible devices<string>

Write something more accurate in here ;)

CBCentralManager reference should be a strong reference to the class as a member variable. It cannot work as a local reference.

Garima Paliwal

try this:

CBPeripheral *mConnectedPeripheral;

-(void)viewDidDisappear:(BOOL)animated{
    [_centralManager cancelPeripheralConnection:mConnectedPeripheral];
}

Same issue happened when I moved all my BLE methods to dedicated class (BLEController) and keep ViewController clean. First I tried to initialize it inside ViewController class like this:

let _ = BLEController()

This leads to "XPC Connection invalid" issue. What really helped is to move the object to AppDelegate class. To be honest I have no idea why it helped and what is the difference.

Ok, I ran into that problem and after trying to add the necessary key to the Info.plist it still did worked and I had no view to use at this point (it was in the AppDelegate). So if it still don't work for you try the following.

I used to use: (in Swift)

_ = BluetoothMngr.init(config: bleConfig)

The problem here was that the variable managing the Bluetooth was not retained so when we add BLE callback these one ended up in an empty class, so simply create a global variable where it's gonna be retained (this is why it works with singleton and view these are retained) like this.

self.bleMngr = BluetoothMngr.init(config: bleConfig)

Worked for me, hope it will help.

In my case, I turned off app sandbox from capabilities, and it worked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!