| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import 'package:lszlgl/utils/permission_utils.dart';
- import 'package:permission_handler/permission_handler.dart';
- import '../plugin/bluetooth_plugin.dart';
- import '../router/my_navigator.dart';
- class PrintService {
- PrintService._();
- //是否打开蓝牙
- static Future<bool> isBleOpen() async {
- return await BluetoothPlugin.instance.isBleOpen();
- }
- //安卓版本是否大于等于31
- static Future<bool> isSDKIntGreaterOrEqual() async {
- return await BluetoothPlugin.instance.isSDKIntGreaterOrEqual31();
- }
- //是否支持蓝牙操作
- static Future<bool> 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<int> startBluetoothDiscovery() async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothDiscovery();
- } else {
- return 0;
- }
- }
- //开始配对
- static Future<int> startBluetoothPair(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPair(deviceInfo);
- } else {
- return 0;
- }
- }
- //开始连接
- static Future<int> startBluetoothConnect(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothConnect(deviceInfo);
- } else {
- return 0;
- }
- }
- //断开连接
- static Future<int> endBluetoothConnect(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.endBluetoothConnect(deviceInfo);
- } else {
- return 0;
- }
- }
- //开始打印
- static Future<int> startBluetoothPrint(BlueDeviceInfo deviceInfo) async {
- if(await canExecAction()) {
- return await BluetoothPlugin.instance.startBluetoothPrint(deviceInfo);
- } else {
- return 0;
- }
- }
- }
|