Can't connect to AirPods via BLE

China☆狼群 提交于 2019-12-03 11:26:38

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

  • I can connect other devices with this code
  • I can connect the airpods with my mac without problems with a similar code
  • I can't connect airpods with my iphone :S

Something strange happens here I am almost sure that the code is right

Since the AirPods advertise with kCBAdvDataIsConnectable = 0, they're not supposed to be connected via BLE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!