123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import 'dart:io';
- import 'package:device_info_plus/device_info_plus.dart';
- import 'package:lszlgl/utils/permission_utils.dart';
- import 'package:permission_handler/permission_handler.dart';
- import '../model/rsp/sample_task_rsp.dart';
- import '../plugin/bluetooth_plugin.dart';
- import '../router/my_navigator.dart';
- import 'package:flutter/services.dart';
- import 'dict_service.dart';
- class PrintService {
- PrintService._();
- static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
- static List<BlueDeviceInfo> connectedDeviceList = [];
- static List<String> connectedDeviceMacList = [];
- static AndroidDeviceInfo? deviceInfo;
- static Future<AndroidDeviceInfo?> getDeviceInfo() async {
- if (Platform.isIOS) {
- } else if(Platform.isAndroid) {
- if(deviceInfo != null) {
- deviceInfo;
- }
- AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
- deviceInfo = androidInfo;
- return deviceInfo;
- }
- }
- //是否打开蓝牙
- static Future<bool> isBleOpen() async {
- return await BluetoothPlugin.instance.isBleOpen();
- }
- //安卓版本是否大于等于31
- static Future<bool> isSDKIntGreaterOrEqual() async {
- return await BluetoothPlugin.instance.isSDKIntGreaterOrEqual31();
- }
- //是否支持蓝牙操作
- static Future<bool> canExecAction() async {
- if (await isBleOpen()) {
- bool allGranted = false;
- String permissionName = '';
- // permissionRequest
- if(await isSDKIntGreaterOrEqual()) {
- if (await PermissionHandler.handleWith(Permission.bluetoothScan)) {
- allGranted = true;
- } else {
- permissionName = Permission.bluetoothScan.toString();
- allGranted = false;
- }
- if(await PermissionHandler.handleWith(Permission.bluetoothConnect)) {
- allGranted = true;
- } else {
- permissionName = '$permissionName ${Permission.bluetoothConnect}';
- allGranted = false;
- }
- }
- //所有权限申请通过
- if (await PermissionHandler.handleWith(Permission.location)) {
- allGranted = true;
- } else {
- permissionName = Permission.location.toString();
- allGranted = false;
- }
- if(allGranted) {
- return true;
- } else {
- MyNavigator.showToast('权限打开失败:$permissionName');
- return false;
- }
- } else {
- // 蓝牙未开启
- MyNavigator.showToast('蓝牙未开启');
- return false;
- }
- }
- //开始扫描
- static Future<int> startBluetoothDiscovery() async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothDiscovery();
- } else {
- return 0;
- }
- }
- //开始配对
- static Future<int> startBluetoothPair(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPair(deviceInfo);
- } else {
- return 0;
- }
- }
- //开始连接
- static Future<int> startBluetoothConnect(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothConnect(deviceInfo);
- } else {
- return 0;
- }
- }
- //断开连接
- static Future<int> endBluetoothConnect(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.endBluetoothConnect(deviceInfo);
- } else {
- return 0;
- }
- }
- //是否有连接中的设备
- static Future<bool> hasBluetoothConnectDevice() async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.hasBluetoothConnectDevice();
- } else {
- return false;
- }
- }
- // 打印打印图片和文字只给BTP设备使用
- static Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPrintBitMapAndText(bytes, textList);
- } else {
- return false;
- }
- }
- //开始打印图片
- static Future<bool> startBluetoothPrintBitMap(Uint8List bytes) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPrintBitMap(bytes);
- } else {
- return false;
- }
- }
- //开始打印文字
- static Future<bool> startBluetoothPrintText(List<String> textList) async {
- //
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPrintText(textList);
- } else {
- return false;
- }
- }
- //开始打印二维码和文本
- static Future<bool> startBluetoothPrintBarCodeWithText(String barCode, String text) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPrintBarCodeWithText(barCode, text);
- } else {
- return false;
- }
- }
- static List<String> getPrintTextListWithSampleTaskItem(SampleTaskItem? data) {
- List<String> textList = [];
- textList.addAll(["采样品种:${data?.cypzName ?? ''}","种植品种:${data?.jtpzmc ?? ''}"]);
- textList.addAll(["${data?.shengXzqhName ?? ''}${data?.shiXzqhName ?? ''}${data?.quXzqhName ?? ''}${data?.xiangXzqhName ?? ''}${data?.cunXzqhName ?? ''}"]);
- textList.addAll(["扦样人员:${data?.dgryName ?? ''}"]);
- textList.addAll(["收获时间:${data?.shsj ?? ''}","扦样时间:${data?.qysj ?? ''}"]);
- textList.addAll(["扦样数量:${data?.qysl ?? ''}kg","样品层级:${DictService.getLabel(DictType.ypdj, value: data?.ypdj)}"]);
- textList.addAll(["种植面积:${data?.zzmj ?? ''}亩","代表数量:${(data?.qydbsl ?? '').toString()}公斤"]);
- return textList;
- }
- }
|