bluetooth_plugin.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. import 'package:logger/logger.dart';
  4. import '../main.dart';
  5. import '../router/my_navigator.dart';
  6. class BluetoothPlugin {
  7. factory BluetoothPlugin() => _instance;
  8. BluetoothPlugin._();
  9. static final BluetoothPlugin _instance = BluetoothPlugin._();
  10. static BluetoothPlugin get instance => _instance;
  11. final String delimiterStr = "_NIMBOT_";
  12. final MethodChannel _channel = const MethodChannel("io.flutter.plugins/bluetooth/methods");
  13. final EventChannel _onReceiveDataStream = const EventChannel('io.flutter.plugins/bluetooth/onDiscoveryDevice');
  14. final EventChannel _onDeviceStateStream = const EventChannel('io.flutter.plugins/bluetooth/onDeviceState');
  15. //是否打开蓝牙
  16. Future<bool> isBleOpen() async {
  17. if (Platform.isIOS) {
  18. return true;
  19. }
  20. return _channel.invokeMethod('isBleOpen').then<bool>((d) => d);
  21. }
  22. //安卓版本是否大于等于31
  23. Future<bool> isSDKIntGreaterOrEqual31() async {
  24. if (Platform.isIOS) {
  25. return true;
  26. }
  27. return _channel.invokeMethod('isSDKIntGreaterOrEqual').then<bool>((d) => d);
  28. }
  29. //开始扫描
  30. Future<int> startBluetoothDiscovery() async {
  31. if (Platform.isIOS) {
  32. return 0;
  33. }
  34. return _channel.invokeMethod('startBluetoothDiscovery').then<int>((d) => d);
  35. }
  36. //开始配对
  37. Future<int> startBluetoothPair(BlueDeviceInfo deviceInfo) async {
  38. if (Platform.isIOS) {
  39. return 0;
  40. }
  41. String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState;
  42. return _channel.invokeMethod('startBluetoothPair', deviceInfoStr).then<int>((d) => d);
  43. }
  44. //开始连接
  45. Future<int> startBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  46. if (Platform.isIOS) {
  47. return 0;
  48. }
  49. String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState;
  50. return _channel.invokeMethod('startBluetoothConnect', deviceInfoStr).then<int>((d) => d);
  51. }
  52. //断开连接
  53. Future<int> endBluetoothConnect(BlueDeviceInfo deviceInfo) async {
  54. if (Platform.isIOS) {
  55. return 0;
  56. }
  57. String deviceInfoStr = deviceInfo.deviceName+delimiterStr+deviceInfo.deviceMac+delimiterStr+deviceInfo.connectState;
  58. return _channel.invokeMethod('endBluetoothConnect', deviceInfoStr).then<int>((d) => d);
  59. }
  60. //断开连接
  61. Future<bool> hasBluetoothConnectDevice() async {
  62. if (Platform.isIOS) {
  63. return false;
  64. }
  65. return _channel.invokeMethod('hasBluetoothConnectDevice').then<bool>((d) => d);
  66. }
  67. //开始打印
  68. Future<bool> startBluetoothPrintBitMap(Uint8List bytes) async {
  69. if (Platform.isIOS) {
  70. return false;
  71. }
  72. return _channel.invokeMethod('startBluetoothPrintBitMap', bytes).then<bool>((d) => d);
  73. }
  74. //开始打印
  75. Future<bool> startBluetoothPrintText(List<String> textList) async {
  76. if (Platform.isIOS) {
  77. return false;
  78. }
  79. return _channel.invokeMethod('startBluetoothPrintText', textList).then<bool>((d) => d);
  80. }
  81. //开始打印
  82. Future<bool> startBluetoothPrintBitMapAndText(Uint8List bytes,List<String> textList) async {
  83. if (Platform.isIOS) {
  84. return false;
  85. }
  86. return _channel.invokeMethod('startBluetoothPrintBitMapAndText', {"bytes": bytes, "textList": textList}).then<bool>((d) => d);
  87. }
  88. //开始打印
  89. Future<bool> startBluetoothPrintBarCodeWithText(String barCode, String text) async {
  90. if (Platform.isIOS) {
  91. return false;
  92. }
  93. return _channel.invokeMethod('startBluetoothPrintBarCodeWithText', {"barCode": barCode, "text": text}).then<bool>((d) => d);
  94. }
  95. /// 接收数据监听
  96. Stream<BlueDeviceInfo?> get onReceiveDataStream async* {
  97. yield* _onReceiveDataStream
  98. .receiveBroadcastStream()
  99. .map((buffer) {
  100. String str = buffer;
  101. List<String> strList = str.split(delimiterStr);
  102. if(strList.length == 3) {
  103. BlueDeviceInfo deviceInfo = BlueDeviceInfo(deviceName: strList[0], deviceMac: strList[1], connectState: strList[2]);
  104. return deviceInfo;
  105. }
  106. return null;
  107. });
  108. }
  109. /// 接收数据监听
  110. Stream<String?> get onDeviceStateStream async* {
  111. yield* _onDeviceStateStream
  112. .receiveBroadcastStream()
  113. .map((buffer) {
  114. return buffer.toString();
  115. });
  116. }
  117. }
  118. class DeviceState {
  119. static String scanStart = "scanStart";
  120. static String scanEnd = "scanEnd";
  121. static String pairEnd = "pairEnd";
  122. }
  123. class BlueDeviceInfo {
  124. String deviceName;
  125. String deviceMac;
  126. String connectState;
  127. String? _connectStateStr;
  128. BlueDeviceInfo({required this.deviceName, required this.deviceMac, required this.connectState});
  129. String get connectStateStr {
  130. if(connectState == '10') {
  131. _connectStateStr = "未配对(点击进行配对)";
  132. }
  133. if(connectState == '11') {
  134. _connectStateStr = "配对中";
  135. }
  136. if(connectState == '12') {
  137. _connectStateStr = "已配对(点击进行连接)";
  138. }
  139. if(connectState == '13') {
  140. _connectStateStr = "断开(点击断开连接)";
  141. }
  142. return _connectStateStr ?? "未知";
  143. }
  144. void connectSuccess() {
  145. connectState = '13';
  146. }
  147. void disConnectSuccess() {
  148. connectState = '12';
  149. }
  150. @override
  151. String toString() {
  152. return {'deviceName:': deviceName, "mac:": deviceMac, "state:": connectState}.toString();
  153. }
  154. }