|
21 | 21 | #include <zephyr/logging/log_ctrl.h>
|
22 | 22 | #include <ble_conn_params.h>
|
23 | 23 | #include <bluetooth/services/ble_mcumgr.h>
|
| 24 | +#include <zephyr/settings/settings.h> |
| 25 | +#include <settings/bluetooth_name.h> |
24 | 26 |
|
25 | 27 | LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);
|
26 | 28 |
|
@@ -175,6 +177,9 @@ static int ble_change_address(void)
|
175 | 177 | int main(void)
|
176 | 178 | {
|
177 | 179 | int err;
|
| 180 | + const char *custom_advertising_name; |
| 181 | + uint8_t custom_advertising_name_size; |
| 182 | + ble_gap_conn_sec_mode_t sec_mode = {0}; |
178 | 183 | struct ble_adv_config ble_adv_cfg = {
|
179 | 184 | .conn_cfg_tag = CONFIG_NRF_SDH_BLE_CONN_TAG,
|
180 | 185 | .evt_handler = ble_adv_evt_handler,
|
@@ -210,6 +215,13 @@ int main(void)
|
210 | 215 |
|
211 | 216 | LOG_INF("Bluetooth enabled");
|
212 | 217 |
|
| 218 | + err = settings_subsys_init(); |
| 219 | + |
| 220 | + if (err) { |
| 221 | + LOG_ERR("Failed to enable settings, err %d", err); |
| 222 | + } |
| 223 | + |
| 224 | + settings_load(); |
213 | 225 | err = ble_mcumgr_init();
|
214 | 226 |
|
215 | 227 | if (err) {
|
@@ -237,14 +249,38 @@ int main(void)
|
237 | 249 | return 0;
|
238 | 250 | }
|
239 | 251 |
|
| 252 | + custom_advertising_name = bluetooth_name_value_get(); |
| 253 | + custom_advertising_name_size = strlen(custom_advertising_name); |
| 254 | + |
| 255 | + if (custom_advertising_name_size > 0) { |
| 256 | + /* Change advertising name to one from application */ |
| 257 | + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); |
| 258 | + err = sd_ble_gap_device_name_set(&sec_mode, custom_advertising_name, |
| 259 | + custom_advertising_name_size); |
| 260 | + |
| 261 | + if (err) { |
| 262 | + LOG_ERR("Failed to change advertising name, err %d", err); |
| 263 | + return 0; |
| 264 | + } else { |
| 265 | + err = ble_adv_data_encode(&ble_adv_cfg.adv_data, ble_adv.enc_adv_data[0], |
| 266 | + &ble_adv.adv_data.adv_data.len); |
| 267 | + |
| 268 | + if (err) { |
| 269 | + LOG_ERR("Failed to update advertising data, err %d", err); |
| 270 | + return 0; |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + |
240 | 275 | err = ble_adv_start(&ble_adv, BLE_ADV_MODE_FAST);
|
241 | 276 |
|
242 | 277 | if (err) {
|
243 | 278 | LOG_ERR("Failed to start advertising, err %d", err);
|
244 | 279 | return 0;
|
245 | 280 | }
|
246 | 281 |
|
247 |
| - LOG_INF("Advertising as %s", CONFIG_BLE_ADV_NAME); |
| 282 | + LOG_INF("Advertising as %s", (custom_advertising_name_size > 0 ? custom_advertising_name : |
| 283 | + CONFIG_BLE_ADV_NAME)); |
248 | 284 |
|
249 | 285 | while (notification_sent == false && device_disconnected == false) {
|
250 | 286 | while (LOG_PROCESS()) {
|
|
0 commit comments