From 378ba5100370f65f096a8ab46dd9a590798d17cf Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Tue, 12 Nov 2024 13:02:10 +0800 Subject: [PATCH] show the last 2 byte of mac in device name --- EPD/EPD_ble.h | 2 +- main.c | 50 ++++++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/EPD/EPD_ble.h b/EPD/EPD_ble.h index 43ac4c3..31ed989 100644 --- a/EPD/EPD_ble.h +++ b/EPD/EPD_ble.h @@ -19,7 +19,7 @@ #include #include "DEV_Config.h" -#define BLE_EPD_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */ +#define BLE_EPD_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer. */ enum EPD_CMDS { diff --git a/main.c b/main.c index 559aed8..bcc4415 100644 --- a/main.c +++ b/main.c @@ -9,14 +9,6 @@ * the file. * */ -/** @example examples/ble_peripheral/ble_app_hrs/main.c - * - * @brief Heart Rate Service Sample Application main file. - * - * This file contains the source code for a sample application using the Heart Rate service - * (and also Battery and Device Information services). This application uses the - * @ref srvlib_conn_params module. - */ #include #include @@ -34,31 +26,27 @@ #include "app_timer.h" #include "EPD_ble.h" -#define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/ +#define IS_SRVC_CHANGED_CHARACT_PRESENT 1 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/ -#define DEVICE_NAME "NRF_EPD_420" /**< Name of device. Will be included in the advertising data. */ -#define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */ - -#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */ -#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */ -#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ -#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */ +#define DEVICE_NAME "NRF_EPD" /**< Name of device. Will be included in the advertising data. */ +#define APP_ADV_INTERVAL 300 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */ +#define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */ +#define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */ +#define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */ #define MIN_CONN_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS) /**< Minimum connection interval (7.5 ms) */ #define MAX_CONN_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) /**< Maximum connection interval (30 ms). */ #define SLAVE_LATENCY 6 /**< Slave latency. */ #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(430, UNIT_10_MS) /**< Connection supervisory timeout (430 ms). */ -#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */ -#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER)/**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */ -#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ +#define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */ +#define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */ +#define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */ -#define BLE_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer. */ +#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ -#define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */ - -static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ -static ble_epd_t m_epd; /**< Structure to identify the EPD Service. */ +static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */ +static ble_epd_t m_epd; /**< Structure to identify the EPD Service. */ /**@brief Callback function for asserts in the SoftDevice. * @@ -105,14 +93,24 @@ static void services_init(void) static void gap_params_init(void) { uint32_t err_code; + char device_name[20]; + ble_gap_addr_t addr; ble_gap_conn_params_t gap_conn_params; ble_gap_conn_sec_mode_t sec_mode; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); + + err_code = sd_ble_gap_address_get(&addr); + APP_ERROR_CHECK(err_code); + NRF_LOG_PRINTF("Bluetooth MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n", + addr.addr[5], addr.addr[4], addr.addr[3], + addr.addr[2], addr.addr[1], addr.addr[0]); + + snprintf(device_name, 20, "%s_%02X%02X", DEVICE_NAME, addr.addr[1],addr.addr[0]); err_code = sd_ble_gap_device_name_set(&sec_mode, - (const uint8_t *)DEVICE_NAME, - strlen(DEVICE_NAME)); + (const uint8_t *)device_name, + strlen(device_name)); APP_ERROR_CHECK(err_code); memset(&gap_conn_params, 0, sizeof(gap_conn_params));