BleBondUtil.java 1.44 KB
package com.glsx.libble.util;

import android.bluetooth.BluetoothDevice;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Package: com.glsx.module_carkey.util
 * @ClassName: BleBondUtil
 * @Company GLSX
 * @Email yangks@didihu.com.cn
 * @Description: 作用描述:
 * @Author: yangks
 * @CreateDate: 2021/4/22 20:15
 * @Version:
 */
public class BleBondUtil {
    //绑定
    public static boolean createBond(BluetoothDevice bleDevice) {
        boolean result = false;
        try {
            final Method createBond = bleDevice.getClass().getMethod("createBond");
            final Boolean invoke = (Boolean) createBond.invoke(bleDevice);
            result = invoke.booleanValue();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    //与设备解除配对
    public static boolean removeBond(BluetoothDevice bleDevice) {
        boolean result = false;
        try {
            final Method removeBond = bleDevice.getClass().getMethod("removeBond");
            final Boolean returnValue = (Boolean) removeBond.invoke(bleDevice);
            result = returnValue.booleanValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}