Android手机蓝牙总结之注意事项

1、Android在传输数据过程中,默认不超过20个字节,如果超过20字节的数据,应该分包进行传输。

2、数据包在传输过程中,有延时,在实际应用中收发包应该增加延时的处理。在5.0android版本(api 21)做了放开处理,可以进行自己设置(大小)的处理。建议还是分包进行收发,因为当无法知道外部蓝牙硬件设备最大的收发,可能会因为数据过大,而设备内存过小,造成丢包或者其他问题的出现。

3、扫描附近蓝牙应该设置定时处理,当超过一定时间取消蓝牙搜索,避免因为持续搜索造成性能损耗。切记,连接蓝牙设备前一定要进行停止蓝牙搜索,否则会造成蓝牙信号的不稳定,会出现自动断开的现象。

4、关闭通信时,一定要调用BluetoothGatt的disconnect()和close()方法,并且将BluetoothGatt和BluetoothGattService赋值null。

5.0以后蓝牙连接的写法:

private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            // Android 5.0 及以上
            connect(device);
        } else {
            // Android 5.0 以下
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    connect(device);
                }
            });
        }
    }
};

判断是否具有相对应的属性:

 /**
     * @return Returns <b>true</b> if property is writable
     */
    public static boolean isCharacteristicWriteable(BluetoothGattCharacteristic pChar) {
        return (pChar.getProperties() & (BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0;
    }

    /**
     * @return Returns <b>true</b> if property is Readable
     */
    public static boolean isCharacterisitcReadable(BluetoothGattCharacteristic pChar) {
        return ((pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) != 0);
    }

    /**
     * @return Returns <b>true</b> if property is supports notification
     */
    public boolean isCharacterisiticNotifiable(BluetoothGattCharacteristic pChar) {
        return (pChar.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0;
    }

蓝牙在和外设硬件传输数据要转成16进制的编码

/**
     * 将字节按十六进制转换为字符串
     *
     * @param src 需要转换的字节数组
     * @return 返回转换完之后的数据
     */
    public static String bytesToHexString(byte[] src) throws Exception {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

将16进制字节流转成字符串

/**
     * 将字符串转化为16进制的字节,传入的是16进制字符串
     *
     * @param message 需要被转换的字符
     * @return
     */
    public static byte[] getHexBytes(String message) throws Exception {
        int len = message.length() / 2;
        char[] chars = message.toCharArray();
        String[] hexStr = new String[len];
        byte[] bytes = new byte[len];
        for (int i = 0, j = 0; j < len; i += 2, j++) {
            hexStr[j] = "" + chars[i] + chars[i + 1];
            bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
        }
        return bytes;
    }

5、在蓝牙连接过程中,发现有时候连接不稳定,然后断开。或者是直接无法成功连接,出现这两种情况的解决办法:
1、在onConnectionStateChange(BluetoothGatt gatt , int status , int newState)回调中打印第二个参数(status)的值,,如果status的值为0,即status的值等于BluetoothGatt.GATT_SUCCESS,则是建立了蓝牙协议栈通过,直接判断newState的值是否为BluetoothProfile.STATE_DISCONNECTED,如果是则调用BluetoothGatt的connect()方法,即可重新连接。

2、如果status的值不为0(大部分情况为133)

  1. 首先执行gatt.close()清除连接;
  2. 然后重新调用蓝牙的链接方法bluetoothDevice.connectgatt(BluetoothDevice,autoconnect,BluetoothGattCallback); 建立连接

必须要先gatt.close()关闭,因为手机的连接是有限制的。
值为0:不确定因素导致连接失败。不确定因素可能为信号太弱等。

status原因分析:

  1. 值不为0:由于协议栈原因导致连接建立失败。所以清除掉连接后重新建立连接
  2. 值不为0:由于协议栈原因导致连接建立失败。所以清除掉连接后重新建立连接。

在实际开发过程中,还发现对于小米手机,蓝牙连接一直有问题,有时候能成功,但是大部分时间失败,同样的带呀,华为等其余手机不会能正常,后来排查原因是因为connectgatt(BluetoothDevice,autoconnect,BluetoothGattCallback)的自动重连autoconnect设置了true,导致连接不上,或者连接等待的时间很长。把autoconnect设置为false,则能稳定快速的连接上外设的蓝牙设备,猜测应该是小米改过蓝牙底层的代码。

处理后的代码:

@Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
        if (status == BluetoothGatt.GATT_SUCCESS) {
            Log.e(TAG, "成功建立蓝牙通道");
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                gatt.discoverServices();
                Log.e(TAG, "蓝牙连接成功!");
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                Log.e(TAG, "蓝牙连接断开!");
                gatt.connect();
            }
            if (bleStateInterface != null) {
                bleStateInterface.onConnectedChange(newState);
            }
        } else {
            Log.e(TAG, "建立蓝牙通道失败"+status);
            if (bleStateInterface != null) {
                bleStateInterface.onBleGattStatus(status);
            }
        }
    }

相应回调处理:

bleGattCallback.setOnConnectedChange(new BleGattCallback.BleStateInterface() {
            @Override
            public void onConnectedChange(final int newState) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (newState == BluetoothProfile.STATE_CONNECTED) {
                            Toast.makeText(BlueToothDetailsActivity.this, "蓝牙连接成功了!", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(BlueToothDetailsActivity.this, "蓝牙连接断开!", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
            @Override
            public void onBleGattStatus(int status) {
                //关闭后重新连接
                bleManager.closeGatt();
                bleManager.connectDevice(false,bleGattCallback);
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容