import 'package:flutter/services.dart';

class BluetoothUtils {
  static const mChannel = const MethodChannel('io.fivefire/method_channel');
  static const eChannel = const EventChannel("io.fivefire/event_channel");

  Future<bool> isBTDevice() async {//检查本机是否有蓝牙设备
    try {
      return await mChannel.invokeMethod('isBTDevice');
    } on PlatformException catch (e) {
      print("$e");
      return false;
    }
  }

  Future<bool> isEnableBluetooth() async {//打开蓝牙
    try {
      return await mChannel.invokeMethod('isEnableBluetooth');
    } on PlatformException catch (e) {
      print("$e");
      return false;
    }
  }

  Future scanBT() async {//开始搜索蓝牙设备
    try {
      return await mChannel.invokeMethod('scanBT');
    } on PlatformException catch (e) {
      print("$e");
    }
  }

  Future<Null> connnectBT(String address) async {
    try {
      Map<String, String> map = {"address": address};

      return await mChannel.invokeMethod('connnectBT', map);
    } on PlatformException catch (e) {
      print("$e");
    }
  }

  Future disConnnectBT() async {
    try {
    

      return await mChannel.invokeMethod('disConnectBT');
    } on PlatformException catch (e) {
      print("$e");
    }
  }

  Future cancelDiscoveryBT() async {
    try {
      return await mChannel.invokeMethod('cancelDiscoveryBT');
    } on PlatformException catch (e) {
      print("$e");
    }
  }

  Future printOrder(Map<String,String> pum,int type)async{
     try {
      Map<String,Object> map = {"pum":pum,"type": type};

      return await mChannel.invokeMethod('printOrder', map);
    } on PlatformException catch (e) {
      print("$e");
    }

  }


   EventChannel getEchannel(){
     return eChannel;

  }

//   void initState() {
//     eChannel.receiveBroadcastStream().listen(onEvent, onError: onError);
//   }

//    onEvent(Object obj){
//     print(obj);
   
//   }

//    onError(Object obj) {}

   
}
