无法通过BLE连接AirPods

15

我正在使用BLE通过一个简单的应用程序自动连接我的AirPods。我可以获取设备名称和状态为“connecting”,但由于某种原因,我无法连接它。功能'didConnect peripheral'从未被触发。

我尝试了教程和其他帖子中的所有不同方法,尝试将外围数据存储在数组中以保留引用,但似乎什么都没用。

在“didDiscover”和“didConnect”之间有什么额外的信息可以获得吗?

在iPhone上使用Xcode 9.2,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)
}

你修好了吗?我也遇到了完全相同的问题 :S 真的很烦人。 - Ivan Coronado
1
你尝试过实现 didFailToConnect() 来获取更多信息吗? - PascalS
我尝试从应用商店通过nRFConnect APP连接,但无论如何都无法连接...似乎有些特殊。 - PascalS
@Passe 我已经用另一台设备进行了测试,一切正常:S 在 Mac 上也可以正常工作,我可以毫无问题地连接 AirPods。 - Ivan Coronado
三年后仍然存在这个问题。它是随机的,可能在一个设备上工作但在另一个设备上却不行。 - erotsppa
显示剩余3条评论
2个回答

1

由于AirPods使用kCBAdvDataIsConnectable = 0进行广告宣传,因此不应通过BLE连接。


我们可以使用其他协议连接AirPods吗? - Ivan Coronado
@IvanCoronado,恐怕不行。AirPods可能正在使用增强版的蓝牙(3.0)音频配置文件,而所有其他BT 3.0配置文件都无法通过iOS SDK访问。 - DrMickeyLauer

1

这是我的当前实现:

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服务已启动并正在扫描 找到AirPods <CBPeripheral: 0x1c4103f00, identifier = 0E6FCF72-B86E-FB10-DD62-4A575BAD0ECC, name = AirPods de Iván, state = disconnected> 连接
  • 我可以使用此代码连接其他设备
  • 我可以使用类似的代码在我的Mac上轻松连接AirPods
  • 我无法连接iPhone上的AirPods :S

这里发生了一些奇怪的事情,我几乎确定代码是正确的


你解决了吗?我遇到了一个奇怪的问题,当我们暂停应用程序、进入后台并返回时,AirPods上的音频播放会停止。如果我们在进入后台时继续播放,那就没问题了。非常奇怪...我一直在尝试找到从后台返回时显式重新连接它们的方法,当我看到你的帖子时... - jbm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接