gpt4 book ai didi

swift - CoreBluetooth 只能在连接状态下接受命令

转载 作者:行者123 更新时间:2023-12-05 06:07:37 32 4
gpt4 key购买 nike

[CoreBluetooth] API MISUSE: <CBPeripheral: 0x281310820, identifier = 75450BB1-97A7-5648-38FD-B27572F19EDD, name = ESP32 test, state = disconnected> can only accept commands while in the connected state

这是 CoreBluetooth 在我尝试时给我的错误

peripheral.discoverServices([CBUUID(string: "4fafc201-1fb5-459e-8fcc-c5c9c331914b")])

有人知道发生了什么事吗?

这是 didDiscover 委托(delegate):

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
var peripheralName: String!
if let name = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
peripheralName = name
}
else {
peripheralName = "Unknown"
}

if(advertisementData[CBAdvertisementDataServiceUUIDsKey] != nil) {
//print(advertisementData)
let Ids: [CBUUID] = advertisementData[CBAdvertisementDataServiceUUIDsKey] as! [CBUUID]
if(Ids.count == 1) {
print(Ids[0])
let newPeripheral = Peripheral(id: peripherals.count, name: peripheralName, rssi: RSSI.intValue)
//print(newPeripheral)
/*
if let services = peripheral.services {
for service in services {
print("servizio")
//peripheral.discoverCharacteristics([CBUUID(string: "4fafc201-1fb5-459e-8fcc-c5c9c331914b")], for: service)
}
}*/
if(accepted_services.contains(Ids[0].uuidString)) {
print(newPeripheral)
peripherals.append(newPeripheral)
peripheral.delegate = self
peripheral.discoverServices([CBUUID(string: "4fafc201-1fb5-459e-8fcc-c5c9c331914b")])
//peripheral.discoverCharacteristics([CBUUID(string: "beb5483e-36e1-4688-b7f5-ea07361b26a8")], for: <#CBService#>)
}
}
}
}

编辑:我现在尝试先连接到外围设备,但出现另一个错误:

[CoreBluetooth] API MISUSE: Cancelling connection for unused peripheral <CBPeripheral: 0x2829e01e0, identifier = 75450BB1-97A7-5648-38FD-B27572F19EDD, name = ESP32 test, state = connecting>, Did you forget to keep a reference to it?

这是实际的代码:

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String:Any], rssi RSSI: NSNumber) {
print("scoperto peripheral")
myCentral.connect(peripheral, options: nil)
}

这是什么意思?

最佳答案

您负责在某处保存对 CBPeripheral 的引用。这就是消息告诉您的内容(“您是否忘记保留对它的引用?”)您正在创建一个 Peripheral(我假设它是一个自定义结构或类),但它只有名称,而不是 CBPeripheral。这意味着在 didDiscover 结束时,CBPeripheral 没有被任何东西引用并且它被释放了。

关于swift - CoreBluetooth 只能在连接状态下接受命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65370708/

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