How to update Battery Level in BLE Device with CoreBluetooth in Swift?

元气小坏坏 提交于 2020-05-25 08:06:21

问题


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?

来源:https://stackoverflow.com/questions/45750632/how-to-update-battery-level-in-ble-device-with-corebluetooth-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!