diff options
Diffstat (limited to 'lib/device_details.dart')
-rw-r--r-- | lib/device_details.dart | 130 |
1 files changed, 63 insertions, 67 deletions
diff --git a/lib/device_details.dart b/lib/device_details.dart index 9c92a64..961ad33 100644 --- a/lib/device_details.dart +++ b/lib/device_details.dart @@ -1,16 +1,13 @@ -import 'dart:async'; - -import 'package:badge/first_where_ext.dart'; +import 'package:badge/model/device.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:logger/logger.dart'; -var logger = Logger(printer: PrettyPrinter()); +var logger = Logger(); class DeviceDetailsScreen extends StatefulWidget { - final BluetoothDevice btDevice; + final Device device; - const DeviceDetailsScreen({super.key, required this.btDevice}); + const DeviceDetailsScreen({super.key, required this.device}); @override State<StatefulWidget> createState() { @@ -21,15 +18,14 @@ class DeviceDetailsScreen extends StatefulWidget { class DeviceDetailsState extends State<DeviceDetailsScreen> { String connectStatus = "<Status>"; // Just to have a resonable default subscription? - StreamSubscription<BluetoothConnectionState> subs = - Stream<BluetoothConnectionState>.empty().listen((e) => ()); + // StreamSubscription<BluetoothConnectionState> subs = + // Stream<BluetoothConnectionState>.empty().listen((e) => ()); /// Whether the back button should be active. bool backActive = false; - BluetoothCharacteristic? current; - - BluetoothCharacteristic? available; + // BluetoothCharacteristic? current; + // BluetoothCharacteristic? available; @override Widget build(BuildContext context) { @@ -63,7 +59,7 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { Navigator.pop(context); } - void onConnStateChange(BluetoothConnectionState event) { + void onConnStateChange(ConnectionStatus event) { setState(() { connectStatus = event.toString(); }); @@ -74,40 +70,40 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { void deactivate() { super.deactivate(); logger.i("Closing state"); - subs.cancel().ignore(); - widget.btDevice.disconnect().ignore(); + // subs.cancel().ignore(); + // widget.device.disconnect().ignore(); } void _doConnect() async { - final dev = widget.btDevice; - subs.cancel().ignore(); - subs = dev.connectionState.listen(onConnStateChange); + final dev = widget.device; + // subs.cancel().ignore(); + // subs = dev.connectionState.listen(onConnStateChange); try { logger.i("Try to connect..."); // connect timeout doesn't work under Linux - await dev.connect().timeout(Duration(seconds: 2)); - logger.i("Connected!"); - - connectStatus = "Connected"; - - // ???? WTF ???? - List<BluetoothService> svcs = await dev.discoverServices(); - dev.onServicesReset.listen((_) async { - logger.i("Services Reset"); - try { - List<BluetoothService> svcs = await dev.discoverServices(); - findCharac(svcs); - } catch (e) { - logger.e(e); - } - }); - - logger.i("services discovered"); - - findCharac(svcs); + // await dev.connect().timeout(Duration(seconds: 2)); + // logger.i("Connected!"); + + // connectStatus = "Connected"; + + // // ???? WTF ???? + // List<BluetoothService> svcs = await dev.discoverServices(); + // dev.onServicesReset.listen((_) async { + // logger.i("Services Reset"); + // try { + // List<BluetoothService> svcs = await dev.discoverServices(); + // findCharac(svcs); + // } catch (e) { + // logger.e(e); + // } + // }); + + // logger.i("services discovered"); + + // findCharac(svcs); } catch (e) { logger.e(e); - dev.disconnect().ignore(); + // dev.disconnect().ignore(); connectStatus = e.toString(); } finally { backActive = true; @@ -117,32 +113,32 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { } } - void findCharac(List<BluetoothService> svcs) { - if (svcs.isEmpty) { - connectStatus += ", No services found!"; - return; - } - connectStatus += ", Services found!"; - BluetoothService? badgeService = svcs.firstWhereOrNull( - (s) => s.serviceUuid.str == "ca260000-b4bb-46b2-bd06-b7b7a61ea990", - ); - - if (badgeService == null) { - } else { - logger.i("badge service found"); - current = badgeService.characteristics.firstWhereOrNull( - (c) => - c.characteristicUuid.str == "ca260001-b4bb-46b2-bd06-b7b7a61ea990", - ); - available = badgeService.characteristics.firstWhereOrNull( - (c) => - c.characteristicUuid.str == "ca260002-b4bb-46b2-bd06-b7b7a61ea990", - ); - } - - if (current == null || available == null) { - } else { - logger.i("characteristics found"); - } - } + // void findCharac(List<BluetoothService> svcs) { + // if (svcs.isEmpty) { + // connectStatus += ", No services found!"; + // return; + // } + // connectStatus += ", Services found!"; + // BluetoothService? badgeService = svcs.firstWhereOrNull( + // (s) => s.serviceUuid.str == "ca260000-b4bb-46b2-bd06-b7b7a61ea990", + // ); + + // if (badgeService == null) { + // } else { + // logger.i("badge service found"); + // current = badgeService.characteristics.firstWhereOrNull( + // (c) => + // c.characteristicUuid.str == "ca260001-b4bb-46b2-bd06-b7b7a61ea990", + // ); + // available = badgeService.characteristics.firstWhereOrNull( + // (c) => + // c.characteristicUuid.str == "ca260002-b4bb-46b2-bd06-b7b7a61ea990", + // ); + // } + + // if (current == null || available == null) { + // } else { + // logger.i("characteristics found"); + // } + // } } |