diff options
| author | uvok | 2025-07-21 19:08:18 +0200 | 
|---|---|---|
| committer | uvok | 2025-07-21 19:08:18 +0200 | 
| commit | 53242a20032c054e854f2bf3d94f4a5bd10b0be3 (patch) | |
| tree | d57be4e401b2f09878c8e04ecd3246f6b3fada4c /lib | |
| parent | dbf1f754078ceebe532663aa163519de699e5a90 (diff) | |
More BLE handling in second screeen?
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/device.dart | 44 | ||||
| -rw-r--r-- | lib/main.dart | 27 | 
2 files changed, 47 insertions, 24 deletions
| diff --git a/lib/device.dart b/lib/device.dart index 5f604c8..eb863ad 100644 --- a/lib/device.dart +++ b/lib/device.dart @@ -1,8 +1,10 @@  import 'package:flutter/material.dart'; +import 'package:flutter_blue_plus/flutter_blue_plus.dart';  import 'package:logger/logger.dart';  var logger = Logger(printer: PrettyPrinter()); +// Is this even needed? I have no idea...  class Device extends StatelessWidget {    const Device({super.key}); @@ -13,7 +15,9 @@ class Device extends StatelessWidget {  }  class DeviceScreen extends StatefulWidget { -  const DeviceScreen({super.key}); +  final BluetoothDevice btDevice; + +  const DeviceScreen({super.key, required this.btDevice});    @override    State<StatefulWidget> createState() { @@ -24,7 +28,41 @@ class DeviceScreen extends StatefulWidget {  class DeviceState extends State<DeviceScreen> {    @override    Widget build(BuildContext context) { -    // TODO: implement build -    throw UnimplementedError(); +    return Scaffold( +      appBar: AppBar(title: Text("Device details")), +      body: Center( +        child: Column( +          children: [ElevatedButton(onPressed: backClick, child: Text("Back"))], +        ), +      ), +    ); +  } + +  @override +  void initState() { +    super.initState(); +    _doConnect(); +  } + +  void backClick() { +    Navigator.pop(context); +  } + +  void onConnStateChange(BluetoothConnectionState event) { +    logger.i("New conn state: ${event.toString()}"); +  } + +  void _doConnect() async { +    final dev = widget.btDevice; +    var subs = dev.connectionState.listen(onConnStateChange); +    try { +      logger.i("Try to connect..."); +      await dev.connect(); +      logger.i("Connected!"); +      await dev.disconnect(); +      logger.i("Disonnected!"); +    } finally { +      subs.cancel(); +    }    }  } diff --git a/lib/main.dart b/lib/main.dart index f821106..aef85cf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -70,28 +70,13 @@ class _MyHomePageState extends State<MyHomePage> {    static const int noItemSelected = -1;    int selectedResult = noItemSelected; -  void onConnStateChange(BluetoothConnectionState event) { -    logger.i("New conn state: ${event.toString()}"); -  } -    void _doConnect() async {      var dev = scanResults[selectedResult].device; -    var subs = dev.connectionState.listen(onConnStateChange); -    try { -      logger.i("Try to connect..."); -      await dev.connect(); -      logger.i("Connected!"); -      await dev.disconnect(); -      logger.i("Disonnected!"); -    } finally { -      subs.cancel(); -    } -      //??? -    // Navigator.push( -    //   context, -    //   MaterialPageRoute(builder: (context) => DeviceScreen()), -    // ); +    Navigator.push( +      context, +      MaterialPageRoute(builder: (context) => DeviceScreen(btDevice: dev)), +    );    }    void _doScan() async { @@ -110,14 +95,14 @@ class _MyHomePageState extends State<MyHomePage> {        if (Platform.isAndroid) {          await FlutterBluePlus.startScan(            withKeywords: ["NimBLE"], -          timeout: Duration(seconds: 5), +          timeout: Duration(seconds: 3),          );        } else {          // for Linux, which can't do advNames          // msd doesn't work, either????          await FlutterBluePlus.startScan(            withMsd: [MsdFilter(0xffff, data: ascii.encode("uvok"))], -          timeout: Duration(seconds: 5), +          timeout: Duration(seconds: 3),          );        } | 
