print_service.dart 5.5 KB

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