diff options
| author | uvok | 2025-07-21 18:23:53 +0200 | 
|---|---|---|
| committer | uvok | 2025-07-21 18:23:53 +0200 | 
| commit | ae9ef9a1eac773fc2a912f2d9e49905e5f1f2f8d (patch) | |
| tree | ec7115820d4106d16e8f55975663ad7b5e9a4336 /lib | |
| parent | 27830b7bce9bdabc5ba46beab99389ecc96f3f92 (diff) | |
Add selection, scanning, connect button
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/main.dart | 48 | 
1 files changed, 38 insertions, 10 deletions
| diff --git a/lib/main.dart b/lib/main.dart index fb4eaff..6ccf64d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,7 @@ +import 'dart:convert'; + +import 'dart:io' show Platform; +  import 'package:flutter/material.dart';  import 'package:permission_handler/permission_handler.dart'; @@ -62,24 +66,36 @@ class MyHomePage extends StatefulWidget {  class _MyHomePageState extends State<MyHomePage> {    final List<ScanResult> scanResults = [];    bool isScanning = false; -  int selectedResult = -1; +  static const int noItemSelected = -1; +  int selectedResult = noItemSelected; + +  void _doConnect() {}    void _doScan() async {      setState(() {        scanResults.clear();        isScanning = true; -      selectedResult = -1; +      selectedResult = noItemSelected;      });      var subscription = FlutterBluePlus.onScanResults.listen(        onScanResult,        onError: (e) => logger.e(e),      ); -    await FlutterBluePlus.startScan( -      //withServices:[Guid("180D")], // match any of the specified services -      //withNames:["Bluno"], // *or* any of the specified names -      timeout: Duration(seconds: 10), -    ); +    // Ehhhh... can't have both +    if (Platform.isAndroid) { +      await FlutterBluePlus.startScan( +        withKeywords: ["NimBLE"], +        timeout: Duration(seconds: 5), +      ); +    } 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), +      ); +    }      // wait for scanning to stop      await FlutterBluePlus.isScanning.where((val) => val == false).first; @@ -164,9 +180,21 @@ class _MyHomePageState extends State<MyHomePage> {            // wireframe for each widget.            mainAxisAlignment: MainAxisAlignment.center,            children: <Widget>[ -            ElevatedButton( -              onPressed: isScanning ? null : _doScan, -              child: isScanning ? Text("Scanning...") : Text("Start scan"), +            Row( +              mainAxisAlignment: MainAxisAlignment.center, +              spacing: 15.0, +              children: [ +                ElevatedButton( +                  onPressed: isScanning ? null : _doScan, +                  child: isScanning ? Text("Scanning...") : Text("Start scan"), +                ), +                ElevatedButton( +                  onPressed: (selectedResult == noItemSelected || isScanning) +                      ? null +                      : _doConnect, +                  child: Text("Connect"), +                ), +              ],              ),              Expanded(                child: ListView.separated( | 
