Files
universal_ble/example/lib/data/mock_universal_ble.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

151 lines
3.5 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:universal_ble/universal_ble.dart';
/// Mock implementation of [UniversalBlePlatform] for testing
class MockUniversalBle extends UniversalBlePlatform {
final _mockBleDevice = BleDevice(
name: 'MockDevice',
deviceId: 'MockDeviceId',
rssi: 50,
manufacturerDataList: [],
);
Uint8List _serviceValue = utf8.encode('Result');
bool _isScanning = false;
final BleService _mockService = BleService('180', [
BleCharacteristic('180A', [
CharacteristicProperty.read,
CharacteristicProperty.write,
CharacteristicProperty.notify,
], []),
]);
@override
Future<void> startScan({
ScanFilter? scanFilter,
PlatformConfig? platformConfig,
}) async {
_isScanning = true;
updateScanResult(_mockBleDevice);
}
@override
Future<void> stopScan() async {
_isScanning = false;
}
@override
Future<bool> isScanning() async {
return _isScanning;
}
@override
Future<void> connect(String deviceId,
{Duration? connectionTimeout, bool autoConnect = false}) async {
updateConnection(deviceId, true);
}
@override
Future<void> disconnect(String deviceId) async {
updateConnection(deviceId, false);
}
@override
Future<List<BleService>> discoverServices(String deviceId, bool withDescriptors) async {
return [_mockService];
}
@override
Future<bool> enableBluetooth() async {
await Future.delayed(const Duration(milliseconds: 500));
return true;
}
@override
Future<AvailabilityState> getBluetoothAvailabilityState() async {
return AvailabilityState.poweredOn;
}
@override
Future<List<BleDevice>> getSystemDevices(List<String>? withServices) async {
return [];
}
@override
Future<Uint8List> readValue(
String deviceId,
String service,
String characteristic, {
final Duration? timeout,
}) async {
await Future.delayed(const Duration(milliseconds: 500));
return _serviceValue;
}
@override
Future<void> writeValue(String deviceId, String service, String characteristic, Uint8List value,
BleOutputProperty bleOutputProperty) async {
await Future.delayed(const Duration(milliseconds: 500));
_serviceValue = value;
}
@override
Future<int> requestMtu(String deviceId, int expectedMtu) async {
await Future.delayed(const Duration(seconds: 1));
return 512;
}
@override
Future<void> requestConnectionPriority(
String deviceId,
BleConnectionPriority priority,
) async {}
@override
Future<void> setNotifiable(String deviceId, String service, String characteristic,
BleInputProperty bleInputProperty) async {}
@override
Future<bool> isPaired(String deviceId) async {
await Future.delayed(const Duration(milliseconds: 500));
return true;
}
@override
Future<bool> pair(String deviceId) async {
updatePairingState(deviceId, true);
return true;
}
@override
Future<void> unpair(String deviceId) async {
updatePairingState(deviceId, false);
}
@override
Future<BleConnectionState> getConnectionState(String deviceId) {
throw UnimplementedError();
}
@override
Future<bool> disableBluetooth() {
throw UnimplementedError();
}
@override
Future<void> requestPermissions({
bool withAndroidFineLocation = false,
bool withAndroidBluetoothAdvertise = false,
}) {
throw UnimplementedError();
}
@override
Future<int> readRssi(String deviceId) {
throw UnimplementedError();
}
}