0

Is there a way to come an answer after writing a hex command via Bluetooth Low Energy in android? I write the hex command over gatt, this is my write function:

/* set new value for particular characteristic */
public void writeDataToCharacteristic(final BluetoothGattCharacteristic ch, final byte[] dataToWrite) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null || ch == null) return;

    // first set it locally....
    ch.setValue(dataToWrite);
    // ... and then "commit" changes to the peripheral
    mBluetoothGatt.writeCharacteristic(ch);
}

After the write is completed the Callback tells me only it's succesfull or it's failed, but the receiver sends an answer back. In moment there is only the check it's succesfull or not, but I wan't to display the answer from receiver. Is there a way to show the answer?

    /*The callback function*/
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        String deviceName = gatt.getDevice().getName();
        String serviceName = BleNamesResolver.resolveServiceName(characteristic.getService().getUuid().toString().toLowerCase(Locale.getDefault()));
        String charName = BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString().toLowerCase(Locale.getDefault()));
        String description = "Device: " + deviceName + " Service: " + serviceName + " Characteristic: " + charName;

        // we got response regarding our request to write new value to the characteristic
        // let see if it failed or not
        if(status == BluetoothGatt.GATT_SUCCESS) {
             mUiCallback.uiSuccessfulWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description);
        }
        else {
             mUiCallback.uiFailedWrite(mBluetoothGatt, mBluetoothDevice, mBluetoothSelectedService, characteristic, description + " STATUS = " + status);
        }
    };
rene
  • 41,474
  • 78
  • 114
  • 152
user3181654
  • 1
  • 1
  • 3

1 Answers1

0

In your callback have you tried

characteristic.getValue() ?

If this value is different from the one you set and sent across this could be the response you're looking for.

Also, make sure that the Characteristic you're writing to has read or notifiable properties. You can do this as follows:

int props = characteristic.getProperties();
String propertiesString = String.format("0x%04X ", props);
if((props & BluetoothGattCharacteristic.PROPERTY_READ) != 0) propertiesString += "read ";
if((props & BluetoothGattCharacteristic.PROPERTY_WRITE) != 0) propertiesString += "write ";
if((props & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) propertiesString += "notify ";
if((props & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) propertiesString += "indicate ";

It's possible there are two Characteristics for a service - one that's writeable (for sending data) and one that's Notifiable for receiving data. That makes it possible for the receiver to do asynchronous processing. Make sure that there isn't another Characteristic in the Service that's Notifiable or Readable

Michael
  • 703
  • 1
  • 5
  • 11