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

Issue when sending multiple notifications consecutively on single characteristic

I just ran a few experiments on calling sd_ble_gatts_hvx() multiple times based on SDK v10's ble_app_uart example. I have configure the app so that it for each external event, for 15 times, it increments a byte and call sd_ble_gatts_hvx() with that byte.

However, through monitoring the BLE_EVT_TX_COMPLETE, I observed that only two TX_COMPLETE events are thrown each time. One with a count of 6, another with a count of 1.

On my experimental Android application, I could also only receive 7 notification each time. However, the next time I trigger the 15 transfers, I receive 7 notifications of value starting from 16, as shown in the log here.

04-08 05:46:36.153 25065-25080/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #0 - value = 01
04-08 05:46:36.156 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #1 - value = 02
04-08 05:46:36.156 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #2 - value = 03
04-08 05:46:36.157 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #3 - value = 04
04-08 05:46:36.157 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #4 - value = 05
04-08 05:46:36.157 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #5 - value = 06
04-08 05:46:36.157 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #6 - value = 07

04-08 05:46:38.933 25065-25080/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #7 - value = 16
04-08 05:46:38.936 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #8 - value = 17
04-08 05:46:38.936 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #9 - value = 18
04-08 05:46:38.937 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #10 - value = 19
04-08 05:46:38.937 25065-25079/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #11 - value = 20
04-08 05:46:38.939 25065-25080/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #12 - value = 21
04-08 05:46:38.939 25065-25080/com.simple_ble_demo D/hieu-SimpleGattCallback: notification #13 - value = 22

Apparently, the 15 sd_ble_gatts_hvx() only results in transfer in two connection intervals, one with 6 packets sent and one with 1 packet sent. 8 / 15 calls did not result in any notification at all.

Why is this?

Additional information:

  • Firmware's main.c: main.c

  • My Android app's notification handling method:

     int mCntr = 0;
     @Override
     public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
         String s = "";
         for (byte tempByte : characteristic.getValue()) {
             s += String.format("%02d", tempByte);
         }
         Log.d(LOG_TAG, "notification #" + (mCntr++) + " - value = " + s); // mCntr reset every connection
     }
    
  • When you call sd_ble_gatts_hvx it doesn't immediately send your packet. The packet is queued up in the stack and the function call returns. The stack only has a limited number of buffers (6) before it runs out of buffers to hold your packets. When that happens the stack will return a BLE_ERROR_NO_TX_BUFFERS error code and you need to wait for a BLE_EVT_TX_COMPLETE from the stack before you can queue up more packets to send.

    This answer may help you out with this: TX Complete

Related