无法通过 BLE 连接到 AirPods
Posted
技术标签:
【中文标题】无法通过 BLE 连接到 AirPods【英文标题】:Can't connect to AirPods via BLE 【发布时间】:2018-05-21 14:39:19 【问题描述】:我正在尝试通过一个使用 BLE 的简单应用程序自动连接到我的 AirPods。我得到了设备的名称和“正在连接”的状态,但由于某种原因我无法连接到它。永远不会触发“didConnect 外围设备”函数。
我尝试了教程和其他帖子中的所有不同方法,尝试将外围数据存储在数组中以保留引用,但似乎没有任何效果。
是否有任何步骤可以让我在“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)
【问题讨论】:
你解决了这个问题吗?我有完全相同的问题:S 这真的很烦人 你尝试了哪些教程? 您是否尝试实现 didFailToConnect() 以获取更多信息? 我尝试通过应用商店的 nRFConnect APP 进行连接,但它无论如何都不起作用......似乎很特别 @Passe 是的,我做到了,但它从未到达那里 【参考方案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 service is powered on and scanning
found AirPods <CBPeripheral: 0x1c4103f00, identifier = 0E6FCF72-B86E-FB10-DD62-4A575BAD0ECC, name = AirPods de Iván, state = disconnected>
connect
这里发生了一些奇怪的事情我几乎可以肯定代码是正确的
【讨论】:
你解决了吗?我有一个奇怪的问题,如果我们暂停我们的应用程序,转到后台,然后返回,AirPods 上的音频播放就会停止。如果我们在进入后台(和返回)时继续播放,那很好。超级奇怪......当我发现你的帖子时,我正试图找到从后台明确重新连接到他们的方法......【参考方案2】:由于 AirPods 使用 kCBAdvDataIsConnectable = 0
做广告,因此它们不应该通过 BLE 连接。
【讨论】:
那么我们可以使用其他协议来连接airpods吗? @IvanCoronado 恐怕不是 iOS。很有可能,AirPods 正在使用蓝牙 (3.0) 音频配置文件的增强版本,它以及所有其他 BT 3.0 配置文件无法通过 iOS SDK 访问。以上是关于无法通过 BLE 连接到 AirPods的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 react-native-ble-manager 通过蓝牙连接到设备
无法通过 BLE Python 连接到 Arduino:[org.bluez.Error.Failed] 软件导致连接中止