Skip to content

Commit d5d06c6

Browse files
committed
bt: code refactoring
1 parent 6a13a3b commit d5d06c6

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

main/inc/chip/bt.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
#ifndef INC_CHIP_BT_H_
99
#define INC_CHIP_BT_H_
1010

11-
extern const char *bt_dev_address;
11+
#include <stdint.h>
12+
13+
extern char *bt_get_mac_string(void);
14+
extern char *ble_get_mac_string(void);
15+
16+
extern uint8_t *bt_get_mac_address(void);
17+
extern uint8_t *ble_get_mac_address(void);
1218

1319
extern void bt_init(void);
1420

main/src/chip/bt.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,40 @@
55
* Author: Jack Chen <[email protected]>
66
*/
77

8+
#include <string.h>
9+
810
#include "esp_bt.h"
911
#include "esp_log.h"
1012
#include "esp_bt_main.h"
1113
#include "esp_bt_device.h"
1214

1315
#define TAG "bt"
1416

15-
const char *bt_dev_address = NULL;
17+
static char bt_mac_string[18] = {0};
18+
static char ble_mac_string[18] = {0};
19+
20+
static uint8_t bt_mac_address[6] = {0};
21+
static uint8_t ble_mac_address[6] = {0};
22+
23+
char *bt_get_mac_string(void)
24+
{
25+
return bt_mac_string;
26+
}
27+
28+
char *ble_get_mac_string(void)
29+
{
30+
return ble_mac_string;
31+
}
32+
33+
uint8_t *bt_get_mac_address(void)
34+
{
35+
return bt_mac_address;
36+
}
37+
38+
uint8_t *ble_get_mac_address(void)
39+
{
40+
return ble_mac_address;
41+
}
1642

1743
void bt_init(void)
1844
{
@@ -26,7 +52,13 @@ void bt_init(void)
2652
ESP_ERROR_CHECK(esp_bluedroid_init());
2753
ESP_ERROR_CHECK(esp_bluedroid_enable());
2854

29-
bt_dev_address = (const char *)esp_bt_dev_get_address();
55+
memcpy(bt_mac_address, esp_bt_dev_get_address(), sizeof(bt_mac_address));
56+
memcpy(ble_mac_address, esp_bt_dev_get_address(), sizeof(ble_mac_address));
57+
58+
ble_mac_address[0] |= 0xC0;
59+
60+
snprintf(bt_mac_string, sizeof(bt_mac_string), MACSTR, MAC2STR(bt_mac_address));
61+
snprintf(ble_mac_string, sizeof(ble_mac_string), MACSTR, MAC2STR(ble_mac_address));
3062

3163
ESP_LOGI(TAG, "initialized, bt: 1, ble: %d",
3264
#ifdef CONFIG_ENABLE_BLE_CONTROL_IF

main/src/user/ble_app.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "esp_gatt_common_api.h"
1515

1616
#include "core/os.h"
17+
#include "chip/bt.h"
18+
1719
#include "user/ble_gatts.h"
1820

1921
#define BLE_APP_TAG "ble_app"
@@ -23,7 +25,7 @@ static esp_ble_adv_params_t adv_params = {
2325
.adv_int_min = 0x20,
2426
.adv_int_max = 0x40,
2527
.adv_type = ADV_TYPE_IND,
26-
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
28+
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
2729
.channel_map = ADV_CHNL_ALL,
2830
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY
2931
};
@@ -66,6 +68,7 @@ static void ble_gap_config_adv_data(const char *name)
6668
// adv name
6769
raw_adv_data[3] = len + 1;
6870
raw_adv_data[4] = ESP_BLE_AD_TYPE_NAME_CMPL;
71+
6972
memcpy(raw_adv_data + 5, name, len);
7073

7174
esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data));
@@ -84,6 +87,7 @@ void ble_app_init(void)
8487
xEventGroupSetBits(user_event_group, BLE_GATTS_IDLE_BIT);
8588

8689
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ble_gap_event_handler));
90+
ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(ble_get_mac_address()));
8791
ESP_ERROR_CHECK(esp_ble_gap_set_device_name(CONFIG_BT_NAME));
8892

8993
ESP_ERROR_CHECK(esp_ble_gatts_register_callback(ble_gatts_event_handler));

main/src/user/bt_av.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
133133
a2d_sample_rate = 16000;
134134
}
135135

136-
ESP_LOGI(BT_A2D_TAG, "A2DP audio player configuration, sample rate: %d", a2d_sample_rate);
136+
ESP_LOGI(BT_A2D_TAG, "A2DP audio player configuration, sample rate: %d Hz", a2d_sample_rate);
137137
}
138138

139139
break;

0 commit comments

Comments
 (0)