BLE Peripheral Tutorial
Overview
This tutorial covers the BLE Peripheral example: initialize as a peripheral, register advertising and scan-response payloads, advertise, accept a connection from a central (for example a phone running nRF Connect), and handle GATT-related events (read update, writes, subscribe/notify, connection parameter update, MTU). The example path is examples/ble/ble_peripher (folder name ble_peripher in the repo).
Prerequisites
Requirements
- Target that builds
examples/ble/ble_peripherwith BLE peripheral support. - A BLE central (phone with nRF Connect, USB dongle, or BLE Central tutorial firmware) to scan and connect.
- Serial log for event traces.
Steps
-
Open
examples/ble/ble_peripherin the TuyaOpen tree. -
Choose a BLE-capable board in
tos.py config choice. -
Build and flash:
cd examples/ble/ble_periphertos.py config choicetos.py build -
On boot,
tal_ble_bt_initruns withTAL_BLE_ROLE_PERIPERAL(this is the identifier used in the current SDK sources) and your event callback. -
On
TAL_BLE_STACK_INIT, wheninit == 0, the sample sets advertising and scan response withtal_ble_advertising_data_set, thental_ble_advertising_start(TUYAOS_BLE_DEFAULT_ADV_PARAM). The scan response includes a Complete Local Name ofTY(bytes0x09,'T','Y') so it is easy to find in nRF Connect. -
Connect from the central. Typical events in the sample:
TAL_BLE_EVT_PERIPHERAL_CONNECT(thental_ble_server_common_read_updatewith a combined buffer),TAL_BLE_EVT_CONN_PARAM_UPDATE,TAL_BLE_EVT_MTU_REQUEST,TAL_BLE_EVT_SUBSCRIBE(optional notifyNOTIFY ENABLED),TAL_BLE_EVT_WRITE_REQfor client writes,TAL_BLE_EVT_DISCONNECTfollowed bytal_ble_advertising_startagain.
Expected outcome: Device advertises as TY, connects, exchanges connection parameters and MTU, and can receive writes; after disconnect it advertises again.
Implementation notes
- Advertising payloads are 31-byte arrays in the example (
adv_data_const,scan_rsp_data_const). Production devices should follow Bluetooth SIG GAP rules for AD structures (length, type, value). - The peripheral tutorial complements the central tutorial: use two boards or a phone plus device for end-to-end tests.
- For API details and error handling patterns, see TAL Bluetooth API reference.
- Extra diagrams and log walkthroughs:
examples/ble/ble_peripher/README.md.
References
- Source:
examples/ble/ble_peripher/src/example_ble_peripheral.c - BLE Central tutorial
- TAL Bluetooth API reference
- Examples index