Commit b2d5632d authored by Andrey's avatar Andrey

Added toaster

parent a0a71a25
...@@ -2,18 +2,21 @@ import 'dart:async'; ...@@ -2,18 +2,21 @@ import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart'; import 'package:flutter_bluetooth_basic/flutter_bluetooth_basic.dart';
import 'package:oktoast/oktoast.dart';
void main() => runApp(MyApp()); void main() => runApp(MyApp());
class MyApp extends StatelessWidget { class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return OKToast(
child: MaterialApp(
title: 'Bluetooth demo', title: 'Bluetooth demo',
theme: ThemeData( theme: ThemeData(
primarySwatch: Colors.blue, primarySwatch: Colors.blue,
), ),
home: MyHomePage(title: 'Bluetooth demo'), home: MyHomePage(title: 'Bluetooth demo'),
),
); );
} }
} }
...@@ -31,6 +34,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -31,6 +34,7 @@ class _MyHomePageState extends State<MyHomePage> {
BluetoothManager bluetoothManager = BluetoothManager.instance; BluetoothManager bluetoothManager = BluetoothManager.instance;
bool _connected = false; bool _connected = false;
bool _isScanning = false; bool _isScanning = false;
bool _isPrinting = false;
List<BluetoothDevice> _devices = []; List<BluetoothDevice> _devices = [];
StreamSubscription _scanResultsSubscription; StreamSubscription _scanResultsSubscription;
StreamSubscription _isScanningSubscription; StreamSubscription _isScanningSubscription;
...@@ -75,6 +79,19 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -75,6 +79,19 @@ class _MyHomePageState extends State<MyHomePage> {
} }
void _testPrint(BluetoothDevice printer) async { void _testPrint(BluetoothDevice printer) async {
if (_isScanning) {
// print('Print failed (scanning in progress)');
showToast('Print failed (scanning in progress)');
return;
}
if (_isPrinting) {
// print('Print failed (another printing in progress)');
showToast('Print failed (another printing in progress)');
return;
}
_isPrinting = true;
// We have to rescan before connecting, otherwise we can connect only once // We have to rescan before connecting, otherwise we can connect only once
await bluetoothManager.startScan(timeout: Duration(seconds: 1)); await bluetoothManager.startScan(timeout: Duration(seconds: 1));
await bluetoothManager.stopScan(); await bluetoothManager.stopScan();
...@@ -92,12 +109,13 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -92,12 +109,13 @@ class _MyHomePageState extends State<MyHomePage> {
print('@@@@SEND DATA......'); print('@@@@SEND DATA......');
final List<int> bytes = latin1.encode('test!\n\n\n').toList(); final List<int> bytes = latin1.encode('test!\n\n\n').toList();
await bluetoothManager.writeData(bytes); await bluetoothManager.writeData(bytes);
// TODO show message "Data sent" showToast('Data sent');
} }
// TODO sending disconnect signal should be event-based // TODO sending disconnect signal should be event-based
_sleep(3).then((dynamic printer) async { _sleep(3).then((dynamic printer) async {
print('@@@@DISCONNECTING......'); print('@@@@DISCONNECTING......');
await bluetoothManager.disconnect(); await bluetoothManager.disconnect();
_isPrinting = false;
}); });
_connected = true; _connected = true;
break; break;
......
...@@ -9,6 +9,7 @@ dependencies: ...@@ -9,6 +9,7 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2
oktoast: ^2.3.0
flutter_bluetooth_basic: flutter_bluetooth_basic:
git: git:
url: git://github.com/andrey-ushakov/flutter_bluetooth_basic url: git://github.com/andrey-ushakov/flutter_bluetooth_basic
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment