50 lines
1.4 KiB
Dart
50 lines
1.4 KiB
Dart
import 'package:flutter_blue/flutter_blue.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'sidebar.dart';
|
|
|
|
class BluetoothTest extends StatefulWidget {
|
|
const BluetoothTest({super.key});
|
|
|
|
@override
|
|
State<BluetoothTest> createState() => _BluetoothTest();
|
|
}
|
|
|
|
class _BluetoothTest extends State<BluetoothTest> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Bluetooth Test"),
|
|
),
|
|
body: Center(
|
|
child: TextButton(
|
|
onPressed: () async {
|
|
|
|
FlutterBlue flutterBlue = FlutterBlue.instance;
|
|
flutterBlue.startScan(timeout: const Duration(seconds: 4));
|
|
|
|
flutterBlue.scanResults.listen((results) async {
|
|
// do something with scan results
|
|
for (ScanResult r in results) {
|
|
String scan = '"scan: ${r.device.name} found! rssi: ${r
|
|
.rssi}"';
|
|
debugPrint(scan);
|
|
if(r.device.name == "Crusher ANC") {
|
|
await r.device.connect();
|
|
}
|
|
}
|
|
});
|
|
// Stop scanning
|
|
flutterBlue.stopScan();
|
|
|
|
}, child: const Text("Scan Devices"),
|
|
),
|
|
),
|
|
drawer: const Sidebar(),// This trailing comma makes auto-formatting nicer for build methods.
|
|
);
|
|
}
|
|
}
|
|
|
|
|