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,51 +30,85 @@ class MyHomePage extends StatefulWidget { ...@@ -29,51 +30,85 @@ 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 = [];
});
void _testPrint(BluetoothDevice printer) async { bluetoothManager.startScan(timeout: Duration(seconds: 4));
// print('Test print.... name: ${printer.name}');
if (printer != null && printer.address != null) {
// Connect
await bluetoothManager.connect(printer);
// Subscribe to the events _scanResultsSubscription =
bluetoothManager.state.listen((state) async { bluetoothManager.scanResults.listen((devices) async {
// print('**************cur device status: $state'); setState(() {
switch (state) { _devices = devices;
case BluetoothManager.CONNECTED: });
print('********************* CONNECTED'); });
// to avoid double call
if (!_connected) {
print('@@@@SEND DATA......');
final List<int> bytes = latin1.encode('test!\n\n\n').toList();
await bluetoothManager.writeData(bytes);
// print('################## print send #############');
// bluetoothManager.
}
sleep3().then((dynamic printer) async {
print('@@@@DISCONNECTING......');
await bluetoothManager.disconnect();
});
_connected = true; _isScanningSubscription =
break; bluetoothManager.isScanning.listen((isScanningCurrent) async {
case BluetoothManager.DISCONNECTED: // if isScanning value changed (scan just stopped)
print('********************* DISCONNECTED'); if (_isScanning && !isScanningCurrent) {
_connected = false; _scanResultsSubscription.cancel();
break; _isScanningSubscription.cancel();
default: }
break; setState(() {
} _isScanning = isScanningCurrent;
}); });
});
}
// TODO show message "Data sent" void _stopScanDevices() {
} else { bluetoothManager.stopScan();
// TODO show message "Can't connect to the device" }
}
Future _sleep(int seconds) {
return Future<dynamic>.delayed(Duration(seconds: seconds));
}
void _testPrint(BluetoothDevice printer) async {
// We have to rescan before connecting, otherwise we can connect only once
await bluetoothManager.startScan(timeout: Duration(seconds: 1));
await bluetoothManager.stopScan();
// Connect
await bluetoothManager.connect(printer);
// Subscribe to the events
bluetoothManager.state.listen((state) async {
switch (state) {
case BluetoothManager.CONNECTED:
print('********************* CONNECTED');
// to avoid double call
if (!_connected) {
print('@@@@SEND DATA......');
final List<int> bytes = latin1.encode('test!\n\n\n').toList();
await bluetoothManager.writeData(bytes);
// TODO show message "Data sent"
}
// TODO sending disconnect signal should be event-based
_sleep(3).then((dynamic printer) async {
print('@@@@DISCONNECTING......');
await bluetoothManager.disconnect();
});
_connected = true;
break;
case BluetoothManager.DISCONNECTED:
print('********************* DISCONNECTED');
_connected = false;
break;
default:
break;
}
});
} }
@override @override
...@@ -82,65 +117,53 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -82,65 +117,53 @@ 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( child: Column(
onTap: () => _testPrint(d), children: <Widget>[
child: Column( Container(
height: 60,
padding: EdgeInsets.only(left: 10),
alignment: Alignment.centerLeft,
child: Row(
children: <Widget>[ children: <Widget>[
Container( Icon(Icons.print),
height: 60, SizedBox(width: 10),
padding: EdgeInsets.only(left: 10), Expanded(
alignment: Alignment.centerLeft, child: Column(
child: Row( crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Icon(Icons.print), Text(_devices[index].name ?? ''),
SizedBox(width: 10), Text(_devices[index].address),
Expanded( Text(
child: Column( 'Click to print a test receipt',
crossAxisAlignment: CrossAxisAlignment.start, style: TextStyle(color: Colors.grey[700]),
mainAxisAlignment: MainAxisAlignment.center, ),
children: <Widget>[
Text(d.name ?? ''),
Text(d.address),
Text(
'Click to print a test receipt',
style: TextStyle(color: Colors.grey[700]),
),
],
),
)
], ],
), ),
), )
Divider(),
], ],
), ),
)) ),
.toList(), Divider(),
), ],
), ),
floatingActionButton: StreamBuilder<bool>( );
stream: bluetoothManager.isScanning, }),
initialData: false, floatingActionButton: _isScanning
builder: (c, snapshot) { ? FloatingActionButton(
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: _startScanDevices,
onPressed: () => ),
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