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<bool> enableBluetooth() async {
    try {
      return await mChannel.invokeMethod('enableBluetooth');
    } on PlatformException catch (e) {
      print("$e");
      return false;
    }
  }

  /**
   * 扫描蓝牙设备
   */
  Future scanBT() async {
    try {
      return await mChannel.invokeMethod('scanBT');
    } on PlatformException catch (e) {
      print("$e");
    }
  }

  /**
   * 传入mac 地址 
   * 连接蓝牙
   */
  Future<bool> connectBT(String address) async {
    try {
      Map<String, String> map = {"address": address};

      return await mChannel.invokeMethod('connectBT', 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");
    }

  }

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

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


   EventChannel getEchannel(){
     return eChannel;

  }

   
}
