Closed mortenboye closed 1 month ago
PR Description updated to latest commit (https://github.com/Navideck/universal_ble/commit/ed6809c6de8b622fb1332301d8f4b1a1cddf0775)
⏱️ Estimated effort to review [1-5] | 2, because the changes are focused on a specific functionality (characteristic subscription keying) and involve a moderate amount of code. The logic is straightforward and mainly revolves around the generation and use of a new key format for characteristic subscriptions. The changes are localized and do not seem to affect other parts of the system, making the review process less complex. |
🧪 Relevant tests | No |
🔍 Possible issues | Possible Bug: The PR assumes that the combination of `deviceId`, `service`, and `characteristic` will always uniquely identify a subscription. While this is likely true, it's important to ensure that there are no edge cases where this assumption might fail, especially in environments with many devices and services. |
Error Handling: The PR throws generic exceptions for cases where a characteristic is already notifying or not notifying. It might be beneficial to provide more specific error messages or custom exception types to help with debugging and error handling in client code. | |
🔒 Security concerns | No |
Category | Suggestions |
Enhancement |
Improve key generation for map access to enhance readability and avoid potential collisions.___ **Consider using a more structured approach to generate the key for_characteristicPropertiesSubscriptions to ensure uniqueness and readability. For instance, you could use a tuple or a custom object as the key if the map supports it, or alternatively, create a method that generates a standardized key format. This approach can help avoid potential key collisions and improve code maintainability.** [lib/src/universal_ble_linux/universal_ble_linux.dart [151]](https://github.com/Navideck/universal_ble/pull/35/files#diff-bd61ea0530e3e409336cd7d8a6d93c00911618355a9bf99fd1cf3d92cb6a02d9R151-R151) ```diff -var key = "$deviceId-$service-$characteristic"; +var key = generateKey(deviceId, service, characteristic); +// Elsewhere in your code +String generateKey(String deviceId, String service, String characteristic) { + return "$deviceId-$service-$characteristic"; +} ``` |
Enhance log messages with more context for better debugging.___ **For logging unhandled characteristic value property changes, consider adding more contextto the log message, such as the device ID, service, and characteristic. This can help in debugging by providing more information about where the unhandled property change occurred.** [lib/src/universal_ble_linux/universal_ble_linux.dart [174]](https://github.com/Navideck/universal_ble/pull/35/files#diff-bd61ea0530e3e409336cd7d8a6d93c00911618355a9bf99fd1cf3d92cb6a02d9R174-R174) ```diff -UniversalBlePlatform.logInfo("UnhandledCharValuePropertyChange: $property"); +UniversalBlePlatform.logInfo("UnhandledCharValuePropertyChange: $property for device $deviceId, service $service, characteristic $characteristic"); ``` | |
Possible issue |
Add null check for
___
**It's a good practice to check if the |
Best practice |
Use specific exception types for better error context and handling.___ **Instead of throwing a genericException when a characteristic is already notifying or not notifying, consider using a more specific exception type or creating a custom exception class. This can provide more context to the exception and make error handling more precise.** [lib/src/universal_ble_linux/universal_ble_linux.dart [155-179]](https://github.com/Navideck/universal_ble/pull/35/files#diff-bd61ea0530e3e409336cd7d8a6d93c00911618355a9bf99fd1cf3d92cb6a02d9R155-R179) ```diff -if (char.notifying) throw Exception('Characteristic already notifying'); +if (char.notifying) throw CharacteristicAlreadyNotifyingException(); ... -if (!char.notifying) throw Exception('Characteristic not notifying'); +if (!char.notifying) throw CharacteristicNotNotifyingException(); +// Define these exceptions somewhere in your code +class CharacteristicAlreadyNotifyingException implements Exception { + @override + String toString() => 'Characteristic is already notifying'; +} +class CharacteristicNotNotifyingException implements Exception { + @override + String toString() => 'Characteristic is not notifying'; +} ``` |
Ensure proper cleanup of cancelled subscriptions to avoid memory leaks.___ **When cancelling subscriptions, it's a good practice to also set the corresponding mapentry to null or remove it entirely after cancellation. This helps in avoiding memory leaks and ensures that the map does not hold onto cancelled subscriptions, which are no longer needed.** [lib/src/universal_ble_linux/universal_ble_linux.dart [160-181]](https://github.com/Navideck/universal_ble/pull/35/files#diff-bd61ea0530e3e409336cd7d8a6d93c00911618355a9bf99fd1cf3d92cb6a02d9R160-R181) ```diff -_characteristicPropertiesSubscriptions[key]?.cancel(); +await _characteristicPropertiesSubscriptions[key]?.cancel(); +_characteristicPropertiesSubscriptions.remove(key); ``` |
User description
As described in https://github.com/Navideck/universal_ble/issues/33, the Linux implementation did not allow for multiple simultaneous subscriptions to the same characteristic on different devices.
This PR fixes this by adding
deviceId
andserviceUUID
to the key, identifying the characteristic.Type
bug_fix
Description
deviceId
,serviceUUID
, andcharacteristic
, ensuring unique identification across devices and services.Changes walkthrough
universal_ble_linux.dart
Enhance Characteristic Subscription Keying on Linux
lib/src/universal_ble_linux/universal_ble_linux.dart
deviceId
,service
, andcharacteristic
to uniquely identify characteristic subscriptions._characteristicPropertiesSubscriptions
for adding, checking, andremoving subscriptions.
changes.