import 'dart:ffi'; 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 '../plugin/bluetooth_plugin.dart'; import '../router/my_navigator.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.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; } } //开始打印图片 static Future startBluetoothPrintBitMap(Uint8List bytes) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintBitMap(bytes); } else { return false; } } //开始打印二维码和文本 static Future startBluetoothPrintBarCodeWithText(String barCode, String text) async { if(await canExecAction()) { return await BluetoothPlugin.instance.startBluetoothPrintBarCodeWithText(barCode, text); } else { return false; } } }