components/bt: Modify demo bt_discovery

This commit is contained in:
baohongde 2021-12-03 14:51:41 +08:00
parent 214d62b9ad
commit 55bd48b24e
2 changed files with 35 additions and 38 deletions

View File

@ -122,6 +122,10 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
char bda_str[18]; char bda_str[18];
uint32_t cod = 0; uint32_t cod = 0;
int32_t rssi = -129; /* invalid value */ int32_t rssi = -129; /* invalid value */
uint8_t *bdname = NULL;
uint8_t bdname_len = 0;
uint8_t *eir = NULL;
uint8_t eir_len = 0;
esp_bt_gap_dev_prop_t *p; esp_bt_gap_dev_prop_t *p;
ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, 18)); ESP_LOGI(GAP_TAG, "Device found: %s", bda2str(param->disc_res.bda, bda_str, 18));
@ -137,6 +141,15 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
ESP_LOGI(GAP_TAG, "--RSSI: %d", rssi); ESP_LOGI(GAP_TAG, "--RSSI: %d", rssi);
break; break;
case ESP_BT_GAP_DEV_PROP_BDNAME: case ESP_BT_GAP_DEV_PROP_BDNAME:
bdname_len = (p->len > ESP_BT_GAP_MAX_BDNAME_LEN) ? ESP_BT_GAP_MAX_BDNAME_LEN :
(uint8_t)p->len;
bdname = (uint8_t *)(p->val);
break;
case ESP_BT_GAP_DEV_PROP_EIR: {
eir_len = p->len;
eir = (uint8_t *)(p->val);
break;
}
default: default:
break; break;
} }
@ -144,7 +157,7 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
/* search for device with MAJOR service class as "rendering" in COD */ /* search for device with MAJOR service class as "rendering" in COD */
app_gap_cb_t *p_dev = &m_dev_info; app_gap_cb_t *p_dev = &m_dev_info;
if (p_dev->dev_found && 0 != memcmp(param->disc_res.bda, p_dev->bda, ESP_BD_ADDR_LEN)) { if (p_dev->dev_found) {
return; return;
} }
@ -156,40 +169,27 @@ static void update_device_info(esp_bt_gap_cb_param_t *param)
memcpy(p_dev->bda, param->disc_res.bda, ESP_BD_ADDR_LEN); memcpy(p_dev->bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
p_dev->dev_found = true; p_dev->dev_found = true;
for (int i = 0; i < param->disc_res.num_prop; i++) {
p = param->disc_res.prop + i; p_dev->cod = cod;
switch (p->type) { p_dev->rssi = rssi;
case ESP_BT_GAP_DEV_PROP_COD: if (bdname_len > 0) {
p_dev->cod = *(uint32_t *)(p->val); memcpy(p_dev->bdname, bdname, bdname_len);
break; p_dev->bdname[bdname_len] = '\0';
case ESP_BT_GAP_DEV_PROP_RSSI: p_dev->bdname_len = bdname_len;
p_dev->rssi = *(int8_t *)(p->val); }
break; if (eir_len > 0) {
case ESP_BT_GAP_DEV_PROP_BDNAME: { memcpy(p_dev->eir, eir, eir_len);
uint8_t len = (p->len > ESP_BT_GAP_MAX_BDNAME_LEN) ? ESP_BT_GAP_MAX_BDNAME_LEN : p_dev->eir_len = eir_len;
(uint8_t)p->len;
memcpy(p_dev->bdname, (uint8_t *)(p->val), len);
p_dev->bdname[len] = '\0';
p_dev->bdname_len = len;
break;
}
case ESP_BT_GAP_DEV_PROP_EIR: {
memcpy(p_dev->eir, (uint8_t *)(p->val), p->len);
p_dev->eir_len = p->len;
break;
}
default:
break;
}
} }
if (p_dev->eir && p_dev->bdname_len == 0) { if (p_dev->eir && p_dev->bdname_len == 0) {
get_name_from_eir(p_dev->eir, p_dev->bdname, &p_dev->bdname_len); get_name_from_eir(p_dev->eir, p_dev->bdname, &p_dev->bdname_len);
ESP_LOGI(GAP_TAG, "Found a target device, address %s, name %s", bda_str, p_dev->bdname);
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVER_COMPLETE;
ESP_LOGI(GAP_TAG, "Cancel device discovery ...");
esp_bt_gap_cancel_discovery();
} }
ESP_LOGI(GAP_TAG, "Found a target device, address %s, name %s", bda_str, p_dev->bdname);
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVER_COMPLETE;
ESP_LOGI(GAP_TAG, "Cancel device discovery ...");
esp_bt_gap_cancel_discovery();
} }
static void bt_app_gap_init(void) static void bt_app_gap_init(void)
@ -197,8 +197,7 @@ static void bt_app_gap_init(void)
app_gap_cb_t *p_dev = &m_dev_info; app_gap_cb_t *p_dev = &m_dev_info;
memset(p_dev, 0, sizeof(app_gap_cb_t)); memset(p_dev, 0, sizeof(app_gap_cb_t));
/* start to discover nearby Bluetooth devices */ p_dev->state = APP_GAP_STATE_IDLE;
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVERING;
} }
static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
@ -236,7 +235,6 @@ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
for (int i = 0; i < param->rmt_srvcs.num_uuids; i++) { for (int i = 0; i < param->rmt_srvcs.num_uuids; i++) {
esp_bt_uuid_t *u = param->rmt_srvcs.uuid_list + i; esp_bt_uuid_t *u = param->rmt_srvcs.uuid_list + i;
ESP_LOGI(GAP_TAG, "--%s", uuid2str(u, uuid_str, 37)); ESP_LOGI(GAP_TAG, "--%s", uuid2str(u, uuid_str, 37));
// ESP_LOGI(GAP_TAG, "--%d", u->len);
} }
} else { } else {
ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, 18)); ESP_LOGI(GAP_TAG, "Services for device %s not found", bda2str(p_dev->bda, bda_str, 18));
@ -267,6 +265,9 @@ static void bt_app_gap_start_up(void)
/* inititialize device information and status */ /* inititialize device information and status */
bt_app_gap_init(); bt_app_gap_init();
/* start to discover nearby Bluetooth devices */
app_gap_cb_t *p_dev = &m_dev_info;
p_dev->state = APP_GAP_STATE_DEVICE_DISCOVERING;
esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0); esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
} }

View File

@ -1,9 +1,5 @@
# Override some defaults so BT stack is enabled and # Enable Classic BT and disable BLE
# Classic BT is enabled and BT_DRAM_RELEASE is disabled
CONFIG_BT_ENABLED=y CONFIG_BT_ENABLED=y
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=n
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=y CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=y
CONFIG_BTDM_CTRL_MODE_BTDM=n
CONFIG_BT_CLASSIC_ENABLED=y CONFIG_BT_CLASSIC_ENABLED=y
CONFIG_BT_A2DP_ENABLE=n
CONFIG_BT_BLE_ENABLED=n CONFIG_BT_BLE_ENABLED=n