123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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<bool> isBleOpen() async {
- if (Platform.isIOS) {
- return true;
- }
- return _channel.invokeMethod('isBleOpen').then<bool>((d) => d);
- }
- //安卓版本是否大于等于31
- Future<bool> isSDKIntGreaterOrEqual31() async {
- if (Platform.isIOS) {
- return true;
- }
- return _channel.invokeMethod('isSDKIntGreaterOrEqual').then<bool>((d) => d);
- }
- //开始扫描
- Future<int> startBluetoothDiscovery() async {
- if (Platform.isIOS) {
- return 0;
- }
- return _channel.invokeMethod('startBluetoothDiscovery').then<int>((d) => d);
- }
- //开始配对
- Future<int> 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<int>((d) => d);
- }
- //开始连接
- Future<int> 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<int>((d) => d);
- }
- //断开连接
- Future<int> 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<int>((d) => d);
- }
- //断开连接
- Future<bool> hasBluetoothConnectDevice() async {
- if (Platform.isIOS) {
- return false;
- }
- return _channel.invokeMethod('hasBluetoothConnectDevice').then<bool>((d) => d);
- }
- //开始打印
- Future<bool> startBluetoothPrintBitMap(Uint8List bytes) async {
- if (Platform.isIOS) {
- return false;
- }
- return _channel.invokeMethod('startBluetoothPrintBitMap', bytes).then<bool>((d) => d);
- }
- //开始打印
- Future<bool> startBluetoothPrintText(List<String> textList) async {
- if (Platform.isIOS) {
- return false;
- }
- return _channel.invokeMethod('startBluetoothPrintText', textList).then<bool>((d) => d);
- }
- //开始打印
- Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
- if (Platform.isIOS) {
- return false;
- }
- return _channel.invokeMethod('startBluetoothPrintBitMapAndText', {"bytes": bytes, "textList": textList}).then<bool>((d) => d);
- }
- //开始打印
- Future<bool> startBluetoothPrintBarCodeWithText(String barCode, String text,List<String> textList) async {
- if (Platform.isIOS) {
- return false;
- }
- return _channel.invokeMethod('startBluetoothPrintBarCodeWithText', {"barCode": barCode, "text": text, "textList":textList}).then<bool>((d) => d);
- }
- /// 接收数据监听
- Stream<BlueDeviceInfo?> get onReceiveDataStream async* {
- yield* _onReceiveDataStream
- .receiveBroadcastStream()
- .map((buffer) {
- String str = buffer;
- List<String> 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<String?> 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();
- }
- }
|