How can I change the value of BLE characteristic in the code?

Zeni241
Posts: 85
Joined: Tue Nov 20, 2018 4:28 am

How can I change the value of BLE characteristic in the code?

Postby Zeni241 » Tue Nov 17, 2020 2:41 pm

I am following sample of “gatt_server_service_table” at https://github.com/espressif/esp-‎id ... vice_table .‎
My question is how can I change the value of characteristic in the code? I mean if I want to convey ‎changes in temperature value to client, how will I change the characteristic value, so that it could be ‎notified when notification has been enabled by client?‎

Zeni241
Posts: 85
Joined: Tue Nov 20, 2018 4:28 am

Re: How can I change the value of BLE characteristic in the code?

Postby Zeni241 » Thu Nov 19, 2020 6:04 am

Finally I was able to change value of characteristic and also notify to the client.
For anyone stuck like me, while following gatt_server_service_table example:

I defined characteristic value as :

Code: Select all

uint8_t char_value[] = "InitialValue";
Added characteristic in the table as follows:

Code: Select all

  [IDX_CHAR_A] =
            {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write_notify}},

        /* Characteristic Value */
            [IDX_CHAR_VAL_A] =
            {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&GATTS_CHAR_UUID_TEST_A, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, GATTS_DEMO_CHAR_VAL_LEN_MAX, sizeof(char_value), (uint8_t *)char_value}},

             [IDX_CHAR_CFG_A] =
            {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(heart_measurement_ccc), (uint8_t *)heart_measurement_ccc}},
Now to give new value to characteristic:

Code: Select all

uint8_t nval[ ] = "NewValue";
         esp_ble_gatts_set_attr_value(heart_rate_handle_table[IDX_CHAR_VAL_A], sizeof(nval), nval);

and notify the client:

Code: Select all

  esp_ble_gatts_send_indicate(heart_rate_profile_tab[PROFILE_APP_IDX].gatts_if , 0, heart_rate_handle_table[IDX_CHAR_VAL_A],
                                        sizeof(nval), nval, false);
And that is it!!! :mrgreen:

Who is online

Users browsing this forum: Google [Bot] and 123 guests