espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.51k stars 7.26k forks source link

Alternative of SerialBT.connect() in idf (IDFGH-8896) #10316

Closed beginner140BB closed 1 year ago

beginner140BB commented 1 year ago

Is your feature request related to a problem?

in arduino there is function for connecting to slave device in classic bluetooth but in idf plform there is a example code of initiator available for connecting to slave device whether there is any simple way as in arduino for connecting to slave device to make bluetooth connection. if anyone has any example code for this please share.Hence the initiator code works base on two callabacks GAP and SPP we need to connect to slave device only when required.

Describe the solution you'd like.

No response

Describe alternatives you've considered.

No response

Additional context.

No response

beginner140BB commented 1 year ago

E (48778) BT_BTC: btc_spp_write unable to find RFCOMM slot!

is comming when trying to connect in first case functions used.

void start_bluetooth(char *BTname ) { esp_err_t ret = ESP_OK;

memset(EXAMPLE_DEVICE_NAME,0,sizeof(EXAMPLE_DEVICE_NAME));
strcpy(EXAMPLE_DEVICE_NAME,BTname);

ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
    ESP_LOGE(SPP_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}

if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
    ESP_LOGE(SPP_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}

if ((ret = esp_bluedroid_init()) != ESP_OK) {
    ESP_LOGE(SPP_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}

if ((ret = esp_bluedroid_enable()) != ESP_OK) {
    ESP_LOGE(SPP_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
    return;
}

esp_cb_reg();
vTaskDelay(100);

ifdef DEBUG_MODE

      printf("start_bluetooth\r\n");
    #endif

}

void esp_cb_reg() {

ifdef DEBUG_MODE

        printf("cGet_bt_status\r\n");

endif

        esp_err_t ret = ESP_OK;
        char bda_str[18] = {0};
            if(!_spp_event_group){
                    _spp_event_group = xEventGroupCreate();
                    if(!_spp_event_group){
                            ESP_LOGI(SPP_TAG,"SPP Event Group Create Failed!");
                    }
            }
             //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK) {
                       ESP_LOGE(SPP_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
                       return;
                   }

               #if (CONFIG_BT_SSP_ENABLED == true)
                   /* Set default parameters for Secure Simple Pairing */
                   esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
                   esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IN;
                   esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
                   if (iocap == ESP_BT_IO_CAP_IN || iocap == ESP_BT_IO_CAP_IO) {
                     //  console_uart_init();
                       vTaskDelay(pdMS_TO_TICKS(500));
                   }
               #endif

                   if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) {
                                              ESP_LOGE(SPP_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
                                              return;
                                          }

                   if ((ret = esp_spp_init(esp_spp_mode)) != ESP_OK) {
                       ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
                       return;
                   }
                   /*
                    * Set default parameters for Legacy Pairing
                    * Use variable pin, input pin code when pairing
                    */
                   esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
                   esp_bt_pin_code_t pin_code;
                   esp_bt_gap_set_pin(pin_type, 0, pin_code);

                   ESP_LOGI(SPP_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

int BT_connect(char * name) { memset(remote_device_name,0,sizeof(remote_device_name)); strcpy(remote_device_name,name); if (esp_bt_gap_start_discovery(inq_mode, inq_len, inq_num_rsps) == ESP_OK) { return(waitForConnect(SCAN_TIMEOUT)); }

    return 0;

}

BT_connect(char * name) is used when trying to connect to slave device