Reputation: 5016
I need to write and read a characteristic value to Android BLE device. 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.
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. Where I'm going wrong?
Upvotes: 1
Views: 2415
Reputation: 88
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.
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. Send the value to the remote BLE device.
gatt.writeDescriptor(descriptor);
After that, you may get the data in callback function:
onCharacteristicChanged
At least above work for me! Hope can help someone~
Upvotes: 2