【问题标题】:CoreBluetooth can only accept commands while in the connected stateCoreBluetooth 只能在连接状态下接受命令
【发布时间】:2021-03-29 21:56:59
【问题描述】:

[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 &lt;CBPeripheral: 0x2829e01e0, identifier = 75450BB1-97A7-5648-38FD-B27572F19EDD, name = ESP32 test, state = connecting&gt;, 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)
}

什么意思?

【问题讨论】:

标签: swift bluetooth core-bluetooth bluetooth-peripheral


【解决方案1】:

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

【讨论】:

  • 谢谢!现在似乎工作了。我在开始时添加了这个变量: public var strong_reference: [CBPeripheral] = [CBPeripheral]() 当我收到 CBPeripheral 我保存它。非常感谢。我不知道为什么它会起作用。现在它调用了 didDiscoverServices 和 didDiscoverCharacteristicsFor,但没有看到任何特征。那很奇怪。再次感谢!
  • 如果 Rob 的回答解决了您的问题,您应该考虑接受它。
猜你喜欢
  • 2020-05-18
  • 2020-11-02
  • 1970-01-01
  • 2014-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
  • 2012-11-25
相关资源
最近更新 更多