diff options
author | uvok | 2025-07-29 14:12:32 +0200 |
---|---|---|
committer | uvok | 2025-07-29 14:12:32 +0200 |
commit | 7c0f47a946939875b13250b204898911fc525827 (patch) | |
tree | d54d5406138fcbdfe537558eab7ca3994497c6b0 /lib/device_details.dart | |
parent | f2c186f93939cabda93bffac3419dc92999eddf3 (diff) |
Rename classes
Diffstat (limited to 'lib/device_details.dart')
-rw-r--r-- | lib/device_details.dart | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/lib/device_details.dart b/lib/device_details.dart new file mode 100644 index 0000000..4f96ed8 --- /dev/null +++ b/lib/device_details.dart @@ -0,0 +1,93 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_blue_plus/flutter_blue_plus.dart'; +import 'package:logger/logger.dart'; + +var logger = Logger(printer: PrettyPrinter()); + +class DeviceDetailsScreen extends StatefulWidget { + final BluetoothDevice btDevice; + + const DeviceDetailsScreen({super.key, required this.btDevice}); + + @override + State<StatefulWidget> createState() { + return DeviceDetailsState(); + } +} + +class DeviceDetailsState extends State<DeviceDetailsScreen> { + 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( + spacing: 20, + children: [ + Text(connectStatus), + ElevatedButton( + onPressed: backActive ? backClick : null, + child: Text("Back"), + ), + ], + ), + ), + ); + } + + @override + void initState() { + super.initState(); + _doConnect(); + } + + void backClick() { + Navigator.pop(context); + } + + 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; + 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"; + } catch (e) { + logger.e(e); + dev.disconnect().ignore(); + connectStatus = e.toString(); + } finally { + backActive = true; + if (mounted) { + setState(() {}); + } + } + } +} |