Implement queue (#11)
* Implement queue * Enable queue by default, remove queue from web and update readme * Rename useQueue to queuesRequests * Rename queuesRequests and improve readme * Rename _executeMethod and improve readme * Rename to _isQueueEnabled --------- Co-authored-by: Foti Dim <fdimanidis@gmail.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
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 _mockBleScanResult = BleScanResult(
|
||||
name: 'MockDevice',
|
||||
deviceId: 'MockDeviceId',
|
||||
rssi: 50,
|
||||
manufacturerData: Uint8List(0),
|
||||
);
|
||||
|
||||
Uint8List _serviceValue = utf8.encode('Result');
|
||||
|
||||
final BleService _mockService = BleService('180', [
|
||||
BleCharacteristic('180A', [
|
||||
CharacteristicProperty.read,
|
||||
CharacteristicProperty.write,
|
||||
CharacteristicProperty.notify,
|
||||
]),
|
||||
]);
|
||||
|
||||
@override
|
||||
Future<void> startScan({WebRequestOptionsBuilder? webRequestOptions}) async {
|
||||
onScanResult?.call(_mockBleScanResult);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> stopScan() async {}
|
||||
|
||||
@override
|
||||
Future<void> connect(String deviceId, {Duration? connectionTimeout}) async {
|
||||
onConnectionChanged?.call(deviceId, BleConnectionState.connected);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> disconnect(String deviceId) async {
|
||||
onConnectionChanged?.call(deviceId, BleConnectionState.disconnected);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BleService>> discoverServices(String deviceId) 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<BleScanResult>> getConnectedDevices(
|
||||
List<String>? withServices) async {
|
||||
return [];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> readValue(
|
||||
String deviceId, String service, String characteristic) 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> 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<void> pair(String deviceId) async {
|
||||
onPairingStateChange?.call(deviceId, true, null);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> unPair(String deviceId) async {
|
||||
onPairingStateChange?.call(deviceId, false, null);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:universal_ble/universal_ble.dart';
|
||||
import 'package:universal_ble_example/data/capabilities.dart';
|
||||
import 'package:universal_ble_example/data/mock_universal_ble.dart';
|
||||
import 'package:universal_ble_example/home/widgets/scanned_devices_placeholder_widget.dart';
|
||||
import 'package:universal_ble_example/home/widgets/scanned_item_widget.dart';
|
||||
import 'package:universal_ble_example/data/permission_handler.dart';
|
||||
@@ -21,6 +22,8 @@ class MyApp extends StatefulWidget {
|
||||
class _MyAppState extends State<MyApp> {
|
||||
final _scanResults = <BleScanResult>[];
|
||||
bool _isScanning = false;
|
||||
bool _isQueueEnabled = true;
|
||||
|
||||
AvailabilityState? bleAvailabilityState;
|
||||
late WebRequestOptionsBuilder _requestOptions;
|
||||
final List<String> _services = [
|
||||
@@ -36,6 +39,15 @@ class _MyAppState extends State<MyApp> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
/// Set mock instance for testing
|
||||
if (const bool.fromEnvironment('MOCK')) {
|
||||
UniversalBle.setInstance(MockUniversalBle());
|
||||
}
|
||||
|
||||
/// Setup queue and timeout
|
||||
UniversalBle.queuesCommands = _isQueueEnabled;
|
||||
UniversalBle.timeout = const Duration(seconds: 10);
|
||||
|
||||
/// Add common services for web
|
||||
if (kIsWeb) {
|
||||
_services.addAll(WebRequestOptionsBuilder.defaultServices);
|
||||
@@ -162,6 +174,15 @@ class _MyAppState extends State<MyApp> {
|
||||
});
|
||||
},
|
||||
),
|
||||
PlatformButton(
|
||||
text: _isQueueEnabled ? 'Disable Queue' : 'Enable Queue',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_isQueueEnabled = !_isQueueEnabled;
|
||||
UniversalBle.queuesCommands = _isQueueEnabled;
|
||||
});
|
||||
},
|
||||
),
|
||||
if (_scanResults.isNotEmpty)
|
||||
PlatformButton(
|
||||
text: 'Clear List',
|
||||
|
||||
Reference in New Issue
Block a user