从 iOS 13 开始,应该可以注册 BR/EDR 蓝牙(经典)设备的连接事件。我已按照中提供的说明进行操作全球开发者大会主题演讲 https://developer.apple.com/videos/play/wwdc2019/901/,特别是我以这种方式初始化我的 CBCentralManager:
func initCentralManager() {
if self.centralManager == nil{
self.centralManager = CBCentralManager.init(delegate: self, queue: nil, options:[CBCentralManagerOptionRestoreIdentifierKey:"myCentralManagerIdentifier"]);
if #available(iOS 13.0, *) {
let matchingOptions = [CBConnectionEventMatchingOption.serviceUUIDs:[CBUUID(string: "1108"),
CBUUID(string: "110A"),
CBUUID(string: "110B"),
CBUUID(string: "110C"),
CBUUID(string: "110D"),
CBUUID(string: "110E"),
CBUUID(string: "110F"),
CBUUID(string: "111F"),
CBUUID(string: "1203"),
CBUUID(string: "1204"),
CBUUID(string: "111E"),
CBUUID(string: "0017"),
CBUUID(string: "0019")]];
print("REGISTERING FOR BR/EDR CONNECTION EVENTS");
self.centralManager!.registerForConnectionEvents(options: matchingOptions);
} else {
// Fallback on earlier versions
};
}
}
然后我实现回调:
func centralManager(_ central: CBCentralManager, connectionEventDidOccur event: CBConnectionEvent, for peripheral: CBPeripheral) {
print("BR/EDR CONNECTED!!!!");
}
正如 WWDC 视频中所建议的,我将进入手机设置,并且可以成功配对我的 BR/EDR 蓝牙设备(蓝牙音频盒),但是回调不会被触发。
我不确切知道我的蓝牙经典设备使用的 UUID,因此我已将我找到的所有与音频相关的 UUID 放入列表中here https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/.
UUID 的形式是否正确?我还尝试使用长符号(例如 0000 1108-0000-1000-8000-00805F9B34FB)添加它们,但没有任何反应,当设备在蓝牙设置中连接时,回调仍然不会触发。
如何获取我的设备公布的 UUID?
先感谢您