bluetooth_utils.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:flutter/services.dart';
  2. class BluetoothUtils {
  3. static const mChannel = const MethodChannel('io.fivefire/method_channel');
  4. static const eChannel = const EventChannel("io.fivefire/event_channel");
  5. /**
  6. * 判断设备是否支持蓝牙
  7. */
  8. Future<bool> isBTDevice() async {
  9. try {
  10. return await mChannel.invokeMethod('isBTDevice');
  11. } on PlatformException catch (e) {
  12. print("$e");
  13. return false;
  14. }
  15. }
  16. /**
  17. * 判断蓝牙是否打开
  18. */
  19. Future<bool> isEnableBluetooth() async {
  20. try {
  21. return await mChannel.invokeMethod('isEnableBluetooth');
  22. } on PlatformException catch (e) {
  23. print("$e");
  24. return false;
  25. }
  26. }
  27. /**
  28. * 打开蓝牙
  29. */
  30. Future<bool> enableBluetooth() async {
  31. try {
  32. return await mChannel.invokeMethod('enableBluetooth');
  33. } on PlatformException catch (e) {
  34. print("$e");
  35. return false;
  36. }
  37. }
  38. /**
  39. * 扫描蓝牙设备
  40. */
  41. Future scanBT() async {
  42. try {
  43. return await mChannel.invokeMethod('scanBT');
  44. } on PlatformException catch (e) {
  45. print("$e");
  46. }
  47. }
  48. /**
  49. * 传入mac 地址
  50. * 连接蓝牙
  51. */
  52. Future<bool> connectBT(String address) async {
  53. try {
  54. Map<String, String> map = {"address": address};
  55. return await mChannel.invokeMethod('connectBT', map);
  56. } on PlatformException catch (e) {
  57. print("$e");
  58. }
  59. }
  60. /**
  61. * 关闭蓝牙
  62. */
  63. Future disConnnectBT() async {
  64. try {
  65. return await mChannel.invokeMethod('disConnectBT');
  66. } on PlatformException catch (e) {
  67. print("$e");
  68. }
  69. }
  70. /**
  71. * 停止蓝牙设备扫描
  72. */
  73. Future cancelDiscoveryBT() async {
  74. try {
  75. return await mChannel.invokeMethod('cancelDiscoveryBT');
  76. } on PlatformException catch (e) {
  77. print("$e");
  78. }
  79. }
  80. /**
  81. * 打印订单
  82. */
  83. Future printOrder(Map<String,String> pum,int type)async{
  84. try {
  85. Map<String,Object> map = {"pum":pum,"type": type};
  86. return await mChannel.invokeMethod('printOrder', map);
  87. } on PlatformException catch (e) {
  88. print("$e");
  89. }
  90. }
  91. Future isConnect(String address) async {
  92. try {
  93. Map<String, String> map = {"address": address};
  94. return await mChannel.invokeMethod('connectBT', map);
  95. } on PlatformException catch (e) {
  96. print("$e");
  97. }
  98. }
  99. EventChannel getEchannel(){
  100. return eChannel;
  101. }
  102. }