This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How do I resolve a characteristic write failure on Android & nRF51?

Hi all,

I am trying to get an Android Nexus 5X app to work with an NRF chip, and facing some problems, specifically with writing to a characteristic. Really hope to get some help.

Specifically, I am trying to set a real-time clock (RTC) on the nrf by means of the app. Details given to me about the GATT service characeristic are as follows:

Properties: Read - Mandatory, Write - Mandatory, WriteWithoutResponse - Excluded, SignedWrite - Excluded, Notify - Excluded, Indicate - Excluded, Writeable Auxilliaries - Excluded, Broadcast - Excluded.

Security: ENC_NO_MITM

Descriptors: None

After connecting to the nrf, my implementation of 'onServiceDiscovered()' is as follows.

public void onServicesDiscovered(BluetoothGatt gatt, int status) {

       // Get the characteristic 
       BluetoothGattCharacteristic loggingRTCCharacteristic = gatt.getService(loggingServiceUUID).getCharacteristic(loggingRTCControlPointCharacteristicUUID);
        // Read characteristic (which succeeded, as onReadCharacteristic is invoked)
        boolean successFlag = gatt.readCharacteristic(loggingRTCCharacteristic);

        // Check for success. 
        
        // Set a plausible timestamp.
        int year_lsb = 221; int year_msb = 7;
        int month = 3;
        int dayOfMonth = 4;
        int dayOfWeek = 7;
        int hour = 9;
        int min = 3;
        int sec = 15;

        byte[] timeStamp = {(byte)year_lsb, (byte)year_msb, (byte)month, (byte)dayOfMonth, (byte)dayOfWeek, (byte)hour, (byte)min, (byte)sec};
       
       logingRTCCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
        
        // This returns a failure. The onCharacteristicWrite() function is not invoked either.
        successFlag = gatt.writeCharacteristic(loggingRTCCharacteristic); }

The readcharacteristic() is successful, while writecharacteristic() failed. I have tried to change the write type of the other 2 options: WRITE_TYPE_ENCRYPTED, and WRITE_TYPE_NO_RESPONSE, but no joy. Can anyone please advise, as I cannot seem to find anything that helps online. Thanks!

Related