bx_btprinter 0.0.2
bx_btprinter: ^0.0.2 copied to clipboard
This Flutter plugin allows you to connect to a Bixolon printer via Bluetooth and send print commands. The files in the libs folder belong to Bixolon.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:bx_btprinter/btprinter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _btprinterPlugin = Btprinter();
List<String?> _devices = [];
final GlobalKey<ScaffoldState> _scaffoldKey =
GlobalKey<ScaffoldState>(); // Scaffold의 key
@override
void initState() {
super.initState();
initBle();
}
void initBle() async {
await _getBle();
}
Future<void> _getBle() async {
String? value;
try {
value = await _btprinterPlugin.getBtPermission();
} on PlatformException catch (e) {
value = 'native code error: ${e.message}';
}
print(value);
_showSnackBar(value!, value == "success" ? true : false);
}
Future<void> _print(String? device) async {
List<String> deviceInfo = device!.split('(');
String logicalName = deviceInfo[0].split("_")[0].trim();
String address = deviceInfo[1].replaceAll(')', '').trim();
// 텍스트 출력을 위한 요청 객체 생성
List<Map<String, dynamic>> textRequests = [
{
'text': 'Alignment Left\n',
'textAlignment': Btprinter.ALIGNMENT_LEFT,
'textAttribute': Btprinter.ATTRIBUTE_NORMAL,
'textSize': 1,
},
{
'text': 'Alignment Center\n',
'textAlignment': Btprinter.ALIGNMENT_CENTER,
'textAttribute': Btprinter.ATTRIBUTE_NORMAL,
'textSize': 1,
},
{
'text': 'Alignment Right\n',
'textAlignment': Btprinter.ALIGNMENT_RIGHT,
'textAttribute': Btprinter.ATTRIBUTE_NORMAL,
'textSize': 1,
},
{
'text': 'Point Size 2\n',
'textSize': 2,
},
{
'text': 'Font Reserve On 2\n',
'textAttribute': Btprinter.ATTRIBUTE_REVERSE,
'textSize': 1,
},
{
'text': 'Font Bold On\n',
'textAttribute': Btprinter.ATTRIBUTE_BOLD,
'textSize': 1,
},
{
'text': 'Font UnderLine\n',
'textAttribute': Btprinter.ATTRIBUTE_UNDERLINE,
'textSize': 1,
},
];
List<Map<String, dynamic>> barcodeRequests = [
{
'data': ' ',
'symbology': Btprinter.BARCODE_TYPE_QRCODE,
'width': 2,
'height': 2,
'alignment': Btprinter.ALIGNMENT_CENTER,
'hri': Btprinter.BARCODE_HRI_NONE,
},
{
'data': '123456789012',
'symbology': Btprinter.BARCODE_TYPE_ITF,
'alignment': Btprinter.ALIGNMENT_CENTER,
'hri': Btprinter.BARCODE_HRI_BELOW,
},
];
String? value;
try {
value =
await _btprinterPlugin.printText(textRequests, logicalName, address);
value = await _btprinterPlugin.printBarcode(
barcodeRequests, logicalName, address);
} on PlatformException catch (e) {
value = "${e.message}";
}
print(value);
_showSnackBar(value!, value == "success" ? true : false);
}
void _getPairedDevices() async {
String value;
try {
List<Object?> devices = await _btprinterPlugin.getPairedDevices();
setState(() {
_devices = devices
.cast<String>()
.where((device) => device.startsWith('SPP')) //SPP로 시작하는 장치만 가져옴
.toList();
});
} on PlatformException catch (e) {
value = "${e.message}";
print(value);
_showSnackBar(value, false);
}
}
void _showSnackBar(String message, bool isSuccess) {
final snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 5),
behavior: SnackBarBehavior.floating,
shape: const StadiumBorder(),
backgroundColor: Colors.blue,
);
final errSnackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 5),
behavior: SnackBarBehavior.floating,
shape: const StadiumBorder(),
backgroundColor: Colors.red,
);
if (isSuccess) {
ScaffoldMessenger.of(_scaffoldKey.currentContext!).showSnackBar(snackBar);
} else {
ScaffoldMessenger.of(_scaffoldKey.currentContext!)
.showSnackBar(errSnackBar);
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _scaffoldKey, //Scaffold에 key 할당
appBar: AppBar(
title: const Text('Bluetooth Device List'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _getPairedDevices,
child: const Text('블루투스 기기 목록 조회')),
Container(
height: 200,
child: _devices == null || _devices.isEmpty
? const Center(
child: Text('연결된 기기를 찾을 수 없습니다.'),
)
: ListView.builder(
itemCount: _devices.length,
itemBuilder: (context, index) {
String? deviceName =
_devices[index]?.split('(')[0].trim();
return ListTile(
title: Text(deviceName ?? ''),
onTap: () async {
await _print(_devices[index]);
},
);
},
),
)
],
),
),
),
);
}
}