如何用相同的UUID swift iOS来区分两个BLE特性之间的区别

时间:2017-11-01 14:19:53

标签: ios swift bluetooth-lowenergy

我有两个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)
    }
}

问题是没有办法区分不同外设的电池电量。该怎么做?

0 个答案:

没有答案