summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/device.dart30
-rw-r--r--lib/main.dart69
2 files changed, 77 insertions, 22 deletions
diff --git a/lib/device.dart b/lib/device.dart
new file mode 100644
index 0000000..5f604c8
--- /dev/null
+++ b/lib/device.dart
@@ -0,0 +1,30 @@
+import 'package:flutter/material.dart';
+import 'package:logger/logger.dart';
+
+var logger = Logger(printer: PrettyPrinter());
+
+class Device extends StatelessWidget {
+ const Device({super.key});
+
+ @override
+ Widget build(BuildContext context) {
+ return MaterialApp();
+ }
+}
+
+class DeviceScreen extends StatefulWidget {
+ const DeviceScreen({super.key});
+
+ @override
+ State<StatefulWidget> createState() {
+ return DeviceState();
+ }
+}
+
+class DeviceState extends State<DeviceScreen> {
+ @override
+ Widget build(BuildContext context) {
+ // TODO: implement build
+ throw UnimplementedError();
+ }
+}
diff --git a/lib/main.dart b/lib/main.dart
index 6ccf64d..f821106 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io' show Platform;
+import 'package:badge/device.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
@@ -21,7 +22,7 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
- title: 'Flutter Demo',
+ title: 'Scanner',
theme: ThemeData(
// This is the theme of your application.
//
@@ -69,7 +70,29 @@ class _MyHomePageState extends State<MyHomePage> {
static const int noItemSelected = -1;
int selectedResult = noItemSelected;
- void _doConnect() {}
+ 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()),
+ // );
+ }
void _doScan() async {
setState(() {
@@ -83,27 +106,29 @@ class _MyHomePageState extends State<MyHomePage> {
);
// 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;
+ try {
+ 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),
+ );
+ }
- subscription.cancel();
- setState(() {
- isScanning = false;
- });
+ // wait for scanning to stop
+ await FlutterBluePlus.isScanning.where((val) => val == false).first;
+ } finally {
+ subscription.cancel();
+ setState(() {
+ isScanning = false;
+ });
+ }
}
void onScanResult(results) {