Improve BleUuid API and tests (#62)

* Improve tests

* Fix tests

* More improvements and ToDos

* Final API changes
Add tests

* Update readme

---------

Co-authored-by: Rohit Sangwan <rohitsangwan647@gmail.com>
This commit is contained in:
Foti Dim
2024-07-15 05:59:30 +02:00
committed by GitHub
parent 44412508e7
commit efaa72acbd
13 changed files with 318 additions and 115 deletions
+2 -2
View File
@@ -5,7 +5,7 @@ class BleService {
List<BleCharacteristic> characteristics;
BleService(String uuid, this.characteristics) {
this.uuid = BleUuid.parse(uuid);
this.uuid = BleUuidParser.string(uuid);
}
}
@@ -14,7 +14,7 @@ class BleCharacteristic {
List<CharacteristicProperty> properties;
BleCharacteristic(String uuid, this.properties) {
this.uuid = BleUuid.parse(uuid);
this.uuid = BleUuidParser.string(uuid);
}
}
@@ -1,11 +1,17 @@
class BleUuid {
/// Parse a String to valid UUID and convert a 16 bit UUID to 128 bit UUID
/// Throws `FormatException` if the UUID is invalid
static String parse(String uuid) {
class BleUuidParser {
BleUuidParser._();
/// Parse a string to a valid 128-bit UUID.
/// Throws `FormatException` if the string does not hold a valid UUID format.
static String string(String uuid) {
if (uuid.length < 4) {
throw const FormatException('Invalid UUID');
}
if (uuid.startsWith('0x')) {
uuid = uuid.substring(2);
}
if (uuid.length <= 8) {
uuid = "${uuid.padLeft(8, '0')}-0000-1000-8000-00805f9b34fb";
}
@@ -41,17 +47,22 @@ class BleUuid {
return uuid.toLowerCase();
}
/// Parse 16/32 bit uuid like `0x1800` to 128 bit uuid like `00001800-0000-1000-8000-00805f9b34fb`
static String extend(int short) =>
parse(short.toRadixString(16).padLeft(4, '0'));
/// Parse an int number into a 128-bit UUID string.
/// e.g. `0x1800` to `00001800-0000-1000-8000-00805f9b34fb`.
static String number(int short) {
if (short <= 0xFF || short > 0xFFFF) {
throw const FormatException('Invalid UUID');
}
return string(short.toRadixString(16).padLeft(4, '0'));
}
/// Compare two UUIDs to automatically convert both to 128 bit UUIDs
/// Throws `FormatException` if the UUID is invalid
static bool equals(String uuid1, String uuid2) =>
parse(uuid1) == parse(uuid2);
/// Compare two UUIDs regardless of their format.
/// Throws `FormatException` if the UUID is invalid.
static bool compareStrings(String uuid1, String uuid2) =>
string(uuid1) == string(uuid2);
}
/// Parse a list of strings to a list of UUIDs
/// Parse a list of strings to a list of UUIDs.
extension StringListToUUID on List<String> {
List<String> toValidUUIDList() => map(BleUuid.parse).toList();
List<String> toValidUUIDList() => map(BleUuidParser.string).toList();
}
+1 -1
View File
@@ -1,5 +1,5 @@
export 'package:universal_ble/src/models/queue_type.dart';
export 'package:universal_ble/src/models/ble_uuid.dart';
export 'package:universal_ble/src/models/ble_uuid_parser.dart';
export 'package:universal_ble/src/models/scan_filter.dart';
export 'package:universal_ble/src/models/ble_property.dart';
export 'package:universal_ble/src/models/ble_service.dart';
+6 -6
View File
@@ -111,8 +111,8 @@ class UniversalBle {
return await _bleCommandQueue.queueCommand(
() => _platform.setNotifiable(
deviceId,
BleUuid.parse(service),
BleUuid.parse(characteristic),
BleUuidParser.string(service),
BleUuidParser.string(characteristic),
bleInputProperty,
),
deviceId: deviceId,
@@ -129,8 +129,8 @@ class UniversalBle {
return await _bleCommandQueue.queueCommand(
() => _platform.readValue(
deviceId,
BleUuid.parse(service),
BleUuid.parse(characteristic),
BleUuidParser.string(service),
BleUuidParser.string(characteristic),
),
deviceId: deviceId,
);
@@ -148,8 +148,8 @@ class UniversalBle {
await _bleCommandQueue.queueCommand(
() => _platform.writeValue(
deviceId,
BleUuid.parse(service),
BleUuid.parse(characteristic),
BleUuidParser.string(service),
BleUuidParser.string(characteristic),
value,
bleOutputProperty,
),
@@ -209,7 +209,7 @@ extension _UniversalBleScanResultExtension on UniversalBleScanResult {
isSystemDevice: isSystemDevice,
services: services
?.where((e) => e != null)
.map((e) => BleUuid.parse(e!))
.map((e) => BleUuidParser.string(e!))
.toList() ??
[],
);
@@ -65,7 +65,8 @@ abstract class UniversalBlePlatform {
/// `onValueChange` interceptor to parse the native uuids to 128 bit uuid, to keep consistency
void updateCharacteristicValue(
String deviceId, String characteristicId, Uint8List value) {
onValueChange?.call(deviceId, BleUuid.parse(characteristicId), value);
onValueChange?.call(
deviceId, BleUuidParser.string(characteristicId), value);
}
OnAvailabilityChange? onAvailabilityChange;