import 'dart:io'; import 'package:flutter/services.dart'; import 'package:logger/logger.dart'; import '../main.dart'; import '../router/my_navigator.dart'; class BluetoothPlugin { factory BluetoothPlugin() => _instance; BluetoothPlugin._(); static final BluetoothPlugin _instance = BluetoothPlugin._(); static BluetoothPlugin get instance => _instance; final String delimiterStr = "_NIMBOT_"; final MethodChannel _channel = const MethodChannel("io.flutter.plugins/bluetooth/methods"); final EventChannel _onReceiveDataStream = const EventChannel('io.flutter.plugins/bluetooth/onDiscoveryDevice'); final EventChannel _onDeviceStateStream = const EventChannel('io.flutter.plugins/bluetooth/onDeviceState'); //是否打开蓝牙 Future isBleOpen() async { if (Platform.isIOS) { return true; } return _channel.invokeMethod('isBleOpen').then((d) => d); } //安卓版本是否大于等于31 Future isSDKIntGreaterOrEqual31() async { if (Platform.isIOS) { return true; } return _channel.invokeMethod('isSDKIntGreaterOrEqual').then((d) => d); } //开始扫描 Future startBluetoothDiscovery() async { if (Platform.isIOS) { return 0; } return _channel.invokeMethod('startBluetoothDiscovery').then((d) => d); } //开始配对 Future startBluetoothPair(BlueDeviceInfo deviceInfo) async { if (Platform.isIOS) { return 0; } String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState; return _channel.invokeMethod('startBluetoothPair', deviceInfoStr).then((d) => d); } //开始连接 Future startBluetoothConnect(BlueDeviceInfo deviceInfo) async { if (Platform.isIOS) { return 0; } String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState; return _channel.invokeMethod('startBluetoothConnect', deviceInfoStr).then((d) => d); } //断开连接 Future endBluetoothConnect(BlueDeviceInfo deviceInfo) async { if (Platform.isIOS) { return 0; } String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState; return _channel.invokeMethod('endBluetoothConnect', deviceInfoStr).then((d) => d); } //断开连接 Future hasBluetoothConnectDevice() async { if (Platform.isIOS) { return false; } return _channel.invokeMethod('hasBluetoothConnectDevice').then((d) => d); } //开始打印 Future startBluetoothPrintBitMap(Uint8List bytes) async { if (Platform.isIOS) { return false; } return _channel.invokeMethod('startBluetoothPrintBitMap', bytes).then((d) => d); } //开始打印 Future startBluetoothPrintText(List textList) async { if (Platform.isIOS) { return false; } return _channel.invokeMethod('startBluetoothPrintText', textList).then((d) => d); } //开始打印 Future startBluetoothPrintBitMapAndText(Uint8List bytes,List textList) async { if (Platform.isIOS) { return false; } return _channel.invokeMethod('startBluetoothPrintBitMapAndText', {"bytes": bytes, "textList": textList}).then((d) => d); } //开始打印 Future startBluetoothPrintBarCodeWithText(String barCode, String text,List textList) async { if (Platform.isIOS) { return false; } return _channel.invokeMethod('startBluetoothPrintBarCodeWithText', {"barCode": barCode, "text": text, "textList":textList}).then((d) => d); } /// 接收数据监听 Stream get onReceiveDataStream async* { yield* _onReceiveDataStream .receiveBroadcastStream() .map((buffer) { String str = buffer; List strList = str.split(delimiterStr); if(strList.length == 3) { BlueDeviceInfo deviceInfo = BlueDeviceInfo(deviceName: strList[0], deviceMac: strList[1], connectState: strList[2]); return deviceInfo; } return null; }); } /// 接收数据监听 Stream get onDeviceStateStream async* { yield* _onDeviceStateStream .receiveBroadcastStream() .map((buffer) { return buffer.toString(); }); } } class DeviceState { static String scanStart = "scanStart"; static String scanEnd = "scanEnd"; static String pairEnd = "pairEnd"; } class BlueDeviceInfo { String deviceName; String deviceMac; String connectState; String? _connectStateStr; BlueDeviceInfo({required this.deviceName, required this.deviceMac, required this.connectState}); String get connectStateStr { if(connectState == '10') { _connectStateStr = "未配对(点击进行配对)"; } if(connectState == '11') { _connectStateStr = "配对中"; } if(connectState == '12') { _connectStateStr = "已配对(点击进行连接)"; } if(connectState == '13') { _connectStateStr = "断开(点击断开连接)"; } return _connectStateStr ?? "未知"; } void connectSuccess() { connectState = '13'; } void disConnectSuccess() { connectState = '12'; } @override String toString() { return {'deviceName:': deviceName, "mac:": deviceMac, "state:": connectState}.toString(); } }