Remove "Characteristic already notifying" error (#110)

This commit is contained in:
Rohit Sangwan
2024-12-09 18:48:18 +05:30
committed by GitHub
parent 9acb13f106
commit 5be22ac0e6
2 changed files with 24 additions and 28 deletions
@@ -199,19 +199,23 @@ class UniversalBleLinux extends UniversalBlePlatform {
@override
Future<void> setNotifiable(String deviceId, String service,
String characteristic, BleInputProperty bleInputProperty) async {
final key = "$deviceId-$service-$characteristic";
final char = _getCharacteristic(deviceId, service, characteristic);
String characteristicKey = "${deviceId}_${service}_$characteristic";
if (bleInputProperty != BleInputProperty.disabled) {
if (char.notifying) throw Exception('Characteristic already notifying');
if (char.notifying) {
UniversalLogger.logInfo('$characteristic already notifying');
return;
}
await char.startNotify();
if (_characteristicPropertiesSubscriptions[key] != null) {
_characteristicPropertiesSubscriptions[key]?.cancel();
if (_characteristicPropertiesSubscriptions[characteristicKey] != null) {
_characteristicPropertiesSubscriptions[characteristicKey]?.cancel();
}
_characteristicPropertiesSubscriptions[key] =
_characteristicPropertiesSubscriptions[characteristicKey] =
char.propertiesChanged.listen((List<String> properties) {
for (String property in properties) {
switch (property) {
@@ -230,9 +234,10 @@ class UniversalBleLinux extends UniversalBlePlatform {
}
});
} else {
if (!char.notifying) throw Exception('Characteristic not notifying');
await char.stopNotify();
_characteristicPropertiesSubscriptions.remove(key)?.cancel();
if (char.notifying) await char.stopNotify();
_characteristicPropertiesSubscriptions
.remove(characteristicKey)
?.cancel();
}
}
@@ -158,31 +158,22 @@ class UniversalBleWeb extends UniversalBlePlatform {
String characteristicKey = "${deviceId}_${service}_$characteristic";
if (bleInputProperty == BleInputProperty.notification ||
bleInputProperty == BleInputProperty.indication) {
if (bleCharacteristic.isNotifying) {
throw Exception("Already listening to this characteristic");
}
if (bleInputProperty != BleInputProperty.disabled) {
if (_characteristicStreamList[characteristicKey] != null) {
_characteristicStreamList[characteristicKey]?.cancel();
}
await bleCharacteristic.startNotifications();
_characteristicStreamList[characteristicKey] = bleCharacteristic.value
.map((event) => event.buffer.asUint8List())
.listen((event) {
updateCharacteristicValue(deviceId, characteristic, event);
_characteristicStreamList[characteristicKey] =
bleCharacteristic.value.listen((ByteData event) {
updateCharacteristicValue(
deviceId,
characteristic,
event.buffer.asUint8List(),
);
});
}
// Cancel Notification
else if (bleInputProperty == BleInputProperty.disabled) {
} else {
await bleCharacteristic.stopNotifications();
_characteristicStreamList.removeWhere((key, value) {
if (key == characteristicKey) value.cancel();
return key == characteristicKey;
});
_characteristicStreamList.remove(characteristicKey)?.cancel();
}
}