I am using nRF52833 on ncs to turn on the extended broadcast. After referring to the corresponding settings, I found that the transmit power setting does not take effect. Is the setting wrong? Or need other methods to set the transmit power?
The version I use is ncs v1.6.0
Below is part of my code:
in main.c:
#include <zephyr.h> #include <zephyr/types.h> #include <stddef.h> #include <sys/util.h> #include <bluetooth/hci.h> #include <bluetooth/hci_vs.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <bluetooth/bluetooth.h> #include <bluetooth/controller.h> #include <sys/byteorder.h> #include <bluetooth/hci_vs.h> #include <logging/log.h> LOG_MODULE_REGISTER(mt_adv, 3); static struct bt_le_ext_adv *adv_set; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_DATA_UUID128_SOME), BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA, 0x4C, 0x00, /* Apple. */ 0x02, 0x15, /* iBeacon. */ 0x01, 0x12, 0x23, 0x34, /* UUID-1. */ 0x45, 0x56, 0x67, 0x78, /* UUID-2. */ 0x89, 0x9A, 0xAB, 0xBC, /* UUID-3. */ 0xCD, 0xDE, 0xEF, 0xF0, /* UUID-4. */ 0x66,0x66, /* Major. */ 0x66,0x66, /* Minor. */ 0xC5), /* Calibrated RSSI @ 1m. */ BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, (sizeof(CONFIG_BT_DEVICE_NAME) - 1)) }; const static struct bt_le_adv_param param = BT_LE_ADV_PARAM_INIT( BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | BT_LE_ADV_OPT_CONNECTABLE, BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MIN_2, NULL); void get_tx_power(uint8_t handle_type, uint16_t handle, int8_t *tx_pwr_lvl) { struct bt_hci_cp_vs_read_tx_power_level *cp; struct bt_hci_rp_vs_read_tx_power_level *rp; struct net_buf *buf, *rsp = NULL; int err; *tx_pwr_lvl = 0xFF; buf = bt_hci_cmd_create(BT_HCI_OP_VS_READ_TX_POWER_LEVEL, sizeof(*cp)); if (!buf) { LOG_ERR("Unable to allocate command buffer\n"); return; } cp = net_buf_add(buf, sizeof(*cp)); cp->handle = sys_cpu_to_le16(handle); cp->handle_type = handle_type; err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_TX_POWER_LEVEL, buf, &rsp); if (err) { uint8_t reason = rsp ? ((struct bt_hci_rp_vs_read_tx_power_level *) rsp->data)->status : 0; LOG_ERR("Read Tx power err: %d reason 0x%02x\n", err, reason); return; } rp = (void *)rsp->data; *tx_pwr_lvl = rp->tx_power_level; net_buf_unref(rsp); } void set_tx_power(uint8_t handle_type, uint16_t handle, int8_t tx_pwr_lvl) { struct bt_hci_cp_vs_write_tx_power_level *cp; struct bt_hci_rp_vs_write_tx_power_level *rp; struct net_buf * buf, *rsp = NULL; int err; buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL, sizeof(*cp)); if (!buf) { LOG_INF("Unable to allocate command buffer\n"); return; } cp = net_buf_add(buf, sizeof(*cp)); cp->handle = sys_cpu_to_le16(handle); cp->handle_type = handle_type; cp->tx_power_level = tx_pwr_lvl; err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL, buf, &rsp); if (err) { uint8_t reason = rsp ? ((struct bt_hci_rp_vs_write_tx_power_level *)rsp->data)->status : 0; LOG_INF("Set Tx power err: %d reason 0x%02x\n", err, reason); return; } rp = (void *)rsp->data; LOG_INF("Actual Tx Power: %d\n", rp->selected_tx_power); net_buf_unref(rsp); } void main(void) { int err = bt_enable(NULL); if (err) { LOG_ERR("[%04d] Bluetooth init failed (err %d).\n", __LINE__, err); return; } LOG_INF("[%04d] Bluetooth initialized.", __LINE__); int tx_power = 0; set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV, 0, tx_power); err = bt_le_ext_adv_create(¶m, NULL, &adv_set); if (err) { LOG_ERR("[%04d] Create extended advertising set_id failed (err %d).\n", __LINE__, err); return; } err = bt_le_ext_adv_set_data(adv_set, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { LOG_ERR("[%04d] Failed to set advertising data (%d).\n", __LINE__, err); return; } err = bt_le_ext_adv_start(adv_set, NULL); if (err) { LOG_ERR("[%04d] Extended advertising failed to start (err %d).\n", __LINE__, err); return; } LOG_INF("[%04d] Extended advertising enable...", __LINE__); }
# Bluetooth. CONFIG_BT=y CONFIG_BT_DEBUG_LOG=n CONFIG_BT_BROADCASTER=y CONFIG_BT_PERIPHERAL=y CONFIG_BT_DEVICE_NAME="NCS-adv" CONFIG_BT_LL_SW_SPLIT=y CONFIG_BT_LL_SOFTDEVICE=n CONFIG_BT_CTLR=y CONFIG_BT_CTLR_CONN_RSSI=y CONFIG_BT_CTLR_ADVANCED_FEATURES=y CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL=y CONFIG_BT_EXT_ADV=y CONFIG_BT_PER_ADV=y CONFIG_BT_CTLR_ADV_EXT=y CONFIG_BT_CTLR_PHY_CODED=y