print_service.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import 'dart:ffi';
  2. import 'dart:io';
  3. import 'package:device_info_plus/device_info_plus.dart';
  4. import 'package:lszlgl/utils/permission_utils.dart';
  5. import 'package:permission_handler/permission_handler.dart';
  6. import '../model/rsp/sample_task_rsp.dart';
  7. import '../plugin/bluetooth_plugin.dart';
  8. import '../router/my_navigator.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter/services.dart';
  11. import 'dict_service.dart';
  12. class PrintService {
  13. PrintService._();
  14. static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  15. static List<BlueDeviceInfo> connectedDeviceList = [];
  16. static List<String> connectedDeviceMacList = [];
  17. static AndroidDeviceInfo? deviceInfo;
  18. static Future<AndroidDeviceInfo?> getDeviceInfo() async {
  19. if (Platform.isIOS) {
  20. } else if(Platform.isAndroid) {
  21. if(deviceInfo != null) {
  22. deviceInfo;
  23. }
  24. AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
  25. deviceInfo = androidInfo;
  26. return deviceInfo;
  27. }
  28. }
  29. //是否打开蓝牙
  30. static Future<bool> isBleOpen() async {
  31. return await BluetoothPlugin.instance.isBleOpen();
  32. }
  33. //安卓版本是否大于等于31
  34. static Future<bool> isSDKIntGreaterOrEqual() async {
  35. return await BluetoothPlugin.instance.isSDKIntGreaterOrEqual31();
  36. }
  37. //是否支持蓝牙操作
  38. static Future<bool> canExecAction() async {
  39. if (await isBleOpen()) {
  40. bool allGranted = false;
  41. String permissionName = '';
  42. // permissionRequest
  43. if(await isSDKIntGreaterOrEqual()) {
  44. if (await PermissionHandler.handleWith(Permission.bluetoothScan)) {
  45. allGranted = true;
  46. } else {
  47. permissionName = Permission.bluetoothScan.toString();
  48. allGranted = false;
  49. }
  50. if(await PermissionHandler.handleWith(Permission.bluetoothConnect)) {
  51. allGranted = true;
  52. } else {
  53. permissionName = '$permissionName ${Permission.bluetoothConnect}';
  54. allGranted = false;
  55. }
  56. }
  57. //所有权限申请通过
  58. if (await PermissionHandler.handleWith(Permission.location)) {
  59. allGranted = true;
  60. } else {
  61. permissionName = Permission.location.toString();
  62. allGranted = false;
  63. }
  64. if(allGranted) {
  65. return true;
  66. } else {
  67. MyNavigator.showToast('权限打开失败:$permissionName');
  68. return false;
  69. }
  70. } else {
  71. // 蓝牙未开启
  72. MyNavigator.showToast('蓝牙未开启');
  73. return false;
  74. }
  75. }
  76. //开始扫描
  77. static Future<int> startBluetoothDiscovery() async {
  78. if(await canExecAction()) {
  79. return await BluetoothPlugin.instance.startBluetoothDiscovery();
  80. } else {
  81. return 0;
  82. }
  83. }
  84. //开始配对
  85. static Future<int> startBluetoothPair(BlueDeviceInfo deviceInfo) async {
  86. if(await canExecAction()) {
  87. return await BluetoothPlugin.instance.startBluetoothPair(deviceInfo);
  88. } else {
  89. return 0;
  90. }
  91. }
  92. //开始连接
  93. static Future<int> startBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  94. if(await canExecAction()) {
  95. return await BluetoothPlugin.instance.startBluetoothConnect(deviceInfo);
  96. } else {
  97. return 0;
  98. }
  99. }
  100. //断开连接
  101. static Future<int> endBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  102. if(await canExecAction()) {
  103. return await BluetoothPlugin.instance.endBluetoothConnect(deviceInfo);
  104. } else {
  105. return 0;
  106. }
  107. }
  108. //是否有连接中的设备
  109. static Future<bool> hasBluetoothConnectDevice() async {
  110. if(await canExecAction()) {
  111. return await BluetoothPlugin.instance.hasBluetoothConnectDevice();
  112. } else {
  113. return false;
  114. }
  115. }
  116. // 打印打印图片和文字只给BTP设备使用
  117. static Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
  118. if(await canExecAction()) {
  119. return await BluetoothPlugin.instance.startBluetoothPrintBitMapAndText(bytes, textList);
  120. } else {
  121. return false;
  122. }
  123. }
  124. //开始打印图片
  125. static Future<bool> startBluetoothPrintBitMap(Uint8List bytes) async {
  126. if(await canExecAction()) {
  127. return await BluetoothPlugin.instance.startBluetoothPrintBitMap(bytes);
  128. } else {
  129. return false;
  130. }
  131. }
  132. //开始打印文字
  133. static Future<bool> startBluetoothPrintText(List<String> textList) async {
  134. //
  135. if(await canExecAction()) {
  136. return await BluetoothPlugin.instance.startBluetoothPrintText(textList);
  137. } else {
  138. return false;
  139. }
  140. }
  141. //开始打印二维码和文本
  142. static Future<bool> startBluetoothPrintBarCodeWithText(String barCode, String text) async {
  143. if(await canExecAction()) {
  144. return await BluetoothPlugin.instance.startBluetoothPrintBarCodeWithText(barCode, text);
  145. } else {
  146. return false;
  147. }
  148. }
  149. static List<String> getPrintTextListWithSampleTaskItem(SampleTaskItem? data) {
  150. List<String> textList = [];
  151. textList.addAll(["采样品种:${data?.cypzName ?? ''}","种植品种:${data?.jtpzmc ?? ''}"]);
  152. textList.addAll(["${data?.shengXzqhName ?? ''}${data?.shiXzqhName ?? ''}${data?.quXzqhName ?? ''}${data?.xiangXzqhName ?? ''}${data?.cunXzqhName ?? ''}"]);
  153. textList.addAll(["扦样人员:${data?.dgryName ?? ''}"]);
  154. textList.addAll(["收获时间:${data?.shsj ?? ''}","扦样时间:${data?.qysj ?? ''}"]);
  155. textList.addAll(["扦样数量:${data?.qysl ?? ''}kg","样品层级:${DictService.getLabel(DictType.ypdj, value: data?.ypdj)}"]);
  156. textList.addAll(["种植面积:${data?.zzmj ?? ''}亩","代表数量:${(data?.qydbsl ?? '').toString()}公斤"]);
  157. return textList;
  158. }
  159. }