BLE_GATTS_EVT_WRITE is not triggered at the peripheral side by sd_ble_gattc_write() at the central side

Hey Nordic,

Currently, I am working on a project based on "ble_app_uart" and "ble_app_uart_c". You can download the projects below:

I am using:

  • VSCODE as IDE, and SDK 17.1.0

The central device currently connects to the peripheral one when the filter below is matched:

static ble_uuid_t const m_scan_uuid[] =   {{BLE_UUID_OUR_CHARS_SERVICE, BLE_UUID_TYPE_VENDOR_BEGIN}/* ,
                                            {BLE_UUID_NUS_SERVICE, BLE_UUID_TYPE_VENDOR_BEGIN} */};

Also, I have two flags:

static uint8_t flag_connected = 0;
static uint8_t flag_discovered = 0;

I set them to 1 to detect both BLE_GAP_EVT_CONNECTED happened and the characteristic with the UUID that I am looking for is discovered in db_disc_handler(). Then I use both in idle_state_handle() as below:

/**@brief Function for handling the idle state (main loop).
 *
 * @details Handles any pending log operations, then sleeps until the next event occurs.
 */
static void idle_state_handle(void)
{
    if (NRF_LOG_PROCESS() == false)
    {
        nrf_pwr_mgmt_run();
    }

    if(flag_connected == 1 && flag_discovered == 1)
    {
        writeChar(data);
        NRF_LOG_INFO("data: %c: 0x%x", data, data);

        flag_connected = 0;
        flag_discovered = 0;
    }
}

I do this in order to write some value into that specific discovered characteristic at the peripheral side. But BLE_GATTS_EVT_WRITE is not triggered and I cannot see the printout of the following block in PuTTY:

        case BLE_GATTS_EVT_WRITE:
            printf("BLE_GATTS_EVT_WRITE");
            for(charIndex = 0; charIndex < NO_OF_CHARACTERISTICS; charIndex++)
            {
                if(p_ble_evt->evt.gatts_evt.params.write.uuid.uuid == BLE_UUID_OUR_CHARACTERISTC_UUID + charIndex)
                {
                    NRF_LOG_INFO("16-bit UUID: 0x%x", p_ble_evt->evt.gatts_evt.params.write.uuid.uuid);
                    NRF_LOG_INFO("value = 0x%x%x%x%x", (p_ble_evt->evt.gatts_evt.params.write.data[0]), 
                                                    (p_ble_evt->evt.gatts_evt.params.write.data[1]), 
                                                    (p_ble_evt->evt.gatts_evt.params.write.data[2]), 
                                                    (p_ble_evt->evt.gatts_evt.params.write.data[3]));
                }
            }

On the other hand, when I connect the peripheral with my phone on nRF Connect, I write some value into that characteristic, and then I can see the printout of the just-above block. Do you have any idea for resolving this issue?

I hope I have done a good job while explaining the issue.

Edit1: I have found an event called "BLE_GATTC_EVT_WRITE_RSP" from here. I used it at the central side under ble_evt_handler(). This is the RTT Viewer logging at the central side:

00> <info> app_timer: RTC: initialized.
00> 
00> <info> app: NRF_BLE_SCAN_EVT_FILTER_MATCH
00> 
00> <info> app: BLE_GAP_EVT_CONNECTED
00> 
00> <info> app: ************************
00> 
00> <info> app: p_evt->evt_type: 0x0
00> 
00> <info> app: srv_uuid.uuid: 0xF00D
00> 
00> <info> app: srv_uuid.type: 2
00> 
00> <info> app: char_count: 1
00> 
00> <info> app: chars[0].char.uuid.uuid: 0xBEEF
00> 
00> <info> app: write_params.len: 1
00> 
00> <info> app: (custom_conn_handle: 0x0) Error: 0x5
00> 
00> <info> app: sd_ble_gattc_write() returned: NRF_SUCCESS
00> 
00> <info> app: data= U, 0x55
00> 
00> <info> app: BLE_GATTC_EVT_WRITE_RSP
00> 
00> <info> app: p_ble_evt->evt.gattc_evt.params.write_rsp.len= 0
00> 

"BLE_GATTC_EVT_WRITE_RSP" occurs but since the data length is zero, I think the write operation couldn't be done successfully.

Edit2: Somehow, uploaded project files seemed to me not downloadable. Anyway, I uploaded them again, the change I did in Edit1 is also included at the central side.

Thanks in advance,

Omer

Parents
  • Hello Omer,

    Sorry for the delayed response. I quickly ran your code on 2 DKs here, and experienced the same problem you reported. I then  noticed you had commented the "write_params.handle" assignment in your writeChar() implementation. So, you were effectively writing to attribute handle zero, which seems to cause the softdevice to simply ignore the write request (i.e. not notify the app of the event).

    As a test, I hardcoded the handle value to 0x10 like this:

    write_params.handle = 0x10;// gs_peripheral[peripheral_id].read_handle;

    And got the following RTT output:

    00> <info> app: BLE_GATTS_EVT_WRITE
    00>  
    00> <info> app: 16-bit UUID: 0xBEEF
    00>  
    00> <info> app: value = 0x550240

    Best regards,

    Vidar

Reply
  • Hello Omer,

    Sorry for the delayed response. I quickly ran your code on 2 DKs here, and experienced the same problem you reported. I then  noticed you had commented the "write_params.handle" assignment in your writeChar() implementation. So, you were effectively writing to attribute handle zero, which seems to cause the softdevice to simply ignore the write request (i.e. not notify the app of the event).

    As a test, I hardcoded the handle value to 0x10 like this:

    write_params.handle = 0x10;// gs_peripheral[peripheral_id].read_handle;

    And got the following RTT output:

    00> <info> app: BLE_GATTS_EVT_WRITE
    00>  
    00> <info> app: 16-bit UUID: 0xBEEF
    00>  
    00> <info> app: value = 0x550240

    Best regards,

    Vidar

Children
  • Hello Vidar,

    Firstly, thanks for your answer. Once I have physical access to my DKs, I will give it a try. I guess your RTT outputs are at the peripheral side, so it seems to be resolved.

    But I have a question. Is there a logic behind your assignment of this handle value to 0x10? It looks like a magic number but I want to learn why. The reason why I commented out that line was I don't have any idea about it. Could you please a little bit explain it to me?

    Thanks again,

    Omer

Related