Update Universal BLE Plugin for Peripheral Support and Code Generation
- Updated autogenerated files to reflect changes from Pigeon v26.3.4. - Added new classes for UniversalBlePeripheralConfig, UniversalBlePeripheralService, UniversalBlePeripheralCharacteristic, UniversalBlePeripheralDescriptor, and UniversalBlePeripheralWriteEvent to handle peripheral configurations and events. - Modified UniversalBlePlugin to implement methods for peripheral support, including StartPeripheral, StopPeripheral, UpdatePeripheralCharacteristicValue, and NotifyPeripheralCharacteristic, returning appropriate error messages for unsupported features on Windows. - Updated HasPermissions method to include an additional parameter for Bluetooth advertising permissions. - Adjusted method signatures and implementations across the plugin to accommodate new peripheral functionality. Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -26,8 +26,7 @@ void main() {
|
||||
group('BleCharacteristic Tests', () {
|
||||
test("DiscoverServices test", () async {
|
||||
debugPrint("Discovering services");
|
||||
List<BleService> services =
|
||||
await UniversalBle.discoverServices(mockDeviceId);
|
||||
List<BleService> services = await UniversalBle.discoverServices(mockDeviceId);
|
||||
expect(services.length, 1);
|
||||
|
||||
BleService service = services.first;
|
||||
@@ -35,18 +34,9 @@ void main() {
|
||||
expect(service.characteristics.length, 1);
|
||||
|
||||
BleCharacteristic characteristic = service.characteristics.first;
|
||||
expect(
|
||||
BleUuidParser.compareStrings(characteristic.uuid, characteristicId),
|
||||
true,
|
||||
);
|
||||
expect(BleUuidParser.compareStrings(characteristic.uuid, characteristicId), true);
|
||||
expect(characteristic.metaData?.deviceId, mockDeviceId);
|
||||
expect(
|
||||
BleUuidParser.compareStrings(
|
||||
characteristic.metaData!.serviceId,
|
||||
serviceId,
|
||||
),
|
||||
true,
|
||||
);
|
||||
expect(BleUuidParser.compareStrings(characteristic.metaData!.serviceId, serviceId), true);
|
||||
});
|
||||
|
||||
test("Subscription Test", () async {
|
||||
@@ -87,14 +77,17 @@ class _UniversalBleMock extends UniversalBlePlatformMock {
|
||||
Uint8List? charValue;
|
||||
|
||||
@override
|
||||
Future<List<BleService>> discoverServices(
|
||||
String deviceId, bool withDescriptors) async {
|
||||
Future<List<BleService>> discoverServices(String deviceId, bool withDescriptors) async {
|
||||
return <BleService>[mockBleService];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setNotifiable(String deviceId, String service,
|
||||
String characteristic, BleInputProperty bleInputProperty) async {
|
||||
Future<void> setNotifiable(
|
||||
String deviceId,
|
||||
String service,
|
||||
String characteristic,
|
||||
BleInputProperty bleInputProperty,
|
||||
) async {
|
||||
if (bleInputProperty == BleInputProperty.disabled) {
|
||||
notifierTimer?.cancel();
|
||||
notifierTimer = null;
|
||||
@@ -113,23 +106,30 @@ class _UniversalBleMock extends UniversalBlePlatformMock {
|
||||
|
||||
@override
|
||||
Future<void> writeValue(
|
||||
String deviceId,
|
||||
String service,
|
||||
String characteristic,
|
||||
Uint8List value,
|
||||
BleOutputProperty bleOutputProperty) async {
|
||||
String deviceId,
|
||||
String service,
|
||||
String characteristic,
|
||||
Uint8List value,
|
||||
BleOutputProperty bleOutputProperty,
|
||||
) async {
|
||||
charValue = value;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> readValue(
|
||||
String deviceId, String service, String characteristic,
|
||||
{Duration? timeout}) async {
|
||||
String deviceId,
|
||||
String service,
|
||||
String characteristic, {
|
||||
Duration? timeout,
|
||||
}) async {
|
||||
return charValue ?? Uint8List(0);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> requestPermissions({bool withAndroidFineLocation = false}) {
|
||||
Future<void> requestPermissions({
|
||||
bool withAndroidFineLocation = false,
|
||||
bool withAndroidBluetoothAdvertise = false,
|
||||
}) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
|
||||
void main() {
|
||||
group('BlePeripheral models', () {
|
||||
test('normalizes UUIDs and defensively copies characteristic values', () {
|
||||
final initialValue = Uint8List.fromList([1, 2, 3]);
|
||||
|
||||
final characteristic = BlePeripheralCharacteristic(
|
||||
uuid: 'fff1',
|
||||
properties: const [CharacteristicProperty.read],
|
||||
permissions: const [BlePeripheralCharacteristicPermission.read],
|
||||
initialValue: initialValue,
|
||||
);
|
||||
|
||||
initialValue[0] = 9;
|
||||
|
||||
expect(
|
||||
BleUuidParser.compareStrings(characteristic.uuid, '0000fff1-0000-1000-8000-00805f9b34fb'),
|
||||
isTrue,
|
||||
);
|
||||
expect(characteristic.initialValue, [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('normalizes write event UUIDs and defensively copies payloads', () {
|
||||
final value = Uint8List.fromList([4, 5, 6]);
|
||||
|
||||
final event = BlePeripheralWriteEvent(
|
||||
deviceId: 'client-1',
|
||||
service: 'fff0',
|
||||
characteristic: 'fff1',
|
||||
value: value,
|
||||
);
|
||||
|
||||
value[0] = 8;
|
||||
|
||||
expect(event.deviceId, 'client-1');
|
||||
expect(BleUuidParser.compareStrings(event.service, 'fff0'), isTrue);
|
||||
expect(BleUuidParser.compareStrings(event.characteristic, 'fff1'), isTrue);
|
||||
expect(event.value, [4, 5, 6]);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user