bluetooth_utils.dart 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Future<bool> isBTDevice() async {//检查本机是否有蓝牙设备
  6. try {
  7. return await mChannel.invokeMethod('isBTDevice');
  8. } on PlatformException catch (e) {
  9. print("$e");
  10. return false;
  11. }
  12. }
  13. Future<bool> isEnableBluetooth() async {//打开蓝牙
  14. try {
  15. return await mChannel.invokeMethod('isEnableBluetooth');
  16. } on PlatformException catch (e) {
  17. print("$e");
  18. return false;
  19. }
  20. }
  21. Future scanBT() async {//开始搜索蓝牙设备
  22. try {
  23. return await mChannel.invokeMethod('scanBT');
  24. } on PlatformException catch (e) {
  25. print("$e");
  26. }
  27. }
  28. Future<Null> connnectBT(String address) async {
  29. try {
  30. Map<String, String> map = {"address": address};
  31. return await mChannel.invokeMethod('connnectBT', map);
  32. } on PlatformException catch (e) {
  33. print("$e");
  34. }
  35. }
  36. Future disConnnectBT() async {
  37. try {
  38. return await mChannel.invokeMethod('disConnectBT');
  39. } on PlatformException catch (e) {
  40. print("$e");
  41. }
  42. }
  43. Future printOrder(Map<String,String> pum,int type)async{
  44. try {
  45. Map<String,Object> map = {"pum":pum,"type": type};
  46. return await mChannel.invokeMethod('printOrder', map);
  47. } on PlatformException catch (e) {
  48. print("$e");
  49. }
  50. }
  51. EventChannel getEchannel(){
  52. return eChannel;
  53. }
  54. // void initState() {
  55. // eChannel.receiveBroadcastStream().listen(onEvent, onError: onError);
  56. // }
  57. // onEvent(Object obj){
  58. // print(obj);
  59. // }
  60. // onError(Object obj) {}
  61. }