After I have finished disconnecting from my bluetooth devices, seeing that they have disconnected in the didDisconnectPeripheral delegate, I attempt to dismiss my viewcontroller.
When this happens I see the message: "[CoreBlueooth] XPC Connection Invalid"
Is there something in specific that has to be cleaned up with Bluetooth before the viewcontroller is dismissed?
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.
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