Commit a0a71a25 authored by Andrey's avatar Andrey

Print multiple times.

parent 883a2aac
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';
...@@ -29,20 +30,60 @@ class MyHomePage extends StatefulWidget { ...@@ -29,20 +30,60 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
BluetoothManager bluetoothManager = BluetoothManager.instance; BluetoothManager bluetoothManager = BluetoothManager.instance;
bool _connected = false; bool _connected = false;
bool _isScanning = false;
List<BluetoothDevice> _devices = [];
StreamSubscription _scanResultsSubscription;
StreamSubscription _isScanningSubscription;
// Buffers used for rescan before sending the data
List<BluetoothDevice> _bufDevices = [];
StreamSubscription _bufScanSubscription;
// StreamSubscription _isScanningSubscription;
Future sleep3() { void _startScanDevices() {
return Future<dynamic>.delayed(const Duration(seconds: 7), () => "1"); setState(() {
_devices = [];
});
bluetoothManager.startScan(timeout: Duration(seconds: 4));
_scanResultsSubscription =
bluetoothManager.scanResults.listen((devices) async {
setState(() {
_devices = devices;
});
});
_isScanningSubscription =
bluetoothManager.isScanning.listen((isScanningCurrent) async {
// if isScanning value changed (scan just stopped)
if (_isScanning && !isScanningCurrent) {
_scanResultsSubscription.cancel();
_isScanningSubscription.cancel();
}
setState(() {
_isScanning = isScanningCurrent;
});
});
}
void _stopScanDevices() {
bluetoothManager.stopScan();
}
Future _sleep(int seconds) {
return Future<dynamic>.delayed(Duration(seconds: seconds));
} }
void _testPrint(BluetoothDevice printer) async { void _testPrint(BluetoothDevice printer) async {
// print('Test print.... name: ${printer.name}'); // We have to rescan before connecting, otherwise we can connect only once
if (printer != null && printer.address != null) { await bluetoothManager.startScan(timeout: Duration(seconds: 1));
await bluetoothManager.stopScan();
// Connect // Connect
await bluetoothManager.connect(printer); await bluetoothManager.connect(printer);
// Subscribe to the events // Subscribe to the events
bluetoothManager.state.listen((state) async { bluetoothManager.state.listen((state) async {
// print('**************cur device status: $state');
switch (state) { switch (state) {
case BluetoothManager.CONNECTED: case BluetoothManager.CONNECTED:
print('********************* CONNECTED'); print('********************* CONNECTED');
...@@ -51,14 +92,13 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -51,14 +92,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);
// print('################## print send #############'); // TODO show message "Data sent"
// bluetoothManager.
} }
sleep3().then((dynamic printer) async { // TODO sending disconnect signal should be event-based
_sleep(3).then((dynamic printer) async {
print('@@@@DISCONNECTING......'); print('@@@@DISCONNECTING......');
await bluetoothManager.disconnect(); await bluetoothManager.disconnect();
}); });
_connected = true; _connected = true;
break; break;
case BluetoothManager.DISCONNECTED: case BluetoothManager.DISCONNECTED:
...@@ -69,11 +109,6 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -69,11 +109,6 @@ class _MyHomePageState extends State<MyHomePage> {
break; break;
} }
}); });
// TODO show message "Data sent"
} else {
// TODO show message "Can't connect to the device"
}
} }
@override @override
...@@ -82,13 +117,11 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -82,13 +117,11 @@ class _MyHomePageState extends State<MyHomePage> {
appBar: AppBar( appBar: AppBar(
title: Text(widget.title), title: Text(widget.title),
), ),
body: StreamBuilder<List<BluetoothDevice>>( body: ListView.builder(
stream: bluetoothManager.scanResults, itemCount: _devices.length,
initialData: [], itemBuilder: (BuildContext context, int index) {
builder: (c, snapshot) => Column( return InkWell(
children: snapshot.data onTap: () => _testPrint(_devices[index]),
.map((d) => InkWell(
onTap: () => _testPrint(d),
child: Column( child: Column(
children: <Widget>[ children: <Widget>[
Container( Container(
...@@ -104,8 +137,8 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -104,8 +137,8 @@ class _MyHomePageState extends State<MyHomePage> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Text(d.name ?? ''), Text(_devices[index].name ?? ''),
Text(d.address), Text(_devices[index].address),
Text( Text(
'Click to print a test receipt', 'Click to print a test receipt',
style: TextStyle(color: Colors.grey[700]), style: TextStyle(color: Colors.grey[700]),
...@@ -119,27 +152,17 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -119,27 +152,17 @@ class _MyHomePageState extends State<MyHomePage> {
Divider(), Divider(),
], ],
), ),
)) );
.toList(), }),
), floatingActionButton: _isScanning
), ? FloatingActionButton(
floatingActionButton: StreamBuilder<bool>(
stream: bluetoothManager.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data) {
return FloatingActionButton(
child: Icon(Icons.stop), child: Icon(Icons.stop),
onPressed: () => bluetoothManager.stopScan(), onPressed: _stopScanDevices,
backgroundColor: Colors.red, backgroundColor: Colors.red,
); )
} else { : FloatingActionButton(
return FloatingActionButton(
child: Icon(Icons.search), child: Icon(Icons.search),
onPressed: () => onPressed: _startScanDevices,
bluetoothManager.startScan(timeout: Duration(seconds: 4)));
}
},
), ),
); );
} }
......
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