[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 デリゲートは次のとおりです。
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)
}
どういう意味ですか?