无法通过BLE连接到AirPods [英] Can't connect to AirPods via BLE
问题描述
我正在尝试使用BLE使用一个简单的应用程序自动连接到我的AirPods.我得到了设备的名称和状态为正在连接",但是由于某种原因我无法连接到它.永远不会触发功能"didConnect外设".
I am trying to connect automatically to my AirPods with a simple app using BLE. I get the name of the device and the status to "connecting", but for some reason I can't connect to it. Function 'didConnect peripheral' is never triggered.
我尝试了教程和其他文章中提出的所有不同方法,试图将外围数据存储在数组中以保留引用,但似乎无济于事.
I tried all different approaches from tutorials and from other posts, tried to store the peripheral data in an array to keep the reference but nothing seem to work.
在"didDiscover"和"didConnect"之间,有没有什么步骤可以使我获得一些额外的信息?
Is there any step which I can get some extra info between 'didDiscover' and 'didConnect'?
在iPhone上使用Swift 4和iOS 11.2在XCode 9.2中工作.
Working in XCode 9.2, using Swift 4 and iOS 11.2 on iPhone.
这是我的代码:
let deviceName = "AirPods de Roger"
var isConnected = false
var manager: CBCentralManager!
var peripheralBLE: CBPeripheral?
override func viewDidLoad() {
super.viewDidLoad()
manager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch manager.state {
case.poweredOff:
print("BLE service is powered off")
case.poweredOn:
print("BLE service is powered on and scanning")
manager.scanForPeripherals(withServices: nil, options: nil)
default:
print("BLE service in another state")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if peripheral.name == deviceName && isConnected == false {
self.manager.stopScan()
self.peripheralBLE = peripheral
self.peripheralBLE?.delegate = self
manager.connect(peripheral, options: nil)
isConnected = true
print("\(peripheral.name) pre-connected")
}
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
lblConnected.isHidden = false
print("AirPods Connected")
peripheral.discoverServices(nil)
}
推荐答案
由于AirPods使用kCBAdvDataIsConnectable = 0
做广告,因此不应将它们通过BLE连接.
Since the AirPods advertise with kCBAdvDataIsConnectable = 0
, they're not supposed to be connected via BLE.
这篇关于无法通过BLE连接到AirPods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!