nRF52840 BLE Connection

I am trying to establish a BLE connection between two nRF52840 devices.

The child devices are properly advertising and are close to each other.

When attempting a BLE connection from the central device, if a match is found during a scan,
the connection is established using the following process.

In this process,

err_code2 = sd_ble_gap_connect(&addr, &scan_params, &conn_params, con_cfg_tag);
The function may or may not be executed.

I have confirmed that the condition parameters are correct.

What do you think the cause could be?

case NRF_BLE_SCAN_EVT_FILTER_MATCH:
        {
            // アドバタイジングレポートを取得
            ble_gap_evt_adv_report_t const * p_adv_report = p_scan_evt->params.filter_match.p_adv_report;

            // アドバタイジングデータを取得
            const uint8_t * p_data = p_adv_report->data.p_data;                                 // ble_data_t から p_data を取得
            uint16_t data_len = p_adv_report->data.len;                                         // ble_data_t からデータ長を取得

            // デバイス名の取得
            char device_name[BLE_GAP_DEVNAME_MAX_LEN + 1];                                      // デバイス名用のバッファ
            memset(device_name, 0, sizeof(device_name));                                        // バッファをクリア

            // アドバタイジングデータからデバイス名を探す
            uint8_t * p_name_data = ble_advdata_parse((uint8_t *)p_data, data_len, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME);
            if (p_name_data != NULL)
            {
                // デバイス名の長さを取得
                uint8_t name_len = strlen((const char *)p_name_data);
                strncpy(device_name, (char *)p_name_data, name_len);
                device_name[name_len] = '\0';                                                   // NULL で終端
                device_name[6] = '\0';
                // デバイス名が一致する場合、接続を開始
                if (strcmp(device_name, device_id) == 0)
                {

                    // 接続先デバイスのアドレスを設定
                    Name_acquisition_flag = 1;
//                    ble_gap_addr_t addr;
                    addr.addr_id_peer = 0;                                                      // プライベートアドレスでない場合は0
                    addr.addr_type = p_adv_report->peer_addr.addr_type;                         // アドレスのタイプを設定
                    memcpy(addr.addr, p_adv_report->peer_addr.addr, BLE_GAP_ADDR_LEN);          // アドレスをコピー

                    // スキャンパラメータの初期化
                    ble_gap_scan_params_t scan_params;
                    memset(&scan_params, 0, sizeof(scan_params));                               // ゼロ初期化
                    scan_params.extended = 0;                                                   // 拡張スキャンを使用しない場合は0
                    scan_params.active = 1;                                                     // アクティブスキャンを有効にする
                    scan_params.report_incomplete_evts = 0;                                     // 不完全なイベントを報告しない
                    scan_params.scan_phys = BLE_GAP_PHY_1MBPS;                                  // スキャンするPHYを設定
                    scan_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS);                   // スキャン間隔を設定
                    scan_params.window = MSEC_TO_UNITS(100, UNIT_0_625_MS);                     // スキャンウィンドウを設定
                    scan_params.timeout = 0; // タイムアウトなし

                    // 接続パラメータの初期化
                    ble_gap_conn_params_t conn_params;
                    memset(&conn_params, 0, sizeof(conn_params));                               // ゼロ初期化
                    conn_params.min_conn_interval = MIN_CONN_INTERVAL;                          // 最小接続間隔
                    conn_params.max_conn_interval = MAX_CONN_INTERVAL;                          // 最大接続間隔
                    conn_params.slave_latency = SLAVE_LATENCY;                                  // スレーブレイテンシ
                    conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;                            // 接続スーパーバイスタイムアウト
                    if (!is_connected)
                    {
                        nrf_delay_ms(200);
                        if(Ble_connect_count == 0 && Connect_flag == 1)
                        {
                            Connect_flag = 0;
                            nrf_gpio_pin_write(LED1,0);
                            nrf_delay_ms(125);
                            nrf_gpio_pin_write(LED1,1);
                            nrf_delay_ms(125);
                            nrf_gpio_pin_write(LED1,0);
                            nrf_delay_ms(125);
                            nrf_gpio_pin_write(LED1,1);
                            err_code2 = sd_ble_gap_connect(&addr, &scan_params, &conn_params, con_cfg_tag);
                            if(err_code2 == NRF_ERROR_NOT_FOUND)
                            {
                                BLE_Connect_flag = 0;
                                nrf_gpio_pin_write(LED1,0);
                                nrf_delay_ms(125);
                                nrf_gpio_pin_write(LED1,1);
                            }
                            else if(err_code2 == 0)
                            {
                                BLE_Connect_flag = 1;
                            }
                        }
                        else
                        {
//                            sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                            scan_start();
                        }
                    }
                    if (err_code2 != NRF_SUCCESS) 
                    {
                        // エラーハンドリング
                        is_connected = false;
                        Ble_connect_count = 0;
                        Connect_flag = 1;
                    }
                }
            }
            break;

Parents
  • Don't bother with the old NRF5 SDK, switch to NCS and look at all those bluetooth LE examples first.

    Posted code has so many problems that I can't liste them all here. Example: Calls to sd_ble_gap_connect()  and sd_ble_gap_disconnect() are non-blocking, that is why all examples are event driven. The corresponding calls in newer NCS SDK behave similar.

Reply
  • Don't bother with the old NRF5 SDK, switch to NCS and look at all those bluetooth LE examples first.

    Posted code has so many problems that I can't liste them all here. Example: Calls to sd_ble_gap_connect()  and sd_ble_gap_disconnect() are non-blocking, that is why all examples are event driven. The corresponding calls in newer NCS SDK behave similar.

Children
No Data
Related