App closes when try to read BLE server strings data

Hi,

I created a BLE server on my ESP32 so I can read data from my cellphone. It worked with some BLE apps, such as nRF Connect and BLE Scanner, but I tried to create an app so I can read the data periodically. However, as soon as I try to read, the app closes.

The section of the esp-idf code and the app inventor can be found below.


// Read data from ESP32 defined as server
static int device_read(uint16_t con_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
{
float dado;
char string[6];

xQueueReceive(fila1,&dado,portMAX_DELAY);
ESP_LOGW("BLE","Distancia = %f m",dado);

//os_mbuf_append(ctxt->om, "Data from the server", strlen("Data from the server"));
sprintf(string,"%.2f",dado);
os_mbuf_append(ctxt->om, string, strlen(string));
vTaskDelay(pdMS_TO_TICKS(1000));
return 0;

}


static const struct ble_gatt_svc_def gatt_svcs = {
{.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = BLE_UUID16_DECLARE(0x180), // Define UUID for device type
.characteristics = (struct ble_gatt_chr_def){
{.uuid = BLE_UUID16_DECLARE(0xFEF4), // Define UUID for reading
.flags = BLE_GATT_CHR_F_READ,
.access_cb = device_read},
{0}}},
{0}};

Two N in Connect

string is a data type, you should not use it to name a variable.

Where did you get the Script (Sketch) from? As you can see in your Topic, there is an issue with a character, which probably means the Sketch file has been saved in the wrong format at some time. However, we cannot see the part where data is sent to the App? A common fault is sending the wrong data format or exceeding the amount of data transferable. If your App reads and writes, make sure they don't clash - think of the BLE transfer channel as a water pipe - the water can only flow in one direction at a time.

App Inventor Project:

...You don't have to type the permission names, App Inventor has built-in tags for them. For most Android versions you do not need permissions, so your code should test the API version and only request permissions when required to do so. If permissions are required, you are missing one - fine location.

Stop scanning before connecting.

Again, you are not showing us the relevant code - we can't see the Blocks that are reading the server data, or whether the correct characteristic is used.

Thank you very much for your tips!
The problem was that the UUID service I was using was actually being used by another access.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.