Classic BT previously bonded connection error

jeeper1974
Posts: 11
Joined: Thu Aug 13, 2020 5:47 am

Classic BT previously bonded connection error

Postby jeeper1974 » Thu Aug 13, 2020 6:14 am

I'm working with a ESP32-LyraT-Mini-V1.2 using the esp-idf release/v4.0 and have an issue with the bluetooth that I hope I can get some help with.

I have the ESP32 setup as an A2DP source that plays an MP3 to some headphones. If I place the headphones in pairing mode they connect to the ESP32 perfectly and music plays fine from the ESP32. If I then reset the ESP32 and allow it to connect to the headphones not in pairing mode but how a previously bonded device would just connect, it does connect, but never cancels discovery mode and there are a constant stream of buffer errors while the music plays sporadically.

It looks like when the headphones connect through the pairing mode that the program layer gets notified, cancels discovery, and everything works. When the headphones just connects (because I assume the bonded key is stores in the BT hardware and the BT just connects) it does not appear to notify the program.
Thanks.
Here is a log of the successful pairing
I (2427) BLUETOOTH_SOURCE_EXAMPLE: --RSSI: -51
I (2432) BLUETOOTH_SOURCE_EXAMPLE: --Name: IFROGZ FreeRein Wireless
I (2439) BLUETOOTH_SOURCE_EXAMPLE: need device name IFROGZ FreeRein Wireless
I (2447) BLUETOOTH_SOURCE_EXAMPLE: Found a target device, address 00:00:00:00:3e:89, name IFROGZ FreeRein Wireless
I (2458) BLUETOOTH_SOURCE_EXAMPLE: Cancel device discovery ...
I (2466) BLUETOOTH_SOURCE_EXAMPLE: Device discovery stopped.
I (2471) BLUETOOTH_SOURCE_EXAMPLE: a2dp connecting to peer: IFROGZ FreeRein Wireless

E (2480) BT_APPL: reset flags
E (3497) BT_BTM: tBTM_SEC_DEV:0x3ffc1bfc rs_disc_pending=0
E (3577) BT_APPL: bta_av_rc_create ACP handle exist for shdl:0
W (3807) BT_APPL: new conn_srvc id:18, app_id:0
W (3812) BT_APPL: new conn_srvc id:18, app_id:1
W (6918) BT_APPL: bta_av_proc_meta_cmd unhandled RC vendor PDU: 0x30
W (31551) BT_BTC: btc_av_state_started_handler : unhandled event:BTC_AV_CONNECT_REQ_EVT
Here is a log of previously bonded connection but not in pairing mode when connected and its errors
I (40069) BLUETOOTH_SOURCE_EXAMPLE: Device discovery failed, continue to discover...
I (40071) BLUETOOTH_SOURCE_EXAMPLE: Discovery started.
E (41686) BT_APPL: bta_av_rc_create ACP handle exist for shdl:0
W (42544) BT_APPL: new conn_srvc id:18, app_id:0
W (42549) BT_APPL: new conn_srvc id:18, app_id:1

W (42621) BT_APPL: bta_av_proc_meta_cmd unhandled RC vendor PDU: 0x30
E (43333) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43361) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 11
E (43428) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43453) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43483) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43513) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43543) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43634) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43663) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43693) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12
E (43728) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 13
E (43753) BT_APPL: ERROR btc_media_aa_prep_sbc_2_send no buffer TxCnt 12

Here is the main code. I've taken most of the code from the esp-idf a2dp_source example

Code: Select all

audio_pipeline_handle_t pipeline;
    audio_element_handle_t fatfs_stream_reader, bt_stream_writer, mp3_decoder;

    esp_log_level_set("*", ESP_LOG_WARN);
    esp_log_level_set(TAG, ESP_LOG_INFO);

    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
        // NVS partition was truncated and needs to be erased
        // Retry nvs_flash_init
    	ESP_LOGI(TAG, "[ A ] Start Erase NVS");
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }


    ESP_LOGI(TAG, "[ 2.1 ] Start codec chip");
     //   audio_board_handle_t board_handle = audio_board_init();
     //   audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);

    ESP_LOGI(TAG, "[ 2.0 ] Mount sdcard");
    // Initialize peripherals management
    esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
    esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);

    // Initialize SD Card peripheral
    audio_board_sdcard_init(set);



    ESP_LOGI(TAG, "[3.0] Create audio pipeline for playback");
    audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
    pipeline = audio_pipeline_init(&pipeline_cfg);
    mem_assert(pipeline);

    ESP_LOGI(TAG, "[3.1] Create fatfs stream to read data from sdcard");
    fatfs_stream_cfg_t fatfs_cfg = FATFS_STREAM_CFG_DEFAULT();
    fatfs_cfg.type = AUDIO_STREAM_READER;
    fatfs_stream_reader = fatfs_stream_init(&fatfs_cfg);

    ESP_LOGI(TAG, "[3.2] Create mp3 decoder to decode mp3 file");
    mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
    mp3_decoder = mp3_decoder_init(&mp3_cfg);

    //Bluetooth
       ESP_LOGI(TAG, "[1.0] Create Bluetooth stream");

           esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
          // esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;//Roger Comment
          // esp_bt_pin_code_t pin_code = {'1', '2', '3', '4'}; //Roger Comment
           ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
           ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
           ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT));
           ESP_ERROR_CHECK(esp_bluedroid_init());
           ESP_ERROR_CHECK(esp_bluedroid_enable());
           const char *remote_name = NULL;
   			remote_name = CONFIG_BT_REMOTE_NAME;
   			if (remote_name) {
   				memcpy(&remote_bt_device_name, remote_name, strlen(remote_name) + 1);
   			} else {
   				memcpy(&remote_bt_device_name, "ESP_SINK_STREAM_DEMO", ESP_BT_GAP_MAX_BDNAME_LEN);
   			}
           //Roger Add
           /* create application task */
              bt_app_task_start_up();

              /* Bluetooth device name, connection mode and profile set up */
              bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);

          #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_IO;//ESP_BT_IO_CAP_IO
              esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
          #endif

              /*
               * 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_FIXED;
              esp_bt_pin_code_t pin_code = {'0', '0', '0', '0'};
              esp_bt_gap_set_pin(pin_type, 4, pin_code);
           //End Roger Add
              //Roger Comment
   //        esp_bt_dev_set_device_name("ESP_SOURCE_STREAM_DEMO");
   //        esp_bt_gap_set_pin(pin_type, 4, pin_code);
   //        esp_bt_gap_register_callback(bt_app_gap_cb);
   //
   //
   //
   //        esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
   //        esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_LIMITED_DISCOVERABLE);//ESP_BT_GENERAL_DISCOVERABLE
           //End Roger Comment

           a2dp_stream_config_t a2dp_config = {
                      .type = AUDIO_STREAM_WRITER,
                      .user_callback = {0},
                  };
                  bt_stream_writer = a2dp_stream_init(&a2dp_config);
           //End BlueTooth

    ESP_LOGI(TAG, "[3.4] Register all elements to audio pipeline");
    audio_pipeline_register(pipeline, fatfs_stream_reader, "file");
    audio_pipeline_register(pipeline, mp3_decoder, "mp3");
    audio_pipeline_register(pipeline, bt_stream_writer, "bt");

    ESP_LOGI(TAG, "[3.5] Link it together [sdcard]-->fatfs_stream-->mp3_decoder-->bt_stream-->[bt sink]");
    const char *link_tag[3] = {"file", "mp3", "bt"};
    audio_pipeline_link(pipeline, &link_tag[0], 3);

    ESP_LOGI(TAG, "[3.6] Set up  uri (file as fatfs_stream, mp3 as mp3 decoder, and default output is i2s)");
    audio_element_set_uri(fatfs_stream_reader, "/sdcard/hands.mp3");

    ESP_LOGI(TAG, "[3.7] Create bt peripheral");
    esp_periph_handle_t bt_periph = bt_create_periph();

    ESP_LOGI(TAG, "[3.8] Start bt peripheral");
    esp_periph_start(set, bt_periph);

    ESP_LOGI(TAG, "[ 4 ] Set up  event listener");
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);

    ESP_LOGI(TAG, "[4.1] Listening event from all elements of pipeline");
    audio_pipeline_set_listener(pipeline, evt);

    ESP_LOGI(TAG, "[4.2] Listening event from peripherals");
    audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);

    ESP_LOGI(TAG, "[ 5 ] Start audio_pipeline");
    audio_pipeline_run(pipeline);

    ESP_LOGI(TAG, "[ 6 ] Listen for all pipeline events");
    while (1) {
        audio_event_iface_msg_t msg;
        esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
            continue;
        }

        if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT
            && msg.source == (void *) mp3_decoder
            && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
            audio_element_info_t music_info = {0};
            audio_element_getinfo(mp3_decoder, &music_info);

            ESP_LOGI(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
                     music_info.sample_rates, music_info.bits, music_info.channels);
            continue;
        }
        if (msg.source_type == PERIPH_ID_BLUETOOTH
                && msg.source == (void *)bt_periph) {
            if ((msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) || (msg.cmd == PERIPH_BLUETOOTH_AUDIO_SUSPENDED)) {
                ESP_LOGW(TAG, "[ * ] Bluetooth disconnected or suspended");
                periph_bt_stop(bt_periph);
                break;
            }
        }
    }

    ESP_LOGI(TAG, "[ 7 ] Stop audio_pipeline");
    audio_pipeline_stop(pipeline);
    audio_pipeline_wait_for_stop(pipeline);
    audio_pipeline_terminate(pipeline);

    /* Terminal the pipeline before removing the listener */
    audio_pipeline_remove_listener(pipeline);

    /* Stop all periph before removing the listener */
    esp_periph_set_stop_all(set);
    audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt);

    /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
    audio_event_iface_destroy(evt);

    /* Release all resources */
    ESP_LOGI(TAG, "[ 7.1 ] Unregister audio_pipeline");
    audio_pipeline_unregister(pipeline, bt_stream_writer);
    audio_pipeline_unregister(pipeline, fatfs_stream_reader);
    audio_pipeline_unregister(pipeline, mp3_decoder);
    ESP_LOGI(TAG, "[ 7.2 ] Deinit audio_pipeline");
    audio_pipeline_deinit(pipeline);
    ESP_LOGI(TAG, "[ 7.3 ] Deinit Stream Writers");
    audio_element_deinit(bt_stream_writer);
    audio_element_deinit(fatfs_stream_reader);
    audio_element_deinit(mp3_decoder);
    ESP_LOGI(TAG, "[ 7.4 ] Destroy Set");
    esp_periph_set_destroy(set);
    ESP_LOGI(TAG, "[ 7.5 ] A2DP Destroy");
    a2dp_destroy();
    ESP_LOGI(TAG, "[ 7.6 ] Disable bluedroid");
    esp_bluedroid_disable();
    esp_bluedroid_deinit();
    ESP_LOGI(TAG, "[ 7.7 ] Disable BT Controller");
    esp_bt_controller_disable();
    esp_bt_controller_deinit();
    ESP_LOGI(TAG, "[ 7.8 ] Release BT Mem");
    esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
}

jeeper1974
Posts: 11
Joined: Thu Aug 13, 2020 5:47 am

Re: Classic BT previously bonded connection error

Postby jeeper1974 » Sat Aug 15, 2020 7:08 am

Maybe someone could help point me in the right direction with the callback functions for bluetooth. Using the example esp-idf a2dp_source I have the same issue. When the headphones are in pairing mode then bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) gets called and state gets changed from discovery to connected.

When the headphones have already been bonded, and then connects, this function never gets called and the device continues in discovery mode.

Where does the callback functions get called from? How do they get triggered?

Who is online

Users browsing this forum: andy_wharton_uk and 90 guests