diff options
author | uvok | 2025-07-22 09:44:17 +0200 |
---|---|---|
committer | uvok | 2025-07-22 09:44:17 +0200 |
commit | a94649aa974298538b3d9ecf0028f68ae5bdca4e (patch) | |
tree | 06fb9c4a61539633ef02aaf2492219e5af66bfe4 /lib/main.dart | |
parent | 53242a20032c054e854f2bf3d94f4a5bd10b0be3 (diff) |
Polish scanning and connecting
scan timeout.
state cleanup.
Use platformname
Diffstat (limited to 'lib/main.dart')
-rw-r--r-- | lib/main.dart | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/lib/main.dart b/lib/main.dart index aef85cf..23721ac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -80,6 +80,14 @@ class _MyHomePageState extends State<MyHomePage> { } void _doScan() async { + var system = await FlutterBluePlus.systemDevices([]); + for (var d in system) { + logger.i('${d.platformName} already connected to! ${d.remoteId}'); + // if (d.platformName == "myBleDevice") { + // await r.connect(); // must connect our app + // } + } + setState(() { scanResults.clear(); isScanning = true; @@ -95,14 +103,14 @@ class _MyHomePageState extends State<MyHomePage> { if (Platform.isAndroid) { await FlutterBluePlus.startScan( withKeywords: ["NimBLE"], - timeout: Duration(seconds: 3), + 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: 3), + //withMsd: [MsdFilter(0xffff, data: ascii.encode("uvok"))], + timeout: Duration(seconds: 5), ); } @@ -116,15 +124,16 @@ class _MyHomePageState extends State<MyHomePage> { } } - void onScanResult(results) { + void onScanResult(List<ScanResult> results) { if (results.isNotEmpty) { - ScanResult r = results.last; // the most recently found device - logger.i( - '${r.device.remoteId}: "${r.device.advName}" / "${r.advertisementData.advName}" found!', - ); - setState(() { + //ScanResult r = results.last; // the most recently found device + for (var r in results.where((d) => d.rssi > -90)) { + logger.i( + '${r.device.remoteId}: "${r.device.platformName}" / "${r.device.advName}" / "${r.advertisementData.advName}" found!', + ); scanResults.add(r); - }); + } + setState(() {}); } } @@ -189,7 +198,9 @@ class _MyHomePageState extends State<MyHomePage> { // action in the IDE, or press "p" in the console), to see the // wireframe for each widget. mainAxisAlignment: MainAxisAlignment.center, + spacing: 24, children: <Widget>[ + SizedBox(height: 15), Row( mainAxisAlignment: MainAxisAlignment.center, spacing: 15.0, @@ -212,9 +223,12 @@ class _MyHomePageState extends State<MyHomePage> { itemBuilder: (context, index) { if (index >= scanResults.length) return null; final ScanResult result = scanResults[index]; - final String name = result.device.advName.isEmpty - ? ("<Unknown>") - : (result.device.advName); + final String name = firstGiven([ + result.device.advName, + result.device.platformName, + "<Unknown>", + ]); + return ListTile( title: Text(name), subtitle: Text(result.device.remoteId.str), @@ -241,3 +255,7 @@ class _MyHomePageState extends State<MyHomePage> { ); } } + +String firstGiven(List<String> list) { + return list.firstWhere((s) => s.isNotEmpty, orElse: () => ""); +} |