简体   繁体   English

Android BLE:确定特征类型?

[英]Android BLE: Identify the Characteristic Type?

I am developing an android application with BLE. 我正在用BLE开发一个android应用程序。 The requirement of this application is to update the voltage variation in a specific hardware with various inputs. 该应用程序的要求是使用各种输入来更新特定硬件中的电压变化。 So I enable BLE notify API in this application. 因此,我在此应用程序中启用了BLE notify API。 This will notify the application in a period of time with latest hardware voltage. 这将在一段时间内以最新的硬件电压通知应用程序。

Implementation 实作

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);

I am getting notification in the notification values in the Gatt CallBack method 我在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
                }

But my problem is, I am getting the normal response also in the same Gatt callback method. 但是我的问题是,我在相同的Gatt回调方法中也得到了正常响应。 I want to update the notification as a specific way in the UI. 我想将通知更新为用户界面中的特定方式。 So I need to separate normal response and notification. 因此,我需要将正常响应和通知分开。 Is there any way to do the same? 有没有办法做同样的事情? Or any option to identify the particular message is from notify? 还是有任何标识特定消息的选项来自notify?

In general, we create (on the hardware side) one characteristic with both WRITE and NOTIFY properties, so that we can read whenever we want or completely enable notify to get real-time data. 通常,我们创建(在硬件方面)具有WRITE和NOTIFY属性的一个特征,以便我们可以在需要或完全启用notify来获取实时数据时进行读取。

If you have access to the hardware firmware and can add characteristics, it would be nice to separate the voltage characteristic, and the response one. 如果您可以访问硬件固件并可以添加特性,则最好将电压特性和响应特性分开。 Thus you can test upon the onCharacteristicChanged argument : 因此,您可以测试onCharacteristicChanged参数:

int propertiesFlags = characteristic.getUuid();

Another good way to do it, and that's what I do usually, is using one characteristic, but assigning data intervals. 做到这一点的另一种好方法,这就是我通常要做的,就是使用一个特征,但是分配数据间隔。 Some kind of convention between your app and the hardware : 您的应用程序和硬件之间的某种约定:

@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.
    }

}

Otherwise, the response will just be a characteristic change meaning a new value of hardware voltage. 否则,响应将只是特性变化,意味着新的硬件电压值。

No if you don't have possibility to change the firmware. 如果您无法更改固件,则不会。 According to your explanation it seems that NOTIFY characteristic type is triggered no meter if you asked for value or the value is changed. 根据您的解释,如果您要求输入值或更改值,则似乎没有仪表触发NOTIFY特征类型。

If so, the only way I can imagine as solution is following. 如果是这样,那么我可以想象的唯一方法就是遵循。

  1. You have asked for value. 您要求的价值。 You have to set flag and you have to consider that all the values reported while flag is set are values that you have been asking for. 您必须设置标志,并且必须考虑设置标志时报告的所有值都是您一直要求的值。
  2. You have not asked for value. 您尚未要求价值。 Consider it as notifications. 将其视为通知。

Hope it helps. 希望能帮助到你。

Update: 更新:

It seems that you are using RN4020 MLDP which doesn't have exposed documentation for private service, according to this 看来,您正在使用RN4020 MLDP不暴露了私人服务文档,根据

The best I could found was the following: 我能找到的最好的结果是:

//    private static final String MLDP_PRIVATE_SERVICE = "00035b03-58e6-07dd-021a-08123a000300"; //Private service for Microchip MLDP
//    private static final String MLDP_DATA_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a000301"; //Characteristic for MLDP Data, properties - notify, write
//    private static final String MLDP_CONTROL_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a0003ff"; //Characteristic for MLDP Control, properties - read, write
//    private static final String CHARACTERISTIC_NOTIFICATION_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";  //Special UUID for descriptor needed to enable notifications

from here 这里

Which means that only way to transfer data is using 这意味着唯一的数据传输方式是使用

//    private static final String MLDP_DATA_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a000301"; //Characteristic for MLDP Data, properties - notify, write

and this characteristic has only NOTIFY, not read. 并且此特征只有NOTIFY,而不是读取。 So if you are using it, you can not avoid notification callback on device side. 因此,如果使用它,则无法避免在设备端进行通知回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM