Improve web (#3)

* Improve web example app

* Improve readme

* Minor improvements

* add github workflow for example app

* Fix flutter version in github action

* Fix flutter version in github action

* Fix flutter version in github action

* Fix flutter version in github action

* Minor fix

* Minor fixes

* Update readme and version

* Update Changelog

* Improve wording

* Update CHANGELOG.md

Co-authored-by: Foti Dim <foti@navideck.com>

---------

Co-authored-by: Foti Dim <fdimanidis@gmail.com>
Co-authored-by: Foti Dim <foti@navideck.com>
This commit is contained in:
Rohit Sangwan
2024-01-26 15:25:55 +05:30
committed by Foti Dim
parent 531b6139c4
commit 2326e598cf
7 changed files with 139 additions and 52 deletions
+18 -3
View File
@@ -1,5 +1,6 @@
// ignore_for_file: use_build_context_synchronously
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';
@@ -35,6 +36,10 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
/// Add common services for web
if (kIsWeb) {
_services.addAll(WebRequestOptionsBuilder.defaultServices);
}
_requestOptions =
WebRequestOptionsBuilder.acceptAllDevices(optionalServices: _services);
@@ -90,8 +95,17 @@ class _MyAppState extends State<MyApp> {
_scanResults.clear();
_isScanning = true;
});
await UniversalBle.startScan(
webRequestOptions: _requestOptions);
try {
await UniversalBle.startScan(
webRequestOptions: _requestOptions);
} catch (e) {
setState(() {
_isScanning = false;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(e.toString())),
);
}
},
),
PlatformButton(
@@ -103,7 +117,8 @@ class _MyAppState extends State<MyApp> {
});
},
),
if (Capabilities.supportsBluetoothEnableApi)
if (Capabilities.supportsBluetoothEnableApi &&
bleAvailabilityState == AvailabilityState.poweredOff)
PlatformButton(
text: 'Enable Bluetooth',
onPressed: () async {
@@ -90,24 +90,6 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
}
}
Uint8List? _getWriteValue() {
if (!valueFormKey.currentState!.validate()) return null;
if (binaryCode.text.isEmpty) {
print("Error: No value to write");
return null;
}
List<int> hexList = [];
try {
hexList = hex.decode(binaryCode.text);
} catch (e) {
print("Error parsing hex $e");
}
return Uint8List.fromList(hexList);
}
Future<void> _discoverServices() async {
var services = await UniversalBle.discoverServices(widget.deviceId);
print('${services.length} services discovered');
@@ -115,6 +97,11 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
setState(() {
discoveredServices = services;
});
if (kIsWeb) {
_addLog("DiscoverServices",
'${services.length} services discovered,\nNote: Only services added in WebRequestOptionsBuilder will be discoverd');
}
}
Future<void> _readValue() async {
@@ -134,9 +121,20 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
}
Future<void> _writeValue() async {
Uint8List? value = _getWriteValue();
if (value == null || selectedCharacteristic == null) return;
print("Writing $value");
if (selectedCharacteristic == null ||
!valueFormKey.currentState!.validate() ||
binaryCode.text.isEmpty) {
return;
}
Uint8List value;
try {
value = Uint8List.fromList(hex.decode(binaryCode.text));
} catch (e) {
_addLog('WriteError', "Error parsing hex $e");
return;
}
try {
await UniversalBle.writeValue(
widget.deviceId,
@@ -314,7 +312,12 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
if (value == null || value.isEmpty) {
return 'Please enter a value';
}
return null;
try {
hex.decode(binaryCode.text);
return null;
} catch (e) {
return 'Please enter a valid hex value ( without spaces or 0x (e.g. F0BB) )';
}
},
decoration: const InputDecoration(
hintText:
@@ -337,15 +340,16 @@ class _PeripheralDetailPageState extends State<PeripheralDetailPage> {
enabled: isConnected,
text: 'Discover Services',
),
PlatformButton(
enabled: isConnected,
onPressed: () async {
int mtu = await UniversalBle.requestMtu(
widget.deviceId, 247);
_addLog('MTU', mtu);
},
text: 'Request Mtu',
),
if (Capabilities.supportsRequestMtuApi)
PlatformButton(
enabled: isConnected,
onPressed: () async {
int mtu = await UniversalBle.requestMtu(
widget.deviceId, 247);
_addLog('MTU', mtu);
},
text: 'Request Mtu',
),
PlatformButton(
enabled: isConnected &&
discoveredServices.isNotEmpty &&