summaryrefslogtreecommitdiff
path: root/lib/device.dart
diff options
context:
space:
mode:
authoruvok2025-07-22 09:44:17 +0200
committeruvok2025-07-22 09:44:17 +0200
commita94649aa974298538b3d9ecf0028f68ae5bdca4e (patch)
tree06fb9c4a61539633ef02aaf2492219e5af66bfe4 /lib/device.dart
parent53242a20032c054e854f2bf3d94f4a5bd10b0be3 (diff)
Polish scanning and connecting
scan timeout. state cleanup. Use platformname
Diffstat (limited to 'lib/device.dart')
-rw-r--r--lib/device.dart46
1 files changed, 40 insertions, 6 deletions
diff --git a/lib/device.dart b/lib/device.dart
index eb863ad..94b2899 100644
--- a/lib/device.dart
+++ b/lib/device.dart
@@ -1,3 +1,5 @@
+import 'dart:async';
+
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:logger/logger.dart';
@@ -26,13 +28,26 @@ class DeviceScreen extends StatefulWidget {
}
class DeviceState extends State<DeviceScreen> {
+ String connectStatus = "<Status>";
+ // ???
+ StreamSubscription<BluetoothConnectionState> subs =
+ Stream<BluetoothConnectionState>.empty().listen((e) => ());
+ bool backActive = false;
+
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Device details")),
body: Center(
child: Column(
- children: [ElevatedButton(onPressed: backClick, child: Text("Back"))],
+ spacing: 20,
+ children: [
+ Text(connectStatus),
+ ElevatedButton(
+ onPressed: backActive ? backClick : null,
+ child: Text("Back"),
+ ),
+ ],
),
),
);
@@ -49,20 +64,39 @@ class DeviceState extends State<DeviceScreen> {
}
void onConnStateChange(BluetoothConnectionState event) {
+ setState(() {
+ connectStatus = event.toString();
+ });
logger.i("New conn state: ${event.toString()}");
}
+ @override
+ void deactivate() {
+ super.deactivate();
+ logger.i("Closing state");
+ subs.cancel().ignore();
+ widget.btDevice.disconnect().ignore();
+ }
+
void _doConnect() async {
final dev = widget.btDevice;
- var subs = dev.connectionState.listen(onConnStateChange);
+ subs.cancel().ignore();
+ subs = dev.connectionState.listen(onConnStateChange);
try {
logger.i("Try to connect...");
- await dev.connect();
+ // connect timeout doesn't work under Linux
+ await dev.connect().timeout(Duration(seconds: 1));
logger.i("Connected!");
- await dev.disconnect();
- logger.i("Disonnected!");
+
+ connectStatus = "Connected";
+ } catch (e) {
+ dev.disconnect().ignore();
+ connectStatus = e.toString();
} finally {
- subs.cancel();
+ backActive = true;
+ if (mounted) {
+ setState(() {});
+ }
}
}
}