diff --git a/README.md b/README.md index 1a514d4..4fe436d 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,8 @@ You should have updated your `PATH` environment variable to add the toolchain programs. 4. Clone this repository and enter the created directory. 5. Pull the submodules: `git submodule init && git submodule update` -6. Run `make menuconfig` to change your BB-8 bluetooth MAC address -7. Compile: `make` -8. Flash your odroid-go: `make flash` +6. Compile: `make` +7. Flash your odroid-go: `make flash` ## Licence diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index d4b9823..3e115c7 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -1,8 +1,18 @@ menu "BB-8 remote control" +config STATIC_MAC + bool "Set a static MAC address" + default n + help + Connect based on a given MAC address to the Sphero BB-8 toy. + + Otherwise, it will try to connect to any BLE device with a name + starting with "BB-" + config BB8_MAC string "BB-8 Bluetooth MAC address" default "e5:12:88:32:81:75" + depends on STATIC_MAC help Bluetooth MAC address of the Spero BB-8 toy to be controlled. diff --git a/main/bb8.c b/main/bb8.c index 1d9278d..141e083 100644 --- a/main/bb8.c +++ b/main/bb8.c @@ -35,8 +35,6 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param); -static esp_bd_addr_t bb8_mac; - static bool Isconnecting = false; static esp_ble_scan_params_t ble_scan_params = { @@ -162,6 +160,9 @@ static void DisplaySpeed() #endif +#if CONFIG_STATIC_MAC +static esp_bd_addr_t bb8_mac; + static void init_mac() { int values[6]; @@ -180,6 +181,34 @@ static void init_mac() ESP_LOGE(GATTC_TAG, "Invalid MAC Address!"); } } +#endif + +uint8_t check_if_bb8(struct ble_scan_result_evt_param scan_rst) +{ + +#if CONFIG_STATIC_MAC + if (memcmp(scan_rst.bda, bb8_mac, ESP_BD_ADDR_LEN) == 0) + return 1; + else + return 0; + +#else + + uint8_t adv_name_len = 0; + uint8_t *adv_name; + + adv_name = esp_ble_resolve_adv_data(scan_rst.ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len); + ESP_LOGI(GATTC_TAG, "adv_name_len: %d", adv_name_len); + esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len); + if (adv_name_len > 3) + if ((adv_name[0] == 'B') && (adv_name[1] == 'B') && (adv_name[2] == '-')) + return 1; + + return 0; + +#endif + +} static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, @@ -314,18 +343,22 @@ static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * pa case ESP_GAP_BLE_SCAN_RESULT_EVT: ESP_LOGI(GATTC_TAG, "SCAN_RESULTE_EVT"); esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *) param; - esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); - if (memcmp(scan_result->scan_rst.bda, bb8_mac, ESP_BD_ADDR_LEN) == 0) { - ESP_LOGI(GATTC_TAG, "BB8 !!!"); - if (Isconnecting) { - break; - } - Isconnecting = true; - esp_ble_gap_stop_scanning(); - esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, - scan_result->scan_rst.ble_addr_type, true); + if (scan_result->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT) { + esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, ESP_BD_ADDR_LEN); + + if (check_if_bb8(scan_result->scan_rst)) { + ESP_LOGI(GATTC_TAG, "BB8 !!!"); + if (Isconnecting) { + break; + } + Isconnecting = true; + esp_ble_gap_stop_scanning(); + esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, + scan_result->scan_rst.ble_addr_type, true); + } } + break; case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT: @@ -403,7 +436,7 @@ void send_command(uint8_t did, uint8_t cid, uint8_t data_length, uint8_t * data) ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE); if (write != ESP_GATT_OK) - ESP_LOGE(GATTC_TAG, "Error writing antidos"); + ESP_LOGE(GATTC_TAG, "Error writing command"); free(packet); seq++; } @@ -564,8 +597,9 @@ void test_input() void app_main() { - +#if CONFIG_STATIC_MAC init_mac(); +#endif input_init(); diff --git a/sdkconfig b/sdkconfig index 96e695b..910ad80 100644 --- a/sdkconfig +++ b/sdkconfig @@ -79,7 +79,7 @@ CONFIG_MONITOR_BAUD=115200 # # BB-8 remote control # -CONFIG_BB8_MAC="e5:12:88:32:81:75" +CONFIG_STATIC_MAC= CONFIG_BB8_GUI=y #