gpt4 book ai didi

ios - iOS 上的蓝牙外设卡在 "Connecting"状态

转载 作者:行者123 更新时间:2023-11-29 00:34:55 25 4
gpt4 key购买 nike

我正在尝试连接到使用 BlueFruit BLE spi 模块的 Arduino 项目。我在尝试使用 iOS 应用程序进行连接时遇到问题。找到设备后,我尝试连接到它,但状态卡在“正在连接”状态=1。这阻止我搜索服务等,因为未实现“连接”状态这是一个代码片段...

//check state of the bluetooth on phone
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOff{
//TODO: ADD SAVE DATA TO REALM BEFORE DISSMISSING
errorView.isHidden = false
}
if central.state == .poweredOn{
errorView.isHidden = true
//scan for peripherals with the service i created
central.scanForPeripherals(withServices: nil, options: nil)
}
}

//devices found(should only be ours because we will create Unique serviceID)
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// get advertisement data and check to make sure the name is matching. set it as the peripheral then make connection
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print("NEXT PERIPHERAL NAME: \(peripheralName)")
print("NEXT PERIPHERAL UUID: \(peripheral.identifier.uuidString)")

if peripheralName == nameID{
manager.stopScan()
self.peripheralHalo = peripheral
peripheralHalo!.delegate = self
manager.connect(peripheral, options: nil)

while(peripheralHalo?.state.rawValue == 1)
{
if(manager.retrieveConnectedPeripherals(withServices: [serviceID]).count > 0 ){
print("\(manager.retrieveConnectedPeripherals(withServices: [serviceID]))")
}
}
}
print("Connected!!")
}

当我调用 manager.connect(peripheral, options: nil) 时,外围设备尝试连接。我添加以下 while 循环进行测试,并始终将状态显示为“正在连接”。我已经尝试了 LightBlue iOS 应用程序,我可以正确连接并接收特征值变化的通知,因此 Arduino 固件应该一切正常。请帮忙!!!

最佳答案

您不需要那个while 循环;这只会阻塞 Core Bluetooth 委托(delegate)线程。发出 connect 后,您将接到一个调用 didConnect CBCentralManagerDelegate 方法。连接外围设备后,您需要调用 discoverServices在外设上,它将回调到 peripheral:didDiscoverServices: 外设委托(delegate)方法。然后,您可以用类似的方式发现特征。

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// get advertisement data and check to make sure the name is matching. set it as the peripheral then make connection
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print("NEXT PERIPHERAL NAME: \(peripheralName)")
print("NEXT PERIPHERAL UUID: \(peripheral.identifier.uuidString)")

if peripheralName == nameID {
self.peripheralHalo = peripheral
central.stopScan()
central.connect(peripheral, options: nil)
}
}
}

func centralManager(_ central: CBCentralManager,
didConnect peripheral: CBPeripheral) {
print("Connected!!")
peripheralHalo!.delegate = self
peripheral.discoverServices([serviceID)
}

此外,如果您要存储一些东西来标识您要连接的外围设备,我建议您使用标识符而不是名称,因为名称可以更改。

关于ios - iOS 上的蓝牙外设卡在 "Connecting"状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40854413/

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