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 connectedDeviceList = []; static List connectedDeviceMacList = []; static AndroidDeviceInfo? deviceInfo; static Future getDeviceInfo() async { if (Platform.isIOS) { } else if(Platform.isAndroid) { if(deviceInfo != null) { deviceInfo; } AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo; deviceInfo = androidInfo; return deviceInfo; } } //是否打开蓝牙 static Future isBleOpen() async { return await BluetoothPlugin.instance.isBleOpen(); } //安卓版本是否大于等于31 static Future isSDKIntGreaterOrEqual() async { return await BluetoothPlugin.instance.isSDKIntGreaterOrEqual31(); } //是否支持蓝牙操作 static Future 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 startBluetoothDiscovery() async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothDiscovery(); } else { return 0; } } //开始配对 static Future startBluetoothPair(BlueDeviceInfo deviceInfo) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPair(deviceInfo); } else { return 0; } } //开始连接 static Future startBluetoothConnect(BlueDeviceInfo deviceInfo) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothConnect(deviceInfo); } else { return 0; } } //断开连接 static Future endBluetoothConnect(BlueDeviceInfo deviceInfo) async { if(await canExecAction()) { return await BluetoothPlugin.instance.endBluetoothConnect(deviceInfo); } else { return 0; } } //是否有连接中的设备 static Future hasBluetoothConnectDevice() async { if(await canExecAction()) { return await BluetoothPlugin.instance.hasBluetoothConnectDevice(); } else { return false; } } // 打印打印图片和文字只给BTP设备使用 static Future startBluetoothPrintBitMapAndText(Uint8List bytes,List textList) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintBitMapAndText(bytes, textList); } else { return false; } } //开始打印图片 static Future startBluetoothPrintBitMap(Uint8List bytes) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintBitMap(bytes); } else { return false; } } //开始打印文字 static Future startBluetoothPrintText(List textList) async { // if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintText(textList); } else { return false; } } //开始打印二维码和文本 static Future startBluetoothPrintBarCodeWithText(String barCode, String text,List textList) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintBarCodeWithText(barCode, text, textList); } else { return false; } } static List getPrintTextListWithSampleTaskItem(SampleTaskItem? data) { List 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; } }