Example on setting up esp32 as BLE server?

Ok. To follow up my earlier message, I have successfully managed to get advertising of state data within Manufacturer data working correctly. This allows sending data, and also reading of data on IOS which locks downs all advertised attributes except manufacturer data.

Using latest esphome 2023.10.6

No need for structs or additional files

Make sure ble_server enabled. Note here this has id: ble

esp32_ble_server:
  id: ble
  manufacturer_data: [0xFF, 0xFF, 0xF1, 0x00]

&&

time:
  - platform: sntp
    id: ntp  
    on_time:
    - seconds: 0,10
      then:
        - lambda: |-
            ESP_LOGD("main", "Advertisement updated");           
            float f_value = id(levelmetres).state;
            uint8_t *float_data = (uint8_t*)&f_value;
            ESP_LOGD("main", "Sending updated Level %f", id(levelmetres).state );
            id(ble).set_manufacturer_data({ 0xFF, 0xFF, 0xF9, float_data[0], float_data[1], float_data[2], float_data[3]});

Using time, repeating every 10 seconds.
levelmetres = is a template level sensor.
The rest convert the float value to hex chars and updates the manufacturer data, with the values.
To read back convert the values after 0xF9 to float using Little endian.

Glenn

1 Like