Page 1 of 1

Bluetooth Pairing with ESP32 Should Prompt for a Password Instead of Pairing Automatically

Posted: Tue Sep 03, 2024 3:12 pm
by nakruf
Hello,

I am working on an ESP32 Bluetooth project and I want the device to prompt for a password when connecting via Bluetooth. Currently, when I try to connect from a phone, it pairs automatically without asking for a password.

How can I configure the ESP32 so that it requests a password during the Bluetooth pairing process?

Thank you!
ESP-IDF v4.4.2
myCode

Code: Select all

esp_err_t alb_bluetoothInit(const char* device_name)
{
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) {
        ESP_LOGE(TAG, "initialize controller failed");
        return ret;
    }
    ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT);
    if (ret) {
        ESP_LOGE(TAG, "enable controller failed");
        return ret;
    }
    ret = esp_bluedroid_init();
    if (ret) {
        ESP_LOGE(TAG, "initialize bluedroid failed");
        return ret;
    }
    ret = esp_bluedroid_enable();
    if (ret) {
        ESP_LOGE(TAG, "enable bluedroid failed");
        return ret;
    }
    esp_bt_dev_set_device_name(device_name);
    esp_spp_register_callback(spp_event_handler);
    esp_spp_init(ESP_SPP_MODE_CB);

    data_queue = xQueueCreate(DATA_QUEUE_SIZE, sizeof(char*));
    if (data_queue == NULL) {
        ESP_LOGE(TAG, "Failed to create data queue");
        return ESP_FAIL;
    }

    data_len_queue = xQueueCreate(DATA_QUEUE_SIZE, sizeof(int));
    if (data_len_queue == NULL) {
        ESP_LOGE(TAG, "Failed to create data queue");
        return ESP_FAIL;
    }

    bt_register_event_handler(bt_event_callback);
    // GAP event handler'ı kaydet
    ret = esp_bt_gap_register_callback(gap_event_handler);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "GAP callback registration failed: %s", esp_err_to_name(ret));
    }
    // Bluetooth PIN kodu ayarı
    esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
    uint8_t pass[16] = {1,2,3,4};
    esp_bt_gap_set_pin(pin_type, BLUETOOTH_PIN_CODE_LEN, pas);
    esp_spp_start_srv(ESP_SPP_SEC_AUTHENTICATE, ESP_SPP_ROLE_MASTER, 0, "SPP_SERVER");
    printf("Bluetooth initialized successfully with name: %s\n", device_name);

    return ESP_OK;
}

Re: Bluetooth Pairing with ESP32 Should Prompt for a Password Instead of Pairing Automatically

Posted: Wed Sep 04, 2024 1:51 am
by chegewara