SquareInc
SquareInc

Reputation: 81

CoreBluetooth can only accept commands while in the connected state

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

Upvotes: 4

Views: 2557

Answers (1)

Rob Napier
Rob Napier

Reputation: 299683

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.

Upvotes: 2

Related Questions