diff --git a/subsys/bluetooth/host/classic/br.c b/subsys/bluetooth/host/classic/br.c index 23a43a01959b3..53e4dcfe11a45 100644 --- a/subsys/bluetooth/host/classic/br.c +++ b/subsys/bluetooth/host/classic/br.c @@ -13,6 +13,7 @@ #include "common/bt_str.h" +#include "br.h" #include "host/hci_core.h" #include "host/conn_internal.h" #include "host/keys.h" @@ -805,7 +806,6 @@ int bt_br_init(void) struct net_buf *buf; struct bt_hci_cp_write_ssp_mode *ssp_cp; struct bt_hci_cp_write_inquiry_mode *inq_cp; - struct bt_hci_write_local_name *name_cp; struct bt_hci_rp_read_default_link_policy_settings *rp; struct net_buf *rsp; int err; @@ -858,19 +858,15 @@ int bt_br_init(void) } /* Set local name */ - buf = bt_hci_cmd_alloc(K_FOREVER); - if (!buf) { - return -ENOBUFS; - } - - name_cp = net_buf_add(buf, sizeof(*name_cp)); - strncpy((char *)name_cp->local_name, CONFIG_BT_DEVICE_NAME, sizeof(name_cp->local_name)); - - err = bt_hci_cmd_send_sync(BT_HCI_OP_WRITE_LOCAL_NAME, buf, NULL); + err = bt_br_write_local_name(CONFIG_BT_DEVICE_NAME); if (err) { return err; } +#if defined(CONFIG_BT_DEVICE_NAME_DYNAMIC) + strncpy(bt_dev.name, CONFIG_BT_DEVICE_NAME, CONFIG_BT_DEVICE_NAME_MAX); +#endif + /* Set Class of device */ buf = bt_hci_cmd_alloc(K_FOREVER); if (!buf) { @@ -1337,3 +1333,19 @@ int bt_br_unpair(const bt_addr_t *addr) return 0; } + +int bt_br_write_local_name(const char *name) +{ + struct net_buf *buf; + struct bt_hci_write_local_name *name_cp; + + buf = bt_hci_cmd_alloc(K_FOREVER); + if (!buf) { + return -ENOBUFS; + } + + name_cp = net_buf_add(buf, sizeof(*name_cp)); + strncpy((char *)name_cp->local_name, name, sizeof(name_cp->local_name)); + + return bt_hci_cmd_send_sync(BT_HCI_OP_WRITE_LOCAL_NAME, buf, NULL); +} diff --git a/subsys/bluetooth/host/classic/br.h b/subsys/bluetooth/host/classic/br.h index 80622aa637d4c..c326b20df56a9 100644 --- a/subsys/bluetooth/host/classic/br.h +++ b/subsys/bluetooth/host/classic/br.h @@ -1,4 +1,5 @@ /* + * Copyright (c) 2025 Xiaomi Corporation * Copyright (c) 2017-2021 Nordic Semiconductor ASA * Copyright (c) 2015-2016 Intel Corporation * @@ -10,3 +11,5 @@ int bt_br_init(void); void bt_br_discovery_reset(void); bool bt_br_update_sec_level(struct bt_conn *conn); + +int bt_br_write_local_name(const char *name); diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 33306596b2ce7..ac1ce14e51e39 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -4752,6 +4752,10 @@ int bt_set_name(const char *name) return -ENOMEM; } + if (len == 0) { + return -EINVAL; + } + if (!strcmp(bt_dev.name, name)) { return 0; } @@ -4766,6 +4770,14 @@ int bt_set_name(const char *name) } } +#if defined(CONFIG_BT_CLASSIC) + err = bt_br_write_local_name(bt_dev.name); + if (err) { + LOG_WRN("Unable to set local name"); + return err; + } +#endif + return 0; #else return -ENOMEM;