![](/img/trans.png)
[英]Android BLE write Characteristic locks up onCharacteristicWrite/onCharacteristicChange
[英]Android BLE: Identify the Characteristic Type?
我正在用BLE开发一个android应用程序。 该应用程序的要求是使用各种输入来更新特定硬件中的电压变化。 因此,我在此应用程序中启用了BLE notify 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);
我在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
}
但是我的问题是,我在相同的Gatt回调方法中也得到了正常响应。 我想将通知更新为用户界面中的特定方式。 因此,我需要将正常响应和通知分开。 有没有办法做同样的事情? 还是有任何标识特定消息的选项来自notify?
通常,我们创建(在硬件方面)具有WRITE和NOTIFY属性的一个特征,以便我们可以在需要或完全启用notify来获取实时数据时进行读取。
如果您可以访问硬件固件并可以添加特性,则最好将电压特性和响应特性分开。 因此,您可以测试onCharacteristicChanged参数:
int propertiesFlags = characteristic.getUuid();
做到这一点的另一种好方法,这就是我通常要做的,就是使用一个特征,但是分配数据间隔。 您的应用程序和硬件之间的某种约定:
@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.
}
}
否则,响应将只是特性变化,意味着新的硬件电压值。
如果您无法更改固件,则不会。 根据您的解释,如果您要求输入值或更改值,则似乎没有仪表触发NOTIFY特征类型。
如果是这样,那么我可以想象的唯一方法就是遵循。
希望能帮助到你。
更新:
看来,您正在使用RN4020 MLDP不暴露了私人服务文档,根据本
我能找到的最好的结果是:
// 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
从这里
这意味着唯一的数据传输方式是使用
// private static final String MLDP_DATA_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a000301"; //Characteristic for MLDP Data, properties - notify, write
并且此特征只有NOTIFY,而不是读取。 因此,如果使用它,则无法避免在设备端进行通知回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.