Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions subsys/bluetooth/host/classic/br.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions subsys/bluetooth/host/classic/br.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2025 Xiaomi Corporation
* Copyright (c) 2017-2021 Nordic Semiconductor ASA
* Copyright (c) 2015-2016 Intel Corporation
*
Expand All @@ -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);
12 changes: 12 additions & 0 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down