Android BLE:识别特征类型?

Nit*_*ith 6 java android android-layout bluetooth-lowenergy android-studio

我正在用BLE开发一个Android应用程序.该应用的要求是用各种输入更新特定硬件中的电压变化.所以我在这个应用程序中启用了BLE通知API.这将在最近的硬件电压的一段时间内通知应用程序.

履行

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor des = characteristic.getDescriptors();
des.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);         
//Set the value of the descriptor to enable notification
                    mBluetoothGatt.writeDescriptor(des);
Run Code Online (Sandbox Code Playgroud)

我在Gatt CallBack方法的通知值中收到通知

      @Override
      public void onCharacteristicChanged(BluetoothGatt Gatt, BluetoothGattCharacteristic characteristic) {
                    Log.w(TAG, "**ACTION_DATA_AVAILABLE**" + characteristic.getUuid());
//Indication or notification was received
                    broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic);                     
//Go broadcast an intent with the characteristic data
                }
Run Code Online (Sandbox Code Playgroud)

但我的问题是,我在相同的Gatt回调方法中也得到了正常的响应.我想在UI中以特定方式更新通知.所以我需要分开正常的响应和通知.有没有办法做同样的事情?或者任何识别特定消息的选项来自通知?

Ach*_*mil 3

一般来说,我们(在硬件方面)创建一个同时具有 WRITE 和 NOTIFY 属性的特征,以便我们可以随时读取或完全启用通知来获取实时数据。

如果您可以访问硬件固件并可以添加特性,那么最好将电压特性和响应特性分开。因此,您可以测试 onCharacteristicChanged 参数:

int propertiesFlags = characteristic.getUuid();
Run Code Online (Sandbox Code Playgroud)

另一种好方法是使用一个特征,但分配数据间隔,这也是我通常所做的。您的应用程序和硬件之间的某种约定:

@Override
public void onCharacteristicChanged(BluetoothGatt Gatt, BluetoothGattCharacteristic characteristic) {
    byte[] data = characteristic.getValue();
    if(data[1] < SOME_PREDIFINDED_VALUE){
        //it's a real-time data update
    }else{
        //it's a response to some data you sent.
    }

}
Run Code Online (Sandbox Code Playgroud)

否则,响应将只是一个特征变化,意味着硬件电压的新值。