How to update Battery Level in BLE Device with Cor

2020-06-22 01:34发布

问题:

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {

        for c in service.characteristics!{
            print("---Characteristic found with UUID: \(c.uuid) \n")

            let uuid = CBUUID(string: "2A19")//Battery Level

            if c.uuid == uuid{
                //peripheral.setNotifyValue(true, for: c)//Battery Level
                peripheral.readValue(for: c)
            }
        }
    }
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
/* Battery Level */
    if (characteristic.uuid == CBUUID(string: "2A19")) && (characteristic.value != nil){
        let value = characteristic.value
        let valueUint8 = [UInt8](value!)
        print("\(valueUint8)")
        print("\(valueUint8[0])")
        let batteryLevel: Int32 = Int32(bitPattern: UInt32(valueUint8[0]))
        print("\(batteryLevel)")

    }
}

I can read Battery Level value, but how to update Battery Level when the value changed?

Why peripheral.setNotifyValue(true, for: c) can't update?