print_service.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:lszlgl/utils/permission_utils.dart';
  2. import 'package:permission_handler/permission_handler.dart';
  3. import '../plugin/bluetooth_plugin.dart';
  4. import '../router/my_navigator.dart';
  5. class PrintService {
  6. PrintService._();
  7. //是否打开蓝牙
  8. static Future<bool> isBleOpen() async {
  9. return await BluetoothPlugin.instance.isBleOpen();
  10. }
  11. //安卓版本是否大于等于31
  12. static Future<bool> isSDKIntGreaterOrEqual() async {
  13. return await BluetoothPlugin.instance.isSDKIntGreaterOrEqual31();
  14. }
  15. //是否支持蓝牙操作
  16. static Future<bool> canExecAction() async {
  17. if (await isBleOpen()) {
  18. bool allGranted = false;
  19. String permissionName = '';
  20. // permissionRequest
  21. if(await isSDKIntGreaterOrEqual()) {
  22. if (await PermissionHandler.handleWith(Permission.bluetoothScan)) {
  23. allGranted = true;
  24. } else {
  25. permissionName = Permission.bluetoothScan.toString();
  26. allGranted = false;
  27. }
  28. if(await PermissionHandler.handleWith(Permission.bluetoothConnect)) {
  29. allGranted = true;
  30. } else {
  31. permissionName = '$permissionName ${Permission.bluetoothConnect}';
  32. allGranted = false;
  33. }
  34. }
  35. //所有权限申请通过
  36. if (await PermissionHandler.handleWith(Permission.location)) {
  37. allGranted = true;
  38. } else {
  39. permissionName = Permission.location.toString();
  40. allGranted = false;
  41. }
  42. if(allGranted) {
  43. return true;
  44. } else {
  45. MyNavigator.showToast('权限打开失败:$permissionName');
  46. return false;
  47. }
  48. } else {
  49. // 蓝牙未开启
  50. MyNavigator.showToast('蓝牙未开启');
  51. return false;
  52. }
  53. }
  54. //开始扫描
  55. static Future<int> startBluetoothDiscovery() async {
  56. if(await canExecAction()) {
  57. return await BluetoothPlugin.instance.startBluetoothDiscovery();
  58. } else {
  59. return 0;
  60. }
  61. }
  62. //开始配对
  63. static Future<int> startBluetoothPair(BlueDeviceInfo deviceInfo) async {
  64. if(await canExecAction()) {
  65. return await BluetoothPlugin.instance.startBluetoothPair(deviceInfo);
  66. } else {
  67. return 0;
  68. }
  69. }
  70. //开始连接
  71. static Future<int> startBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  72. if(await canExecAction()) {
  73. return await BluetoothPlugin.instance.startBluetoothConnect(deviceInfo);
  74. } else {
  75. return 0;
  76. }
  77. }
  78. //断开连接
  79. static Future<int> endBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  80. if(await canExecAction()) {
  81. return await BluetoothPlugin.instance.endBluetoothConnect(deviceInfo);
  82. } else {
  83. return 0;
  84. }
  85. }
  86. //开始打印
  87. static Future<int> startBluetoothPrint(BlueDeviceInfo deviceInfo) async {
  88. if(await canExecAction()) {
  89. return await BluetoothPlugin.instance.startBluetoothPrint(deviceInfo);
  90. } else {
  91. return 0;
  92. }
  93. }
  94. }