3

[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

That's the error CoreBluetooth gives me when I try to

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

Does anyone has any clue about what's going on?

Here's the 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#>)
            }
        }
    }
}

EDIT: I now tried to connect first to the peripheral BUT there is another error:

[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?

this is the actual code:

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

What does it mean?

SquareInc
  • 61
  • 3
  • Does this answer your question? [iOS Core Bluetooth : Getting API MISUSE Warning](https://stackoverflow.com/questions/23338767/ios-core-bluetooth-getting-api-misuse-warning) – El Tomato Dec 19 '20 at 14:22
  • 1
    that's the power on state problem, when you try to connect while the bluetooth is off. Mine problem it's different – SquareInc Dec 19 '20 at 15:25

1 Answers1

1

You are responsible for holding a reference to the CBPeripheral somewhere. That's what the message is telling you ("Did you forget to keep a reference to it?") You're creating a Peripheral (which I assume is a custom struct or class), but it only has the name, not the CBPeripheral. That means that at the end of didDiscover, the CBPeripheral isn't being referenced by anything and it's released.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Thanks! Now seems to work. I added this variable at start: public var strong_reference: [CBPeripheral] = [CBPeripheral]() and when I receive the CBPeripheral I save it. Thanks a lot. I don't know why it does work. Now it calls the didDiscoverServices and the didDiscoverCharacteristicsFor, but doesn't see any characteristic. That's strange. Thanks again! – SquareInc Dec 19 '20 at 17:59
  • 1
    If Rob's answer solved your question you should consider accepting it. – RyuX51 Jul 21 '21 at 14:57