简体   繁体   English

无法通过BLE连接到AirPods

[英]Can't connect to AirPods via BLE

I am trying to connect automatically to my AirPods with a simple app using BLE. 我正在尝试使用BLE通过一个简单的应用程序自动连接到我的AirPods。 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. 函数'didConnect peripheral'永远不会被触发。

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. 我尝试了教程和其他帖子中的所有不同方法,尝试将外围数据存储在数组中以保留引用,但似乎没有任何工作。

Is there any step which I can get some extra info between 'didDiscover' and 'didConnect'? 有什么步骤我可以在'didDiscover'和'didConnect'之间获得一些额外的信息吗?

Working in XCode 9.2, using Swift 4 and iOS 11.2 on iPhone. 在XCode 9.2中工作,在iPhone上使用Swift 4和iOS 11.2。

Here's my code: 这是我的代码:

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

This one is my current implementation: 这是我目前的实施:

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

I am finding the device but when I try to connect, nothing happens 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 我找到了设备但是当我尝试连接时,没有任何事情发生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

  • I can connect other devices with this code 我可以使用此代码连接其他设备
  • I can connect the airpods with my mac without problems with a similar code 我可以用我的mac连接airpods而没有类似代码的问题
  • I can't connect airpods with my iphone :S 我无法连接airpods和我的iphone:S

Something strange happens here I am almost sure that the code is right 这里发生了一些奇怪的事情我几乎可以肯定代码是对的

由于AirPods使用kCBAdvDataIsConnectable = 0广告,因此它们不应通过BLE连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM