如何使用Swift中的CoreBluetooth更新BLE设备中的电池电量?

时间:2017-08-18 07:13:50

标签: ios swift bluetooth core-bluetooth

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)")

    }
}

我可以读取电池电量值,但是当值改变时如何更新电池电量?

为什么peripheral.setNotifyValue(true, for: c)无法更新?

0 个答案:

没有答案