blueprint_flutter 0.0.1 blueprint_flutter: ^0.0.1 copied to clipboard
plugin de impresion por bluetooh.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:blueprint_flutter/blueprint_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bluetooth scanner',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Bluetooth Scanner'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
BluetoothManager bluetoothManager = BluetoothManager.instance;
bool _connected = false;
BluetoothDevice? _device;
String tips = 'no device connect';
@override
void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) async => await initBluetooth());
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initBluetooth() async {
var bluetoothIsOn = await bluetoothManager.isOn;
if (bluetoothIsOn) {
bluetoothManager.startScan(timeout: Duration(seconds: 4));
bool isConnected = await bluetoothManager.isConnected;
bluetoothManager.state.listen((state) {
print('cur device status: $state');
switch (state) {
case BluetoothManager.CONNECTED:
setState(() {
_connected = true;
tips = 'connect success';
});
break;
case BluetoothManager.DISCONNECTED:
setState(() {
_connected = false;
tips = 'disconnect success';
});
break;
default:
break;
}
});
if (!mounted) return;
if (isConnected) {
setState(() {
_connected = true;
});
}
} else {
print("Encienda el bluetooth");
}
}
void _onConnect() async {
if (_device != null && _device!.address != null) {
await bluetoothManager.connect(_device!);
} else {
setState(() {
tips = 'please select device';
});
print('please select device');
}
}
void _onDisconnect() async {
await bluetoothManager.disconnect();
}
/* void _onDestroy() async {
await bluetoothManager.destroy();
} */
void _sendData() async {
List<int> bytes = latin1.encode('Hello world!\n\n\n').toList();
// Set codetable west. Add import 'dart:typed_data';
// List<int> bytes = Uint8List.fromList(List.from('\x1Bt'.codeUnits)..add(6));
// Text with special characters
// bytes += latin1.encode('blåbærgrød\n\n\n');
await bluetoothManager.writeData(bytes);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: RefreshIndicator(
onRefresh: () =>
bluetoothManager.startScan(timeout: const Duration(seconds: 4)),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 10, horizontal: 10),
child: Text(tips),
),
],
),
const Divider(),
StreamBuilder<List<BluetoothDevice>>(
stream: bluetoothManager.scanResults,
initialData: const [],
builder: (c, snapshot) => Column(
children: snapshot.data!
.map((d) => ListTile(
title: Text(d.name ?? ''),
subtitle: Text(d.address ?? ""),
onTap: () async {
setState(() {
_device = d;
});
},
trailing:
_device != null && _device!.address == d.address
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
))
.toList(),
),
),
const Divider(),
Container(
padding: const EdgeInsets.fromLTRB(20, 5, 20, 10),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
onPressed: _connected ? null : _onConnect,
child: const Text('connect'),
),
const SizedBox(width: 10.0),
OutlinedButton(
onPressed: _connected ? _onDisconnect : null,
child: const Text('disconnect'),
),
],
),
OutlinedButton(
onPressed: _connected ? _sendData : null,
child: const Text('Send test data'),
),
],
),
)
],
),
),
),
floatingActionButton: StreamBuilder<bool>(
stream: bluetoothManager.isScanning,
initialData: false,
builder: (c, snapshot) {
if (snapshot.data!) {
return FloatingActionButton(
onPressed: () => bluetoothManager.stopScan(),
backgroundColor: Colors.red,
child: const Icon(Icons.stop),
);
} else {
return FloatingActionButton(
child: const Icon(Icons.search),
onPressed: () => bluetoothManager.startScan(
timeout: const Duration(seconds: 4)));
}
},
),
);
}
}