XPC Error CoreBluetooth

Hi,
I'm currently looking into CoreBluetooth, but I'm already struggeling with the simple setup.
On starting the test app github I receive the below error message:

Search not started as central isn't powered on! unknown
2016-08-12 20:37:18.050489 SwiftBluetooth[1865:574397] [CoreBluetooth] XPC connection invalid

This is happening while I had my bluetooth headphones sucessfully connected to the iPhone.
So I would expect that at least the headphones get recognised.


Device: iPhone 6
OS: 10.0


Thanks for your help
Timo

Replies

I'm getting the same issue on iOS 10 and an iPhone 6s. Was there any solutions found to this?

I'm seeing the same error (below) deploying a similar Bluetooth test app from Xcode 8 to an iPhone 6 running iOS 10. I would be interested to know if you found a solution.


[CoreBluetooth] XPC connection invalid

Looking at the logs on the phone, I see the following entries from the BTServer process just before the app gets the above error:


Sending 'session attached' event for session "MyTestApp-central-541-0"

Registering central session "MyTestApp-central-541-0" with backgrounding: on, persistence: off

Sending 'state updated' event with state "On" to session "MyTestApp-central-541-0"

Sending XPC message "CBMsgIdStateUpdated" to session "MyTestApp-central-541-0"

Received XPC message "CBMsgIdCheckOut" from session "MyTestApp-central-541-0"

Unregistering central session "MyTestApp-central-541-0"

Unregistering session "MyTestApp-central-541-0"

Sending 'session detached' event for session "MyTestApp-central-541-0"

Sending XPC message "CBMsgIdFinalized" to session "MyTestApp-central-541-0"

I think the problem might be that the instance of your Bluetooth() class created in ViewController.viewDidLoad() is going out of scope just as CoreBluetooth is initializing in the background (asynchronously). Try adding a property like this at the top of ViewController:


let bluetooth = Bluetooth()


Then in viewDidLoad() call your connect() function on the instance.

  • This worked for me.

    Additional info:

    The "XPC Invalid" started showing up after I moved the Bluetooth stuff from the main view controller to a separate Bluetooth class. In my main view controller, I added your suggestion at the top <pre> let bluetooth = BLE() </pre> I also moved these lines from the bluetooth class to the main view controller : <pre> bluetooth.centralManager = CBCentralManager(delegate: bluetooth, queue: nil) bluetooth.discover() // start scanning </pre> Thank you!

Add a Comment

Thanks, it is the perfect solution, i found in internet. Thanks again

I don't understand your solution.


I’m struggle with the following Swift code for Mac OS.

If I add the line "centralManager = CBCentralManager(delegate: self, queue: nil)” and run the code I get the error XPC connection invalid.

The code so far:

import Cocoa

import CoreBluetooth

class ViewController: NSViewController, CBCentralManagerDelegate, CBPeripheralDelegate {

func centralManagerDidUpdateState(_ central: CBCentralManager) {

if central.state == CBManagerState.poweredOn {

print("Bluetooth Enabled")

} else {

print("Bluetooth Disabled")

}

}


var centralManager: CBCentralManager!

var RSSIs = [NSNumber]()

var data = NSMutableData()

var writeData: String = ""

var peripherals: [CBPeripheral] = []

var characteristicValue = [CBUUID: NSData]()

var timer = Timer()

var characteristics = [String : CBCharacteristic]()


override func viewDidLoad() {

super.viewDidLoad()

centralManager = CBCentralManager(delegate: self, queue: nil)

}

}


I have:

MacBook

XCode 9.2

Swift 4.0

Mac OS 10.13.2

If I write the same code for iOS it works fine.

Can you help me out? pls?


John

To be complete I tried the following but it gives the same error:


import Foundation

import CoreBluetooth

class BlueToothModel:NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {


func centralManagerDidUpdateState(_ central: CBCentralManager) {


}


var centralManager : CBCentralManager!

var RSSIs = [NSNumber]()

var data = NSMutableData()

var writeData: String = ""

var peripherals: [CBPeripheral] = []

var characteristicValue = [CBUUID: NSData]()

var timer = Timer()

var characteristics = [String : CBCharacteristic]()


func initBluetooth() {

centralManager = CBCentralManager(delegate: self, queue: nil, options: nil)

}

}



import Cocoa

class ViewController: NSViewController {


let bluetooth = BlueToothModel()


override func viewDidLoad() {

super.viewDidLoad()

bluetooth.initBluetooth()

}

}

I solved the problem.

There are differences between iOs and macOS, because I did not write a program for macOS for several years I was a bit rusty.

If you startup a new project in macOS, Sandbox is by default ON. ( no Sandbox in iOS). If Sandbox is on and Bluetooth is not activated in Sandbox Bluetooth will never be activated and the error "XPC connection invalid" will appear.


In iOS you must set in your info.plist the “Private Bluetooth Peripheral Usage" but in macOS it is not required.

I also get the hint to add the corebluetooth.framework to my macOS project but that was not the solution, I keep it in but without it it works to.

The only thing that I was missing was to activate Bluetooth in Sandbox or to switch Sandbox Off.

I have seen a few time a line with ( put this in your info.plist.)

never been able to do it :-)


on this page

https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothOverview/CoreBluetoothOverview.html

"Important: An iOS app linked on or after iOS 10.0 must include in its

Info.plist
file the usage description keys for the types of data it needs to access or it will crash. To access Bluetooth peripheral data specifically, it must include NSBluetoothPeripheralUsageDescription."


can you help me to ad thisto my info.plist ?

after reaing your post

"In iOS you must set in your info.plist the “Private Bluetooth Peripheral Usage" but in macOS it is not required".

i was able to add a line in my info.pllst. very happy about that


the line seem to be

Privacy - Bluetooth Peripheral Usage Description


sadly i still get the same error

Turning bluetooth On on Capabilities solved the issue.

I have developed a few apps using bluetooth and something i keep forgetting is the capabilities in xcode. I have mostly done it in flutter.

  1. When opening your target "Runner" go to signing and capabilities.
  2. Click on the "+ capabilities" and look for "background execution modes"
  3. When using BLE go select the bluetooth le... checkbox.
  4. clean build folder and restart xcode or the ide you are using and go rebuild ios. REBUILD EVERYTHING.

Assuming, you also have the correct permissions in the info.plist.

additionally bluetooth requires location too so select accordingly.