Closed gabbygreat closed 6 months ago
Nice work man.
Configuring the package was a breeze, but, I am currectly facing an issue with printing
`class _BluetoothShareState extends State { BluetoothPrintPlus bluetoothPrint = BluetoothPrintPlus.instance; BluetoothDevice? _device;
@OverRide void initState() { super.initState(); _startScan(); }
void _startScan() async { bluetoothPrint.startScan(timeout: const Duration(seconds: 20)); bluetoothPrint.state.listen((event) { setState(() {}); print('EVENT IS $event'); //0 disconnected //1 connected //11 turn on scan //12 turn on steady //13 turn off //10 turn off steady }); }
Future _printReceipt() async { await bluetoothPrint.connect(_device!);
final tscCommand = TscCommand(); await tscCommand.cleanCommand(); await tscCommand.cls(); await tscCommand.size(width: 10, height: 20); await tscCommand.text(content: "Title", x: 10, y: 10); await tscCommand.text(content: "Hello", x: 10, y: 60, xMulti: 2, yMulti: 2); await tscCommand.print(1); final cmd = await tscCommand.getCommand(); if (cmd == null) return; try { await bluetoothPrint.write(cmd); await bluetoothPrint.disconnect(); ShowFlushBar.showSuccess( message: 'Sent', flushbarPosition: FlushbarPosition.TOP, ); } on PlatformException { ShowFlushBar.showError('Failed to scan'); } catch (error) { ShowFlushBar.showError('${error.runtimeType}'); }
}
@OverRide void dispose() async { bluetoothPrint.disconnect(); super.dispose(); }
@OverRide Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( title: 'Share to printer', action: [ if (_device != null) IconButton( onPressed: _printReceipt, icon: const Icon( Icons.check_circle_outline_rounded, ), ) ], ), body: FutureBuilder( future: bluetoothPrint.isOn, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CustomLoader(); } if (snapshot.data != true) { return const Center( child: Text( 'Turn on your bluetooth', ), ); } else { return StreamBuilder
( stream: bluetoothPrint.scanResults, builder: (c, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CustomLoader(); } else if (snapshot.hasError || snapshot.data == null) { return const Center( child: Text('Failed to scan'), ); } else { if (snapshot.data!.isEmpty) { return const EmptyWidget( text: 'No bluetooth printer found', ); } return ListView( children: snapshot.data!.map( (d) { return ListTile( title: Text(d.name ?? 'Unknown'), subtitle: Text(d.address ?? 'Unknown'), onTap: () async { setState(() { if (d == _device) { _device = null; } else { _device = d; } }); }, trailing: _device != null && _device?.address == d.address ? const Icon( Icons.check, color: Colors.green, ) : null, ); }, ).toList(), ); } }, ); } }, ), ); } } ` This is my code, but, I don't know why it does not print nor throw any errors
The logs when I print reciept, just incase it will help
your code: await bluetoothPrint.write(cmd); await bluetoothPrint.disconnect();
disconnect ???? why ? try remove :await bluetoothPrint.disconnect();
Actually, if I don't disconnect, that is when I get the PlatformException
Nice work man.
Configuring the package was a breeze, but, I am currectly facing an issue with printing
`class _BluetoothShareState extends State {
BluetoothPrintPlus bluetoothPrint = BluetoothPrintPlus.instance;
BluetoothDevice? _device;
@override void initState() { super.initState(); _startScan(); }
void _startScan() async { bluetoothPrint.startScan(timeout: const Duration(seconds: 20)); bluetoothPrint.state.listen((event) { setState(() {}); print('EVENT IS $event'); //0 disconnected //1 connected //11 turn on scan //12 turn on steady //13 turn off //10 turn off steady }); }
Future _printReceipt() async {
await bluetoothPrint.connect(_device!);
}
@override void dispose() async { bluetoothPrint.disconnect(); super.dispose(); }
@override Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar( title: 'Share to printer', action: [ if (_device != null) IconButton( onPressed: _printReceipt, icon: const Icon( Icons.check_circle_outline_rounded, ), ) ], ), body: FutureBuilder( future: bluetoothPrint.isOn, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CustomLoader(); } if (snapshot.data != true) { return const Center( child: Text( 'Turn on your bluetooth', ), ); } else { return StreamBuilder<List>(
stream: bluetoothPrint.scanResults,
builder: (c, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CustomLoader();
} else if (snapshot.hasError || snapshot.data == null) {
return const Center(
child: Text('Failed to scan'),
);
} else {
if (snapshot.data!.isEmpty) {
return const EmptyWidget(
text: 'No bluetooth printer found',
);
}
return ListView(
children: snapshot.data!.map(
(d) {
return ListTile(
title: Text(d.name ?? 'Unknown'),
subtitle: Text(d.address ?? 'Unknown'),
onTap: () async {
setState(() {
if (d == _device) {
_device = null;
} else {
_device = d;
}
});
},
trailing:
_device != null && _device?.address == d.address
? const Icon(
Icons.check,
color: Colors.green,
)
: null,
);
},
).toList(),
);
}
},
);
}
},
),
);
}
}
`
This is my code, but, I don't know why it does not print nor throw any errors
The logs when I print reciept, just incase it will help