This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit 83955c8dbee8b5fe4037849bfc304f902a66fa4b
Author: Matteo Golin <[email protected]>
AuthorDate: Mon Sep 21 16:22:16 2026 -0400

    examples/nimble: Use ble_hs_adv_fields instead
    
    This removes the less readable way of setting advertising parameters and
    replaces it with the more readable `ble_gap_adv_set_fields` struct. This
    also resolves the bug where the value in `g_gap_name` appeared truncated
    on devices discovering the advertisement. Now the full string appears in
    discovery.
    
    Signed-off-by: Matteo Golin <[email protected]>
---
 examples/nimble/nimble_main.c | 54 +++++++++++++++----------------------------
 1 file changed, 18 insertions(+), 36 deletions(-)

diff --git a/examples/nimble/nimble_main.c b/examples/nimble/nimble_main.c
index 68eb2f051..6658f6221 100644
--- a/examples/nimble/nimble_main.c
+++ b/examples/nimble/nimble_main.c
@@ -73,9 +73,6 @@ void ble_hci_sock_set_device(int dev);
  * Private Functions Prototypes
  ****************************************************************************/
 
-static void put_ad(uint8_t ad_type, uint8_t ad_len, FAR const void *ad,
-                   FAR uint8_t *buf, FAR uint8_t *len);
-static void update_ad(void);
 static void start_advertise(void);
 static int gap_event_cb(FAR struct ble_gap_event *event, FAR void *arg);
 static void app_ble_sync_cb(void);
@@ -94,38 +91,6 @@ static uint8_t g_own_addr_type;
  * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: put_ad
- ****************************************************************************/
-
-static void put_ad(uint8_t ad_type, uint8_t ad_len, FAR const void *ad,
-                   FAR uint8_t *buf, FAR uint8_t *len)
-{
-  buf[(*len)++] = ad_len + 1;
-  buf[(*len)++] = ad_type;
-
-  memcpy(&buf[*len], ad, ad_len);
-
-  *len += ad_len;
-}
-
-/****************************************************************************
- * Name: update_ad
- ****************************************************************************/
-
-static void update_ad(void)
-{
-  uint8_t ad_flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
-  uint8_t ad_len   = 0;
-  uint8_t ad[BLE_HS_ADV_MAX_SZ];
-
-  put_ad(BLE_HS_ADV_TYPE_FLAGS, 1, &ad_flags, ad, &ad_len);
-  put_ad(BLE_HS_ADV_TYPE_COMP_NAME, sizeof(g_gap_name), g_gap_name,
-         ad, &ad_len);
-
-  ble_gap_adv_set_data(ad, ad_len);
-}
-
 /****************************************************************************
  * Name: start_advertise
  ****************************************************************************/
@@ -133,15 +98,32 @@ static void update_ad(void)
 static void start_advertise(void)
 {
   struct ble_gap_adv_params advp;
+  struct ble_hs_adv_fields  adv_fields;
   int                       rc;
 
   printf("advertise\n");
 
-  update_ad();
+  /* Set advertisement parameters */
 
   memset(&advp, 0, sizeof advp);
   advp.conn_mode = BLE_GAP_CONN_MODE_UND;
   advp.disc_mode = BLE_GAP_DISC_MODE_GEN;
+
+  /* Populate fields for advertising data */
+
+  memset(&adv_fields, 0, sizeof(adv_fields));
+  adv_fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP,
+
+  adv_fields.name = (uint8_t *)g_gap_name;
+  adv_fields.name_len = strlen(g_gap_name);
+  adv_fields.name_is_complete = 1;
+
+  adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+  adv_fields.tx_pwr_lvl_is_present = 1;
+
+  rc = ble_gap_adv_set_fields(&adv_fields);
+  assert(rc == 0);
+
   rc = ble_gap_adv_start(g_own_addr_type, NULL, BLE_HS_FOREVER,
                          &advp, gap_event_cb, NULL);
   assert(rc == 0);

Reply via email to