Android ble (蓝牙低功耗)使用注意事项(转)

一、如何定义ble中service uuid?

  • 蓝牙标准规范里面定义了很多已经定义过的service uuid,如果冲突了会造成很多意外的问题。
  • 蓝牙的service uuid的格式如下
    UUID.fromString("00001234-0000-1000-8000-00805f9b34fb")
  • 在Android可以简单的采用这个原则:1、利用这个字符串【00002903-0000-1000-8000-00805f9b34fb】用第5-8位的数字做变化,其他数字保持不变。比如
    UUID.fromString("00007777-0000-1000-8000-00805f9b34fb")
    UUID.fromString("00009999-0000-1000-8000-00805f9b34fb")

二、ble中心设备开启扫描,设置所关心的serviceuuid。

bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
        List<ScanFilter> filters = new ArrayList<>();
       ScanFilter filter = new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb");)
                .build();
        filters.add(filter);
        ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); bluetoothLeScanner.startScan(filters, scanSettings, scanCallback); 

new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb");

三、ble外围设备可以在广播的时候设定AdvertiseData的magic number【manufacturerId 和 manufacturerSpecificData】。这样即使定义service uuid跟别人的有冲突,也可以在中心过滤该magic number来找到符合自己需求的外围设备

  • 外围构建AdvertiseData
AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .addServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb"))
                .addManufacturerData(0x7777, new byte[]{0x07, 0x07}) .build(); 
  • 中心处理AdvertiseData中的
final ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); ScanRecord scanRecord = result.getScanRecord(); SparseArray<byte[]> mandufacturerData = scanRecord.getManufacturerSpecificData(); 

此时可以根据mandufacturerData来匹配自己设定的外围设备

四、什么时候ble的中心和外围才算真正的连接上?(可以开始传输数据了)

在BluetoothGattCallback中的关于此问题有三步回调
1、public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)

  • 这是ble中心和外围连接后,最先触发的回调。newstate等于BluetoothProfile.STATE_CONNECTED仅仅表示中心设备连接上了,这个时候需要去调用BluetoothGatt去发现服务。

  • 注意case1,在new state为BluetoothProfile.STATE_DISCONNECTED时,务必关掉BluetoothGatt,因为每次调用mBluetoothGatt = device.connectGatt(SpeakerApp.appContext, false, mGattCallBack);都会生成新的对象,而不会去主动关闭老的对象

  • 注意case2,133问题,iPhone 和 某些Android手机作为旁支会出现蓝牙初始连接就是133,此情况下应该立刻重新扫描连接。133问题链接

//iPhone 和 某些Android手机作为旁支会出现蓝牙初始连接就是133,此情况下立刻重试 
            if (status == 133) {
                RLog.d(TAG, "发生设备初始连接133情况,需要重新扫描连接设备");
                mBluetoothGatt.close();
                //!!!需要去增加代码进行重新扫描重连 return; } if (newState == BluetoothProfile.STATE_CONNECTED) { mBluetoothGatt = gatt; mBluetoothGatt.discoverServices(); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { mBluetoothGatt.close(); mBluetoothGatt = null; } 

2、 public void onServicesDiscovered(BluetoothGatt gatt, int status)
mBluetoothGatt.discoverServices()执行后得到的callback,如果状态为GATT_SUCCESS,则可以获取ble旁支发起广播的service和descriptor,把广播设为enable

mCharacteristic = service.getCharacteristic(UUID.fromString("00007770-0000-1000-8000-00805f9b34fb"));
                if (mCharacteristic == null) {
                    RLog.e(TAG, "Can't find target characteristic.");
                    return; } gatt.setCharacteristicNotification(mCharacteristic, true); BluetoothGattDescriptor descriptor = mCharacteristic.getDescriptor(UUID.fromString("00007777-0000-1000-8000-00805f9b34fb")); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); 

3、public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
只有这一步status == BluetoothGatt.GATT_SUCCESS,才可以真正的传输数据,如果在第一步或者第二步就开始传输数据,会在某些特定的case下导致未知的bug或者空指针错误

所以,在中心设备跟外围开始连接后,你可以设定一个超时时间,在超时时间过后,依然没能回调onDescriptorWrite并获得BluetoothGatt.GATT_SUCCESS,则此次过程失败,你可以根据实际情况进行重连或者提示错误

五、mtu-20字节问题

mtu20的来源:GATT是基于ATT Protocol的,而它的 core spec里面定义了ATT的默认MTU为23个bytes,除去ATT的opcode一个字节以及ATT的handle2个字节之后,剩下的20个字节便是留给GATT的了

如果要传输大于20字节的数据怎么办?

1、 系统mtu可以支持修改到512字节,完成大数据量的传输。但是由于涉及到中心和旁支都需要修改,会造成很大的局限性和底层修改量,而且会触发比如某些设备第一次修改不生效,另一个设备一次连接中只能修改一次等bug,非常不可取,十分不建议。

2、分包传输,自己设计协议分包传输是最可取的方案,需要注意的是在分包后,每一个包之间写入数据需要设置间隔,比如100ms。

六、写数据之前做校验,判断获取的characteristic是否满足可读,可广播,或者需要回复等约定。

  return ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 ||
                (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0);

七、丢数据包问题

在做好5和6的基础上,依然会在一些设备上出现,由于系统原因,ble刚开始的发送第一个数据出现丢包,请对此做出特殊处理。

八、解析数据

  • 中心端mtu分包发给外围后,外围可以在
    public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) 接收到数据并还原成原始数据

  • 外围端mtu分包发给中心端后,中心端可以在 public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 接收到数据并还原成原始数据

  • 注意,对于一些蓝牙设备,总有一些特殊的状态,对于接受到的数据一定要进行正确性校验

九、other坑

  • ble中的PROPERTY_WRITE_NO_RESPONSE不可信任,google的有些版本并没有去读取这个属性值,而是直接设置为需要résponse,稳妥的方式最好设置为必须回复

  • 在项目中如果有多个ble或 ble + 经典蓝牙连接,在一些临界情况(比如设备重启,crash闪退重启),a ble连接可能需要移除b ble(或 b经典蓝牙)连接产生的设备,否则会导致a ble一直连接不上。

 BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice("另一个ble 或者 蓝牙设备mac值");
                    if (remoteDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
                        try {
                            Method removeBond = remoteDevice.getClass().getDeclaredMethod("removeBond");
                            removeBond.invoke(remoteDevice);
                            RLog.d(TAG , "成功移除系统bug"); } catch (Exception e) { RLog.e(TAG , "反射异常"); } } 
 

作者:RDuwan.链接:



猜你喜欢

转载自www.cnblogs.com/sunupo/p/10405371.html