android ble4.0绑定

蓝牙BLE4.0绑定

    蓝牙的绑定其实网上的方法都是大同小异的,都是用的反射的方法去绑定蓝牙设备,在API19以后,ble配对其实已经可以不用反射配对,直接bleDevice对象去用createBond,然后再在配对广播里面使用bleDevice.setPin(),输入取消  用户输入对话框的代码,但是取消用户输入框没办法,还得用反射的方式,不过有的时候反射仍然去不掉 用户输入框 这时我们在广播里面直接将配对广播屏蔽掉

public class BLEClassUtil {
    private static final String TAG = "--" + BLEClassUtil.class.getSimpleName();

    /**
     * 取消用户输入
     *
     * @param bleDevice
     * @return
     */
    public static boolean cancelPairingUserInput(BluetoothDevice bleDevice) {

        final Method cancelPairingUserInput;
        boolean result = false;
        try {
            cancelPairingUserInput = bleDevice.getClass().getMethod("cancelPairingUserInput");
            Boolean returnValue = (Boolean) cancelPairingUserInput.invoke(bleDevice);
            result = returnValue.booleanValue();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Logger.e(TAG, "cancelPairingUserInput         " + result);
        return result;
    }

    /**
     * 手机和蓝牙采集器配对
     *
     * @param bleDevice
     * @param str
     * @return
     * @throws Exception
     */
    public static boolean setPin(BluetoothDevice bleDevice, String str) {
        boolean value = false;

        try {
            Logger.e(TAG, "..........");
            final Method setPin = bleDevice.getClass().getDeclaredMethod("setPin", new Class[]{byte[].class});
            Logger.e(TAG, "..........");
            final Boolean invoke = (Boolean) setPin.invoke(bleDevice, new Object[]{str.getBytes()});
            value = invoke.booleanValue();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return value;

//        Logger.e(TAG, "setPin" + 111111111);
//        Method removeBondMethod = bleDevice.getClass().getMethod("setPin", new Class[]{byte.class});
//        Logger.e(TAG, "setPin" + 222222);
//
//        final Boolean returnValue = (Boolean) removeBondMethod.invoke(bleDevice, new Object[]{str.getBytes()});
//        Logger.e(TAG, "setPin" + 333333);
//        value = returnValue.booleanValue();
//        Logger.e(TAG, "setPin     SecurityException" + 333333);
//        return value;
    }

    /**
     * 绑定
     *
     * @param bleDevice
     * @return
     * @throws Exception
     */
    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;
    }

    /**
     * 取消配对
     *
     * @param cls
     * @param bleDevice
     * @return
     * @throws Exception
     */
    public static boolean removeBond(Class cls, BluetoothDevice bleDevice) throws Exception {

        final Method removeBond = cls.getClass().getMethod("removeBond");
        final Boolean returnValue = (Boolean) removeBond.invoke(bleDevice);
        return returnValue.booleanValue();
    }



    /**
     * 取消绑定
     *
     * @param bleDevice
     * @return
     * @throws Exception
     */
    public static boolean cancelBondProcess(BluetoothDevice bleDevice) throws Exception {


//        Class.forName("cancelBondProcess")


        Logger.e(TAG, "++++");
        Method cancelBondProcess = bleDevice.getClass().getMethod("cancelBondProcess");
        Logger.e(TAG, "----");
        Boolean returnValue = (Boolean) cancelBondProcess.invoke(bleDevice);
        Logger.e(TAG, "%%%%%");
        return returnValue.booleanValue();
    }

这是我的工具类,在扫描到蓝牙,要绑定蓝牙的时候,假设我要绑定的蓝牙设备的对象是device;那么我就会先在代码里面敲上

        if (SDKUtil.isBigger(18)) {
            isCreate = device.createBond();
//            isCreate = device.setPin(pinStr.getBytes());
            Logger.e(TAG, "isCreate    " + isCreate);
        } else {
            BLEClassUtil.createBond(device);
//            isCreate = BLEClassUtil.setPin(device, pinStr);
        }

        BLEClassUtil.cancelPairingUserInput(device);//因为这个只能通过反射

然后在我们的静态注册广播,设置优先级为1000,这个广播里面里面写上如下代码:

public class PairBroadcastReceiver extends BroadcastReceiver {
    private String TAG = "--" + getClass().getSimpleName();


    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            final int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
            boolean isSuccess = false;
            switch (type) {
                case 0:
                case 2:
                    if (SDKUtil.isBigger(18)) {
                        isSuccess = device.setPin("84512".getBytes());
                    } else {
                        isSuccess = BLEClassUtil.setPin(device, "84512");
                    }
                    break;
            }
            if (isSuccess) {
                Logger.e(TAG, "123456");
                abortBroadcast();//这个主要是在setpin成功后防止输入框的弹出 
            } else {
                if (SDKUtil.isBigger(18)) {
                    device.createBond();
                    Logger.e(TAG, "isBigger");
                } else {
                    BLEClassUtil.createBond(device);
                }
            }
        }
    }
}




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页