无法通过BLE连接到AirPods

我正在尝试使用BLE通过一个简单的应用程序自动连接到我的AirPods。 我得到设备的名称和状态为“连接”,但由于某种原因,我无法连接到它。 函数’didConnect peripheral’永远不会被触发。

我尝试了教程和其他post中的所有不同方法,尝试将外围数据存储在一个数组中以保留引用,但似乎没有任何工作。

有什么步骤我可以在’didDiscover’和’didConnect’之间获得一些额外的信息吗?

在XCode 9.2中工作,在iPhone上使用Swift 4和iOS 11.2。

这是我的代码:

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) } 

这是我目前的实施:

 import UIKit import CoreBluetooth var manager: CBCentralManager! var peripheralBLE: CBPeripheral! class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate { let deviceName = "AirPods de Iván" var isConnected = false @IBOutlet weak var Label: UILabel! @IBAction func Click(_ sender: UIButton) { self.connect() } override func viewDidLoad() { super.viewDidLoad() manager = CBCentralManager(delegate: self, queue: nil) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func connect() { manager.connect(peripheralBLE, options: nil) print("connect") self.updateLabelStatus() } func disconnect() { manager.cancelPeripheralConnection(peripheralBLE!) print("disconnect") self.updateLabelStatus() } func updateLabelStatus() { switch peripheralBLE.state { case.connected: Label.text = "connected" case.disconnected: Label.text = "disconnected" case.connecting: Label.text = "connecting" case.disconnecting: Label.text = "disconnecting" default: Label.text = "label" } } 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 { print("found AirPods \(peripheral)") peripheralBLE = peripheral peripheralBLE!.delegate = self manager.stopScan() self.updateLabelStatus() } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { print("AirPods Connected") peripheral.discoverServices(nil) self.updateLabelStatus() } func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { print("AirPods Connect error") print(error) self.updateLabelStatus() } } 

我找到了设备但是当我尝试连接时,没有任何事情发生BLE service is powered on and scanning found AirPods connect

  • 我可以使用此代码连接其他设备
  • 我可以用我的mac连接airpods而没有类似代码的问题
  • 我无法连接airpods和我的iphone:S

这里发生了一些奇怪的事情我几乎可以肯定代码是对的