[英]Not able to read a characteristic value from BLE device
I need to write and read a characteristic value to Android BLE device. 我需要为Android BLE设备编写和读取特征值。 I'm able to write but not read. 我能写但不能读。 Here is how I'm writing : 这就是我写作的方式:
public void writeCustomCharacteristic(int value) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("<MY SERVICE UUID>"));
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("<MY CHARACTERSTIC UUID>"));
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
mWriteCharacteristic.setValue(value,BluetoothGattCharacteristic.FORMAT_UINT8,0);
if(!mBluetoothGatt.writeCharacteristic(mWriteCharacteristic)){
Log.w(TAG, "Failed to write characteristic");
}else{
Log.w(TAG, "Success to write characteristic");
}
}
And this operation is success if I use 如果我使用这个操作是成功的
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
If I use any other write types rather than WRITE_TYPE_NO_RESPONSE
then it is not triggering onCharacteristicWrite()
callback method. 如果我使用任何其他写类型而不是WRITE_TYPE_NO_RESPONSE
那么它不会触发onCharacteristicWrite()
回调方法。
Here is how I'm reading the characteristic : 这是我正在阅读的特征:
private boolean readCharacteristics(int groupPosition,int childPosition){
try{
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(2);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(characteristic, true);
}
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicIndication(characteristic, true);
}
waitSometime(100);
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
byte[] value = characteristic.getValue(); //this is being NULL always
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true);
mNotifyCharacteristic = null;
}
mBluetoothLeService.writeCharacteristic(characteristic, value);
if(value!=null&&value.length>0) {
Toast.makeText(DeviceControlActivity.this,"success reading data",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(DeviceControlActivity.this,"error reading data",Toast.LENGTH_LONG).show();
}
}
/* if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) {
mNotifyCharacteristic = characteristic;
//characteristic.setWriteType();
mBluetoothLeService.setCharacteristicIndication(
characteristic, true);
byte[] value=characteristic.getValue();
}*/
return true;
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
characteristic.getValue()
returns always null. characteristic.getValue()
返回null。 Where I'm going wrong? 我哪里错了?
Here is my opinion: 这是我的意见:
You need to figure out what kind of characteristic you are going to get data? 你需要弄清楚你将获得数据的特征是什么? Some of them need to set indication or notify first! 他们中的一些人需要先设置指示或通知! Like: 喜欢:
a. 一种。 Get the descriptor of the characteristic 获取特征的描述符
BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptors().get(0);
//Usually is Client_Characteristic_Configuration
b. 湾 According to the characteristic, set the property() of descriptor true. 根据特性,设置描述符的property()为true。
if ((gattCharacteristic.PROPERTY_NOTIFY)> 0 ){
if (descriptor != null) {
descriptor.setValue((BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE));
}
}
else if((gattCharacteristic.PROPERTY_INDICATE) >0 ){
if(descriptor != null){
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
}
}
c. C。 Send the value to the remote BLE device. 将值发送到远程BLE设备。
gatt.writeDescriptor(descriptor);
After that, you may get the data in callback function: 之后,您可以获得回调函数中的数据:
onCharacteristicChanged
At least above work for me! 至少上面的工作对我来说! Hope can help someone~ 希望可以帮助某人〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.