ANDROID 蓝牙4.0 BLE 开发

1.蓝牙配对问题

硬件发起配对,和手机发起配对,不一样的;
当时在设备发起配对时,很是疑惑,设备是如何发起配对的;
不知道怎么弄;
后来才知道,链接上设备后,gatt服务链接过程中,会收到设备发起的广播;
我弄得监听广播接收,有接收数据;这才明白;

1.1要在manifest里注册广播接收;

    <receiver
            android:name=".CommonUtils.BluetoothConnectActivityReceiver"
            android:priority="2147483647">
            <intent-filter>
                <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
            </intent-filter>
        </receiver>

1.2新建一个接收类

public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
    String strPsw = "123455";

    @Override
    public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
        BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
            //requestMtu
            Log.e("sjt", "on receiver pair progress,name==" + btDevice.getName());
            Log.e("sjt", "on receiver pair progress,connect==" + BluetoothLeService.getConnectionState());
            int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
                    BluetoothDevice.ERROR);
            Log.e("sjt", "on receiver pair progress,type==" + type);
            int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,
                    BluetoothDevice.ERROR);
            Log.e("sjt", "on receiver pair progress,pairingKey==" + pairingKey);
            try {
                //1.确认配对
                //(三星)4.3版本测试手机还是会弹出用户交互页面(闪一下),如果不注释掉下面这句页面不会取消但可以配对成功。(中兴,魅族4(Flyme 6))5.1版本手机两中情况下都正常
                // ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
                //2.终止有序广播
                // Log.e("sjt", "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
                // abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。
                //3.调用setPin方法进行配对...
                //      ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw);// 手机和蓝牙采集器配对
                //             ClsUtils.createBond(btDevice.getClass(), btDevice);
//                ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);

            } catch (Exception e) {
// TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

android系统接收到设备发起得配对广播后,就会弹出配对密码框;
这时候注意;
会弹出是否配对;点击确认后,弹出输入密码框;
这里,密码输入不输入;看硬件的配置项;

1.3(硬件端)配对密码认证

AT+PASSEN
增加了一个NO参数
NO 无认证、无加密
OFF 认证配对、无加密(会弹配对请求框,不弹输入密码框,有的手机在通知栏里,需要点击一下,有的手机需要点击2下,这里见华为荣耀9 配对 截图;
ON 认证、加密(会弹配对请求框,也会弹输入密码框)

1.4 想禁止蓝牙弹出配对框,自动配对;

一般博客,网上,都是利用反射,工具类见下;
思路就是系统收到广播后,调用配对,输入密码;阻止系统弹出对话框;
上面广播接收的代码

  //1.确认配对
                //(三星)4.3版本测试手机还是会弹出用户交互页面(闪一下),如果不注释掉下面这句页面不会取消但可以配对成功。(中兴,魅族4(Flyme 6))5.1版本手机两中情况下都正常
                // ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
                //2.终止有序广播
                // Log.e("sjt", "isOrderedBroadcast:" + isOrderedBroadcast() + ",isInitialStickyBroadcast:" + isInitialStickyBroadcast());
                // abortBroadcast();//如果没有将广播终止,则会出现一个一闪而过的配对框。
                //3.调用setPin方法进行配对...
                //      ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw);// 手机和蓝牙采集器配对
                //             ClsUtils.createBond(btDevice.getClass(), btDevice);
//                ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);

实际测试发现:
android 19可以阻止;android 26上无法阻止,android 21上部分机型也不行;建议不要这样搞,还是利用系统的弹框吧;

1.4.1 工具类

public class ClsUtils {
    public static boolean createBond(Class btClass, BluetoothDevice btDevice) throws Exception {
        Method createBondMethod = btClass.getMethod("createBond");
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
        return returnValue.booleanValue();
    }


    public static boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception

    {
        Method removeBondMethod = btClass.getMethod("removeBond");
        Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
        return returnValue.booleanValue();
    }

    public static boolean setPin(Class btClass, BluetoothDevice btDevice, String str) throws Exception

    {
        try {
            Method removeBondMethod = btClass.getDeclaredMethod("setPin", new Class[]{byte[].class});
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[]{str.getBytes()});
            Log.e("returnValue", "" + returnValue);
        } catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;

    }

    // 取消用户输入
    public static boolean cancelPairingUserInput(Class btClass, BluetoothDevice device) throws Exception

    {
        Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
// cancelBondProcess()
        Boolean returnValue = (Boolean) createBondMethod.invoke(device);
        return returnValue.booleanValue();
    }

    // 取消配对
    public static boolean cancelBondProcess(Class btClass, BluetoothDevice device) throws Exception

    {
        Method createBondMethod = btClass.getMethod("cancelBondProcess");
        Boolean returnValue = (Boolean) createBondMethod.invoke(device);
        return returnValue.booleanValue();
    }
    //确认配对

    public static void setPairingConfirmation(Class<?> btClass, BluetoothDevice device, boolean isConfirm) throws Exception {
        Method setPairingConfirmation = btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
        setPairingConfirmation.invoke(device, isConfirm);
    }

    public static 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) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static boolean setPassKey(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice, String strPsw) {
        try {
            Method removeBondMethod = btClass.getDeclaredMethod("setPassKey", new Class[]{byte[].class});
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[]{strPsw.getBytes()});
            Log.e("returnValue", "" + returnValue);
        } catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;

    }
}

2.蓝牙断链问题

现象,就是有时候触发gatt的disconnect;在close;发现有时候,断不开;

 /**
     * Disconnects an existing connection or cancel a pending connection. The
     * disconnection result is reported asynchronously through the
     * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
     * callback.
     */
    public static void disconnect() {
        Logger.e("disconnect called");
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            return;
        } else {
            //Clearing Bluetooth cache before disconnecting to the device
            if (Utils.getBooleanSharedPreference(mContext, Constants.PREF_PAIR_CACHE_STATUS)) {
                //Logger.e(getActivity().getClass().getName() + "Cache cleared on disconnect!");
                BluetoothLeService.refreshDeviceCache(BluetoothLeService.mBluetoothGatt);
            }
            mBluetoothGatt.disconnect();
            String dataLog = mContext.getResources().getString(R.string.dl_commaseparator)
                    + "[" + mBluetoothDeviceName + "|" + mBluetoothDeviceAddress + "] " +
                    mContext.getResources().getString(R.string.dl_disconnection_request);
            Logger.datalog(dataLog);
            close();
        }

    }
    /**
     * After using a given BLE device, the app must call this method to ensure
     * resources are released properly.
     */
    public static void close() {
        if (mBluetoothGatt == null) {
            Logger.e("sjt--add  gatt is null ");
            return;
        }
        mBluetoothGatt.close();
        mBluetoothGatt = null;
    }

看到一些大牛,写的;建议在gatt 服务,disconnect之后,在状态改变接收机里,再close;
见下面代码: BluetoothLeService.close();// 新加的,在disconnect之后,加close

  /**
     * Implements callback methods for GATT events that the app cares about. For
     * example,connection change and services discovered.
     */
    private final static BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            Logger.i("onConnectionStateChange");
            String intentAction;
            // GATT Server connected
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                synchronized (mGattCallback) {
                    mConnectionState = STATE_CONNECTED;
                }
                broadcastConnectionUpdate(intentAction);
                String dataLog = mContext.getResources().getString(R.string.dl_commaseparator)
                        + "[" + mBluetoothDeviceName + "|" + mBluetoothDeviceAddress + "] " +
                        mContext.getResources().getString(R.string.dl_connection_established);
                Logger.datalog(dataLog);
            }
            // GATT Server disconnected
            else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                Logger.e("sjt add -- BluetoothService receive action gatt disconnected");

                synchronized (mGattCallback) {
                    mConnectionState = STATE_DISCONNECTED;
                }
                BluetoothLeService.close();//新加的,在disconnect之后,加close

                broadcastConnectionUpdate(intentAction);
                String dataLog = mContext.getResources().getString(R.string.dl_commaseparator)
                        + "[" + mBluetoothDeviceName + "|" + mBluetoothDeviceAddress + "] " +
                        mContext.getResources().getString(R.string.dl_connection_disconnected);
                Logger.datalog(dataLog);
            }
            // GATT Server Connecting
            if (newState == BluetoothProfile.STATE_CONNECTING) {
                intentAction = ACTION_GATT_CONNECTING;
                synchronized (mGattCallback) {
                    mConnectionState = STATE_CONNECTING;
                }
                broadcastConnectionUpdate(intentAction);
                String dataLog = mContext.getResources().getString(R.string.dl_commaseparator)
                        + "[" + mBluetoothDeviceName + "|" + mBluetoothDeviceAddress + "] " +
                        mContext.getResources().getString(R.string.dl_connection_establishing);
                Logger.datalog(dataLog);
            }
            // GATT Server disconnected
            else if (newState == BluetoothProfile.STATE_DISCONNECTING) {
                intentAction = ACTION_GATT_DISCONNECTING;
                synchronized (mGattCallback) {
                    mConnectionState = STATE_DISCONNECTING;
                }
                broadcastConnectionUpdate(intentAction);
            }
        }
        //省略了下面代码
}

3.BLE 每次发送接收的字节数目(20字节限制)

这里写图片描述
修改mtu就可以,但是系统要在android 21及以上;

4. OTA 蓝牙升级(后续)

参考链接:
带你解锁蓝牙skill–fanfan
http://blog.csdn.net/zrf1335348191/article/category/6216206
Android BLE中传输数据的最大长度怎么破
http://blog.csdn.net/sinat_19628093/article/details/51890827

Android 蓝牙开发(四)OPP传输文件
http://blog.csdn.net/VNanyesheshou/article/details/70256004?locationNum=9&fps=1
蓝牙自动配对demo
http://blog.csdn.net/qq_25827845/article/details/52400782
蓝牙4.0的低功耗模式下还需要配对操作吗?
https://www.zhihu.com/question/21609225

BLE4.0配对绑定过程的底层剖析
http://blog.csdn.net/happyppdog/article/details/51971545
Android 蓝牙4.0实现,自动匹配蓝牙设备
http://blog.csdn.net/cyinclude/article/details/51777996
[Bluetooth]: android 平台上BLE连接流程之优化方案
http://blog.csdn.net/teaspring/article/details/77200688

android ble 4.0实现自动配对
http://blog.csdn.net/pcw550/article/details/47963005
BluetoothDevice api
http://www.android-doc.com/reference/android/bluetooth/BluetoothDevice.html#BOND_BONDED

BLE配对绑定过程梳理
http://blog.csdn.net/u010603798/article/details/70789216
Android蓝牙配对弹出框过程分析
http://blog.csdn.net/zrf1335348191/article/details/54020225

安卓蓝牙框架汇总
http://blog.csdn.net/yinghaijushi/article/details/52749316

Android 蓝牙自动匹配PIN码跳过用户交互
http://blog.csdn.net/yi412/article/details/70208885

蓝牙BLE 架构剖析
http://blog.csdn.net/xiaoxiaopengbo/article/details/51517218
蓝牙【GATT】协议介绍
http://blog.csdn.net/u013378580/article/details/52891462

[翻译]ATT和GATT概述
http://legendmohe.net/2015/01/16/%E7%BF%BB%E8%AF%91att%E5%92%8Cgatt%E6%A6%82%E8%BF%B0/
蓝牙协议剖析 专栏
http://blog.csdn.net/column/details/bluetooth.html?&page=2
Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
http://blog.csdn.net/jimoduwu/article/details/21604215

Android手机与多个BLE设备通信
http://blog.csdn.net/mark_sssss/article/details/45063813

Bluetooth Developer Center
https://www.bluetooth.com/develop-with-bluetooth

Android 蓝牙4.0(BLE)开发实现对蓝牙的写入数据和读取数据
http://blog.csdn.net/chenfengdejuanlian/article/details/45787123

猜你喜欢

转载自blog.csdn.net/u014624241/article/details/79536374