Search code examples
bluetooth-lowenergyesp32arduino-esp32

EPS32 switch from Arduino to IDF - need to find the corresponding BLE function


I've a a working Arduino-code for my project and rewriting this in ESP-IDF.

For the following Arduino line I'm struggling to find the corresponding function for IDF.

static BLEUUID charUUID_tx("0000ff02-0000-1000-8000-00805f9b34fb");
uint8_t reqBasicInfo[7] = {0xdd, 0xa5, 0x3, 0x0, 0xff, 0xfd, 0x77};
BLERemoteCharacteristic *pRemoteCharacteristicTX;
pRemoteCharacteristicTX->writeValue(reqBasicInfo, sizeof(reqBasicInfo));

Do I use esp_ble_gattc_write_char ? What has to be the handle? 0xff02?

Thank you for your input.


Solution

  • You're correct, you can use the esp_ble_gattc_write_char() function as it technically does what you need. However, this is a low level function for which you need to get the characteristic value handle, connection handle, etc. Below is code (copied from here) that shows how this is done:-

    case ESP_GATTC_GET_CHAR_EVT:
        if (p_data->get_char.status != ESP_GATT_OK) {
            break;
        }
        ESP_LOGI(GATTC_TAG, "GET CHAR: conn_id = %x, status %d", p_data->get_char.conn_id, p_data->get_char.status);
        ESP_LOGI(GATTC_TAG, "GET CHAR: srvc_id = %04x, char_id = %04x", p_data->get_char.srvc_id.id.uuid.uuid.uuid16, p_data->get_char.char_id.uuid.uuid.uuid16);
    
        if (p_data->get_char.char_id.uuid.uuid.uuid16 == 0xee01) {
            gl_profile_tab[PROFILE_A_APP_ID].char_id = p_data->get_char.char_id;
    
            uint8_t value[30];
            for (int i = 0; i < sizeof(value); ++i)
            {
                value[i] = i;
            }
            esp_ble_gattc_write_char(gattc_if,
                                    conn_id,
                                    &alert_service_id,
                                    &gl_profile_tab[PROFILE_A_APP_ID].char_id,
                                    sizeof(value),
                                    value,
                                    ESP_GATT_WRITE_TYPE_RSP,
                                    ESP_GATT_AUTH_REQ_NONE);
    
    
        }
    

    Another useful references:-