我有两个BLE外设,想要监控每个外围设备的电池电量。并在电池电量更新时通知它。
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
for service in peripheral.services! {
if service.uuid == DeviceService.BatteryUUID { // 180F is common to all devices
let theCharacteristic = [DeviceCharacteristics.AllBatteryUUID] // 2A19 common
peripheral.discoverCharacteristics(theCharacteristic, for: service)
}
}
}
public func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for characteristic in service.characteristics! {
if characteristic.uuid == DeviceCharacteristics.AllBatteryUUID {
self.activePeripheral?.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
}
}
public func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
// for battery level
if characteristic.uuid == DeviceCharacteristics.AllBatteryUUID {
self.delegate?.bLEManagerDidReceiveData(data: characteristic.value, char: characteristic)
}
}
问题是没有办法区分不同外设的电池电量。该怎么做?