Files
universal_ble/test/ble_characteristic_test.dart
T
Tony 1d7108c9d0 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>
2026-05-08 03:36:12 +08:00

141 lines
3.9 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:universal_ble/universal_ble.dart';
import 'universal_ble_test_mock.dart';
String serviceId = "180a";
String characteristicId = "202a";
String mockDeviceId = "mock_id";
BleCharacteristic mockBleCharacteristic = BleCharacteristic.withMetaData(
deviceId: mockDeviceId,
serviceId: serviceId,
uuid: characteristicId,
properties: CharacteristicProperty.values,
descriptors: [],
);
BleService mockBleService = BleService(serviceId, [mockBleCharacteristic]);
void main() {
late UniversalBlePlatform platform;
setUp(() {
platform = _UniversalBleMock();
UniversalBle.setInstance(platform);
});
group('BleCharacteristic Tests', () {
test("DiscoverServices test", () async {
debugPrint("Discovering services");
List<BleService> services = await UniversalBle.discoverServices(mockDeviceId);
expect(services.length, 1);
BleService service = services.first;
expect(BleUuidParser.compareStrings(service.uuid, serviceId), true);
expect(service.characteristics.length, 1);
BleCharacteristic characteristic = service.characteristics.first;
expect(BleUuidParser.compareStrings(characteristic.uuid, characteristicId), true);
expect(characteristic.metaData?.deviceId, mockDeviceId);
expect(BleUuidParser.compareStrings(characteristic.metaData!.serviceId, serviceId), true);
});
test("Subscription Test", () async {
BleCharacteristic characteristic = mockBleCharacteristic;
debugPrint("Subscribing to char");
await characteristic.notifications.subscribe();
bool gotEvent = false;
var subscription = characteristic.notifications.listen((data) {
debugPrint("Received CharValue: $data");
gotEvent = true;
});
await Future.delayed(Duration(seconds: 1));
await characteristic.notifications.unsubscribe();
debugPrint("Unsubscribed from char");
subscription.cancel();
expect(gotEvent, true);
});
});
test("Write/Read Value Test", () async {
BleCharacteristic characteristic = mockBleCharacteristic;
Uint8List charValue = Uint8List.fromList([0x01, 0x02]);
await characteristic.write(charValue);
debugPrint("Write Succeed");
var readResult = await characteristic.read();
debugPrint("Read Succeed");
expect(readResult, charValue);
});
}
class _UniversalBleMock extends UniversalBlePlatformMock {
Timer? notifierTimer;
Uint8List? charValue;
@override
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 {
if (bleInputProperty == BleInputProperty.disabled) {
notifierTimer?.cancel();
notifierTimer = null;
return;
}
notifierTimer ??= Timer.periodic(Duration(milliseconds: 500), (timer) {
updateCharacteristicValue(
deviceId,
characteristic,
Uint8List.fromList([1, 2, 3]),
DateTime.now().millisecondsSinceEpoch,
);
});
}
@override
Future<void> writeValue(
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 {
return charValue ?? Uint8List(0);
}
@override
Future<void> requestPermissions({
bool withAndroidFineLocation = false,
bool withAndroidBluetoothAdvertise = false,
}) {
throw UnimplementedError();
}
@override
Future<int> readRssi(String deviceId) async {
return -50; // Mock RSSI value
}
}