Last active
June 1, 2019 07:23
-
-
Save kenreilly/9d373d0334762fff20dc3726a24011d5 to your computer and use it in GitHub Desktop.
Arduino bluetooth demo - Flutter app entry point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'bt-controller.dart'; | |
void main() => runApp(ArduinoBT()); | |
class ArduinoBT extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Arduino BT Demo', | |
theme: ThemeData(primarySwatch: Colors.deepPurple), | |
home: MainPage(title: 'Arduino BT Demo'), | |
); | |
} | |
} | |
class MainPage extends StatefulWidget { | |
MainPage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MainPageState createState() => _MainPageState(); | |
} | |
class _MainPageState extends State<MainPage> { | |
String sensorValue = "N/A"; | |
bool ledState = false; | |
@override | |
initState() { | |
super.initState(); | |
BTController.init(onData); | |
scanDevices(); | |
} | |
void onData(dynamic str) { setState(() { sensorValue = str; }); } | |
void switchLed() { | |
setState(() { ledState = !ledState; }); | |
BTController.transmit(ledState ? '0' : '1'); | |
} | |
Future<void> scanDevices() async { | |
BTController.enumerateDevices() | |
.then((devices) { onGetDevices(devices); }); | |
} | |
void onGetDevices(List<dynamic> devices) { | |
Iterable<SimpleDialogOption> options = devices.map((device) { | |
return SimpleDialogOption( | |
child: Text(device.keys.first), | |
onPressed: () { selectDevice(device.values.first); }, | |
); | |
}); | |
SimpleDialog dialog = SimpleDialog( | |
title: const Text('Choose a device'), | |
children: options.toList(), | |
); | |
showDialog( | |
barrierDismissible: false, | |
context: context, | |
builder: (BuildContext context) { return dialog; } | |
); | |
} | |
selectDevice(String deviceAddress) { | |
Navigator.of(context, rootNavigator: true).pop('dialog'); | |
BTController.connect(deviceAddress); | |
} | |
@override | |
Widget build(BuildContext context) { | |
Color color = ledState ? Colors.deepPurpleAccent : Colors.white24; | |
TextTheme theme = Theme.of(context).textTheme; | |
return Scaffold( | |
appBar: AppBar(title: Text(widget.title)), | |
body: Container( | |
decoration: BoxDecoration(color: Colors.black), | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Sensor Value', style: theme.display1.copyWith(color: Colors.white)), | |
Text(sensorValue, style: theme.display2.copyWith(color: Colors.white)), | |
], | |
) | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
backgroundColor: color, | |
onPressed: switchLed, | |
tooltip: 'Increment', | |
child: Icon(Icons.power_settings_new), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment