bluetooth_plugin.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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> startBluetoothPrintBarCodeWithText(String barCode, String text) async {
  76. if (Platform.isIOS) {
  77. return false;
  78. }
  79. return _channel.invokeMethod('startBluetoothPrintBarCodeWithText', {"barCode": barCode, "text": text}).then<bool>((d) => d);
  80. }
  81. /// 接收数据监听
  82. Stream<BlueDeviceInfo?> get onReceiveDataStream async* {
  83. yield* _onReceiveDataStream
  84. .receiveBroadcastStream()
  85. .map((buffer) {
  86. String str = buffer;
  87. List<String> strList = str.split(delimiterStr);
  88. if(strList.length == 3) {
  89. BlueDeviceInfo deviceInfo = BlueDeviceInfo(deviceName: strList[0], deviceMac: strList[1], connectState: strList[2]);
  90. return deviceInfo;
  91. }
  92. return null;
  93. });
  94. }
  95. /// 接收数据监听
  96. Stream<String?> get onDeviceStateStream async* {
  97. yield* _onDeviceStateStream
  98. .receiveBroadcastStream()
  99. .map((buffer) {
  100. return buffer.toString();
  101. });
  102. }
  103. }
  104. class DeviceState {
  105. static String scanStart = "scanStart";
  106. static String scanEnd = "scanEnd";
  107. static String pairEnd = "pairEnd";
  108. }
  109. class BlueDeviceInfo {
  110. String deviceName;
  111. String deviceMac;
  112. String connectState;
  113. String? _connectStateStr;
  114. BlueDeviceInfo({required this.deviceName, required this.deviceMac, required this.connectState});
  115. String get connectStateStr {
  116. if(connectState == '10') {
  117. _connectStateStr = "未配对(点击进行配对)";
  118. }
  119. if(connectState == '11') {
  120. _connectStateStr = "配对中";
  121. }
  122. if(connectState == '12') {
  123. _connectStateStr = "已配对(点击进行连接)";
  124. }
  125. if(connectState == '13') {
  126. _connectStateStr = "断开(点击断开连接)";
  127. }
  128. return _connectStateStr ?? "未知";
  129. }
  130. void connectSuccess() {
  131. connectState = '13';
  132. }
  133. void disConnectSuccess() {
  134. connectState = '12';
  135. }
  136. @override
  137. String toString() {
  138. return {'deviceName:': deviceName, "mac:": deviceMac, "state:": connectState}.toString();
  139. }
  140. }