You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:hy_printer/device.dart';
class HyPrinter {
static const MethodChannel _channel = MethodChannel('hy_printer');
static FlutterBlue flutterBlue = FlutterBlue.instance;
static void init() {}
static Future<List<Device>> getDeiveces() async {
var devices = <Device>[];
await flutterBlue.startScan(timeout: const Duration(seconds: 4));
flutterBlue.scanResults.listen((results) {
for (ScanResult r in results) {
//过滤名称为空的蓝牙
if (r.device.name.isNotEmpty) {
devices.add(Device(name: r.device.name, address: r.device.id.id));
}
}
});
await flutterBlue.stopScan();
return devices;
}
static Future<bool> blutIsOn() async {
var result = await flutterBlue.isOn;
return result;
}
///0连接成功
///-1连接超时
///-2地址格式错误
///-3打印机与sdk不匹配
///-4连接失败
static Future<int> connect(String address) async {
int result = await _channel.invokeMethod('connect', {'address': address});
return result;
}
static Future<bool> disConnect() async {
bool result = await _channel.invokeMethod('disConnect');
return result;
}
///0:打印机正常
///-1发送失败
///2缺纸
///3开盖
static Future<int> getStatus() async {
int result = await _channel.invokeMethod(
'getStatus',
);
return result;
}
static Future<int> printBarCode(
int direction,
int type,
String width,
String ratio,
String height,
String x,
String y,
bool undertext,
String size,
String offset,
String data,
) async {
int result = await _channel.invokeMethod('printBarCode', {
'direction': direction,
'type': type,
'width': width,
'ratio': ratio,
'height': height,
'x': x,
'y': y,
'undertext': undertext,
'size': size,
'offset': offset,
'data': data,
});
return result;
}
static Future<int> printLine(
String x0,
String y0,
String x1,
String y1,
) async {
int result = await _channel
.invokeMethod('printLine', {'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1});
return result;
}
static Future<int> align(String align) async {
int result = await _channel.invokeMethod('align', {'align': align});
return result;
}
static Future<int> setBold(String bold) async {
int result = await _channel.invokeMethod('setBold', {'bold': bold});
return result;
}
static Future<int> prefeed(String prefeed) async {
int result = await _channel.invokeMethod('setBold', {'prefeed': prefeed});
return result;
}
/// 'code': code, 箱号
// 'fbaCode': fbaCode, fba号/非fba传空字符串
// 'channel': channel,渠道名称
// 'country': country,目的国
// 'count': count,货件数量
// 'hasPlan':hasPlan 有无计划
static Future<int> printAsOrder(
{required String code,
required String fbaCode,
required String channel,
required String country,
required String count,
required bool hasPlan}) async {
int result = await _channel.invokeMethod('printAsOrder', {
'code': code,
'fbaCode': fbaCode,
'channel': channel,
'country': country,
'count': count,
'hasPlan': hasPlan
});
return result;
}
}