Bluetooth LE not triggering OnCharacteristicWrite

Hi all,

I have a Bluetooth LE (BLE) module that requires an initial credits "topup" before sending anything. The BLE module is hooked up to my PC to a terminal, where I can see the data bytes coming in. I initialise the communications with:

mGattListener = new GattListener();
mGatt = mDevice.ConnectGatt(mApplicationContext, false, mGattListener);

This works OK. I get the BluetoothGatt object, and proceed with setting up notifications. Everything is set up correctly, because I get the Ok message on the Terminal.

My BluetoothGattCallback is as follows:

public class GattListener : BluetoothGattCallback
{
    public GattListener() : base()
    {
    }

    override public void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
    {
        // Code for characteristic changed
    }

    public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, [GeneratedEnum] GattStatus status)
    {
        if (characteristic.Uuid.Equals(UART_RX_CREDITS_UUID))
        {
            // Handle when characteristic is for RX Credits
        }
        else if (characteristic.Uuid.Equals(UART_RX_UUID))
        {
            // Handle when characteristic is for RX data
        }
        else
        {
            Debug.WriteLine("unknown characteristic written " + characteristic.Uuid.ToString());
        }
    }

    // Other overrides omitted for brevity
}

When I write to the characteristic UART_RX_CREDITS_UUID, I use:

// Set new characteristic value
mRxCreditsCharacteristic.SetValue(value);
mGatt.WriteCharacteristic(mRxCreditsCharacteristic);

This triggers the OnWriteCharacteristic event, and off it goes. When I write to the characteristic UART_RX_UUID, I use:

mRxCharacteristic.SetValue(data);
mRxCharacteristic.WriteType = GattWriteType.Default;
mGatt.WriteCharacteristic(mRxCharacteristic);

This writes the byte array data the first time, but does not trigger OnCharacteristicWrite, and I don't understand why. As such, subsequent calls to WriteCharacteristic fail. Below is a list of what I have tried (and failed to find a solution):
1. Tried forcing a successful write - WriteCharacteristic fails and does not write (returns false).
2. Made doubly sure all WriteCharacteristic are written on the same Thread (not the UI Thread) - makes no difference.
3. Tried adding a delay between writing to the characteristic - makes no difference.
4. Tried adding a delay between connecting to the device, and writing to the characteristic - makes no difference.
5. Tried changing BLE modules and phones - makes no difference.
6. Tried sending one byte only - makes no difference.

Given these (and the fact that changing hardware didn't help), it must be something going wrong in my code. What are the general rules for BLE communications? I have a callback which works with other characteristics, but not this one. Has anyone else experienced this? What should I be looking out for?

Thanks

Posts

  • eubikeubik Member ✭✭

    Hi @JohnSmith.0620, did you end up figuring out what was going wrong in your code?

  • JohnSmith.0620JohnSmith.0620 USMember ✭✭

    Hi @eubik ,
    Sorry for the late reply.
    It was a while ago, but I think I just rewrote the Bluetooth communications from scratch. I vaguely knew how the process worked, but had bits of code here and there that may have been causing issues.
    Sorry it's of not much help.

  • eubikeubik Member ✭✭

    @JohnSmith.0620 No worries! Thanks for replying and letting me know anyways

Sign In or Register to comment.