package com.printer.demo.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

import com.printer.demo.SettingActivity;
import com.printer.sdk.utils.XLog;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BondUtils {
    public static String TAG = "yxz";

    //与设备配对
    static public boolean createBond(Context mcontext, Class btClass, BluetoothDevice btDevice) throws Exception {
        XLog.d(TAG, "-----------yxz at ClsUtils.java createBond() begin" + btDevice.getAddress() + "----------");
        Method createBondMethod = btClass.getMethod("createBond");
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
        XLog.d(TAG, "-----------yxz at ClsUtils.java createBond() end" + btDevice.getAddress() + "----------");
        return returnValue.booleanValue();
    }

    //与设备解除配对
    static public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception {
        Method removeBondMethod = btClass.getMethod("removeBond");
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
        return returnValue.booleanValue();
    }

    static public boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception {
        XLog.d(TAG, "yxz at ClsUtils.java setPin() begin");
        try {
            Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[]{byte[].class});
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[]{str.getBytes()});
            XLog.e(TAG, "返回值:" + returnValue);
        } catch (SecurityException e) {
            e.printStackTrace();
            XLog.e(TAG, "SecurityException:" + e.getMessage());
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            XLog.e(TAG, "IllegalArgumentException:" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(TAG, "Exception:" + e.getMessage());
        }
        XLog.d(TAG, "yxz at ClsUtils.java setPin() end");
        return true;

    }

    // 取消用户输入
    static public boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception {
        Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
        // cancelBondProcess()
        Boolean returnValue = (Boolean) createBondMethod.invoke(device);
        return returnValue.booleanValue();
    }

    // 取消配对
    static public boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception {
        Method createBondMethod = btClass.getMethod("cancelBondProcess");
        Boolean returnValue = (Boolean) createBondMethod.invoke(device);
        return returnValue.booleanValue();
    }

    //确认配对
    static public void setPairingConfirmation(Context mcontext, Class<?> btClass, BluetoothDevice device, boolean isConfirm) throws Exception {
        XLog.d( TAG, "yxz at ClsUtils.java setPairingConfirmation() begin");
        Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
        setPairingConfirmation.invoke(device, isConfirm);
        XLog.d( TAG, "yxz at ClsUtils.java setPairingConfirmation() end");
    }



    /**
     * @param clsShow
     */
    static public void printAllInform(Class clsShow) {
        try {
            // 取得所有方法
            Method[] hideMethod = clsShow.getMethods();
            int i = 0;
            for (; i < hideMethod.length; i++) {
                Log.e("method name", hideMethod[i].getName() + ";and the i is:" + i);
            }
            // 取得所有常量
            Field[] allFields = clsShow.getFields();
            for (i = 0; i < allFields.length; i++) {
                Log.e("Field name", allFields[i].getName());
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
