gpt4 book ai didi

ios - 无法通过 BLE 连接到 AirPods

转载 作者:IT王子 更新时间:2023-10-29 05:32:16 25 4
gpt4 key购买 nike

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

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

在“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 <CBPeripheral: 0x1c4103f00, identifier = 0E6FCF72-B86E-FB10-DD62-4A575BAD0ECC, name = AirPods de Iván, state = disconnected>
connect

  • 我可以用这个代码连接其他设备
  • 我可以使用类似的代码毫无问题地将 airpods 连接到我的 mac
  • 我无法将 airpods 连接到我的 iphone :S

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

关于ios - 无法通过 BLE 连接到 AirPods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47695912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com