Files
esp-idf/examples/bluetooth/nimble/ble_phy/phy_prph/README.md
Rahul Tank 432cd1ebdd fix(nimble): Improve safety, fix bugs, and update docs across NimBLE examples
- Remove unused headers from examples
- Improve periodic adv/sync example functionality and readability
- Use ble_hs_id_infer_auto() instead of hardcoded BLE_OWN_ADDR_PUBLIC/RANDOM
- Add ble_hs_util_ensure_addr() calls in on_sync for proper address setup
- Use correct ext adv instance (0 instead of 1) in phy_prph, l2cap_coc, multi_conn
- Fix struct name: ble_gap_periodic_adv_enable_params -> ble_gap_periodic_adv_start_params
- Add CONFIG_BT_NIMBLE_GAP_SERVICE guards around ble_svc_gap_device_name_set
- Fix unsafe AD data parsing with bounds checks in central examples
- Fix UUID matching bugs (off-by-one loop condition and byte order) in
  phy_cent, htp_cent, and proximity_sensor_cent
- Fix ble_multi_conn_cent address type to use dynamic inference
- Remove contradictory sm_sc=0 after sm_sc=1 in ble_multi_adv
- Add CONFIG_BT_NIMBLE_EXT_ADV=y to ble_multi_adv sdkconfig defaults
- Check return values for ble_gap_set_host_feat, nimble_port_init
- Update tutorials and READMEs to match code changes
2026-02-25 12:59:48 +05:30

6.2 KiB

Supported Targets ESP32-C2 ESP32-C3 ESP32-C5 ESP32-C6 ESP32-C61 ESP32-H2 ESP32-S3

BLE Peripheral PHY Example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

This example performs advertises on different PHY, accepts connection from client app and gets disconnected. It then advertises on different PHY and does the same.

This example aims at understanding how to establish connections on preferred PHY and changing LE PHY once the connection is established.

To test this demo, use any BLE GATT client app that supports extended advertising.

Note :

  • Make sure to run python -m pip install --user -r $IDF_PATH/requirements.txt -r $IDF_PATH/tools/ble/requirements.txt to install the dependency packages needed.
  • Currently this Python utility is only supported on Linux (BLE communication is via BLuez + DBus).

How to Use Example

Before project configuration and build, be sure to set the correct chip target using:

idf.py set-target <chip_name>

Hardware Required

  • A development board with ESP32-C3 SoC (e.g., ESP-WROVER-KIT, etc.)
  • A USB cable for Power supply and programming

See Development Boards for more information about it.

Configure the Project

Open the project configuration menu:

idf.py menuconfig

Build and Flash

Run idf.py -p PORT flash monitor to build, flash and monitor the project.

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example Output

This is the console output on successful connection:

I (459) NimBLE_BLE_PHY_PRPH: BLE Host Task Started
I (459) NimBLE: Device Address:
I (459) NimBLE: 60:55:f9:f7:3e:23
I (469) NimBLE:

I (469) NimBLE: Default LE PHY set successfully
I (479) NimBLE: GAP procedure initiated: extended advertise; instance=0

I (479) uart: queue free spaces: 8
I (489) main_task: Returned from app_main()
I (599) NimBLE: connection established; status=0
I (599) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (599) NimBLE: 60:55:f9:f7:3e:23
I (599) NimBLE:  our_id_addr_type=0 our_id_addr=
I (609) NimBLE: 60:55:f9:f7:3e:23
I (609) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (619) NimBLE: 60:55:f9:f7:3e:25
I (619) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (619) NimBLE: 60:55:f9:f7:3e:25
I (629) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (639) NimBLE:

I (639) NimBLE: advertise complete; reason=0
I (1749) NimBLE: disconnect; reason=531
I (1749) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (1749) NimBLE: 60:55:f9:f7:3e:23
I (1749) NimBLE:  our_id_addr_type=0 our_id_addr=
I (1759) NimBLE: 60:55:f9:f7:3e:23
I (1759) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (1759) NimBLE: 60:55:f9:f7:3e:25
I (1769) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (1769) NimBLE: 60:55:f9:f7:3e:25
I (1779) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (1789) NimBLE:

I (1789) NimBLE: GAP procedure initiated: extended advertise; instance=0

I (1809) NimBLE: connection established; status=0
I (1809) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (1809) NimBLE: 60:55:f9:f7:3e:23
I (1819) NimBLE:  our_id_addr_type=0 our_id_addr=
I (1819) NimBLE: 60:55:f9:f7:3e:23
I (1819) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (1829) NimBLE: 60:55:f9:f7:3e:25
I (1829) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (1839) NimBLE: 60:55:f9:f7:3e:25
I (1839) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (1849) NimBLE:

I (1859) NimBLE: advertise complete; reason=0
I (3009) NimBLE: disconnect; reason=531
I (3009) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (3009) NimBLE: 60:55:f9:f7:3e:23
I (3009) NimBLE:  our_id_addr_type=0 our_id_addr=
I (3019) NimBLE: 60:55:f9:f7:3e:23
I (3019) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (3029) NimBLE: 60:55:f9:f7:3e:25
I (3029) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (3039) NimBLE: 60:55:f9:f7:3e:25
I (3039) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (3049) NimBLE:

I (3059) NimBLE: GAP procedure initiated: extended advertise; instance=0

I (3079) NimBLE: connection established; status=0
I (3079) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (3079) NimBLE: 60:55:f9:f7:3e:23
I (3079) NimBLE:  our_id_addr_type=0 our_id_addr=
I (3089) NimBLE: 60:55:f9:f7:3e:23
I (3089) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (3099) NimBLE: 60:55:f9:f7:3e:25
I (3099) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (3109) NimBLE: 60:55:f9:f7:3e:25
I (3109) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (3119) NimBLE:

I (3129) NimBLE: advertise complete; reason=0
I (4379) NimBLE: disconnect; reason=531
I (4379) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (4379) NimBLE: 60:55:f9:f7:3e:23
I (4379) NimBLE:  our_id_addr_type=0 our_id_addr=
I (4389) NimBLE: 60:55:f9:f7:3e:23
I (4389) NimBLE:  peer_ota_addr_type=0 peer_ota_addr=
I (4399) NimBLE: 60:55:f9:f7:3e:25
I (4399) NimBLE:  peer_id_addr_type=0 peer_id_addr=
I (4409) NimBLE: 60:55:f9:f7:3e:25
I (4409) NimBLE:  conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0

I (7872) NimBLE:

Accepting connection directly on 2M / Coded Phy

The example can also be configured to advertise directly on 2M auxiliary PHY or CODED PHY

In main.c file, in bleprph_on_sync function, set the s_current_phy to the intended phy.

To advertise, using 2M Phy, set the s_current_phy to BLE_HCI_LE_PHY_2M_PREF_MASK.

To advertise, using Coded Phy, set the s_current_phy to BLE_HCI_LE_PHY_CODED_PREF_MASK

Recompile application and flash the image.

Since , in this mode, the central will initiate direct connection, ensure that peripheral is already advertising, before initiating connection from the central device.

Troubleshooting

For any technical queries, please open an issue on GitHub. We will get back to you soon.