<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>About NUS connection</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/115472/about-nus-connection</link><description>I have implemented NUS on device A and confirmed that it can be connected from a PC by making it the advertising sender. 
 Device B can scan device A&amp;#39;s advertisements and obtain values ​​from the advertisement data. 
 I would like to add an NUS function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 18 Oct 2024 13:26:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/115472/about-nus-connection" /><item><title>RE: About NUS connection</title><link>https://devzone.nordicsemi.com/thread/506947?ContentTypeID=1</link><pubDate>Fri, 18 Oct 2024 13:26:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ea573277-da2e-4dc9-b4fd-3b05372fb472</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;sd_ble_gap_connect() will return&amp;nbsp;NRF_ERROR_NOT_FOUND if&amp;nbsp;conn_cfg_tag (the last parameter) cannot be found. Which value did you set it to, and did you call&amp;nbsp;nrf_sdh_ble_default_cfg_set with that value (search for APP_BLE_CONN_CFG_TAG in the SDK to see examples of this)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: About NUS connection</title><link>https://devzone.nordicsemi.com/thread/506213?ContentTypeID=1</link><pubDate>Tue, 15 Oct 2024 04:44:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2baf68ff-c97c-4415-93ce-5407193d4d0a</guid><dc:creator>Junichi</dc:creator><description>&lt;p&gt;Thank you for your answer.&lt;/p&gt;
&lt;p&gt;By adding UART initialization, the NRF_ERROR_INVALID_STATE problem is resolved.&lt;/p&gt;
&lt;p&gt;However, when I execute err_code2 = sd_ble_gap_connect(&amp;amp;addr, &amp;amp;scan_params, &amp;amp;conn_params, con_cfg_tag);, 0x05 (NRF_ERROR_NOT_FOUND) is returned and I cannot connect.&lt;/p&gt;
&lt;p&gt;What could be the cause?&lt;pre class="ui-code" data-mode="text"&gt;void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
    switch (p_scan_evt-&amp;gt;scan_evt_id)
    {
        case BLE_GAP_EVT_ADV_REPORT:
            break;
        case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
            NRF_LOG_INFO(&amp;quot;Scan timeout.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
            NRF_LOG_ERROR(&amp;quot;Connection error.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected to device.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_FILTER_MATCH:
        {
            // Get Advertising Report
            ble_gap_evt_adv_report_t const * p_adv_report = p_scan_evt-&amp;gt;params.filter_match.p_adv_report;

            // Get advertising data
            const uint8_t * p_data = p_adv_report-&amp;gt;data.p_data; // Get p_data from ble_data_t
            uint16_t data_len = p_adv_report-&amp;gt;data.len; 		// Get the data length from ble_data_t

            // Get Device Name
            char device_name[BLE_GAP_DEVNAME_MAX_LEN + 1]; 		// A buffer for the device name
            memset(device_name, 0, sizeof(device_name)); 		// Clear the buffer

            // Finding device names from advertising data
            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)
            {
                // Get the length of the device name
                uint8_t name_len = strlen((const char *)p_name_data);
                strncpy(device_name, (char *)p_name_data, name_len);
                device_name[name_len] = &amp;#39;\0&amp;#39;; 					// NULL terminated
                device_name[6] = &amp;#39;\0&amp;#39;;
                // If the device names match, the connection is initiated.
                if (strcmp(device_name, TARGET_DEVICE_NAME) == 0)
                {
                    // Set the address of the destination device
                    Name_acquisition_flag = 1;
                    ble_gap_addr_t addr;
                    addr.addr_id_peer = 0; 												// 0 if not a private address
                    addr.addr_type = p_adv_report-&amp;gt;peer_addr.addr_type; 				// Set the address type (0x01)
                    memcpy(addr.addr, p_adv_report-&amp;gt;peer_addr.addr, BLE_GAP_ADDR_LEN); 	// Copy address

                    // Initialize scan parameters
                    ble_gap_scan_params_t scan_params;
                    memset(&amp;amp;scan_params, 0, sizeof(scan_params)); 						// Zero Initialization
                    scan_params.extended = 0; 											// 0 if you do not want to use extended scanning
                    scan_params.active = 1; 											// Enable Active Scanning
                    scan_params.report_incomplete_evts = 0; 							// Do not report incomplete events
                    scan_params.scan_phys = BLE_GAP_PHY_1MBPS; 							// Back PHY and feeling  (0x01)
                    scan_params.interval = 160; 										// Set the scan interval
                    scan_params.window = 72; 											// Set the scan window
                    scan_params.timeout = 0; 											// No timeout

                    // Initializing connection parameters
                    ble_gap_conn_params_t conn_params;
                    memset(&amp;amp;conn_params, 0, sizeof(conn_params)); 						// Zero Initialization
                    conn_params.min_conn_interval = MIN_CONN_INTERVAL; 					// Minimum Connection Interval (160)
                    conn_params.max_conn_interval = MAX_CONN_INTERVAL; 					// Maximum Connection Interval (320)
                    conn_params.slave_latency = SLAVE_LATENCY; 							// Slave Latency  (0)
                    conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; 					// Connection Supervision Timeout (MSEC_TO_UNITS(4000, UNIT_10_MS)
                    int attempts = 0;
//                    if (is_scanning)
//                    {
//                        err_code2 = sd_ble_gap_scan_stop();
//                        if (err_code2 != NRF_SUCCESS) 
//                        {
//                            is_scanning = true;
//                        }
//                        else
//                        {
//                            is_scanning = false; 
//                        }
//                    }
                    // Start a connection
                    if (!is_connected)
                    {
                        nrf_delay_ms(200);
                        err_code2 = sd_ble_gap_connect(&amp;amp;addr, &amp;amp;scan_params, &amp;amp;conn_params, con_cfg_tag);
                        if(err_code2 == NRF_ERROR_NOT_FOUND)
                        {
                            for(int i = 0 ; i &amp;lt; 5 ; i++)
                            {
                                nrf_delay_ms(1000);
                                err_code2 = sd_ble_gap_connect(&amp;amp;addr, &amp;amp;scan_params, &amp;amp;conn_params, con_cfg_tag);
                                if(err_code2 == 0)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    device_name[6] = &amp;#39;\0&amp;#39;;
                    if (err_code2 != NRF_SUCCESS) 
                    {
                        is_connected = true;
                    }
                }
            }
            break;
        }
        default:
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: About NUS connection</title><link>https://devzone.nordicsemi.com/thread/506096?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 11:55:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:204b16fd-76b3-4c44-b170-1c9427e8873c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I see you are using the nRF5 SDK. That incldues the&amp;nbsp;&lt;a href="https://docs.nordicsemi.com/bundle/sdk_nrf5_v17.1.0/page/ble_sdk_app_nus_c.html"&gt;Nordic UART Service Client&lt;/a&gt;&amp;nbsp;example, which is a Central and does what you describe here (this works together with the &lt;a href="https://docs.nordicsemi.com/bundle/sdk_nrf5_v17.1.0/page/ble_sdk_app_nus_eval.html"&gt;UART/Serial Port Emulation over BLE&lt;/a&gt;&amp;nbsp;peripehral example).&lt;/p&gt;
&lt;p&gt;Regardign the -8 error you get, that is&amp;nbsp;NRF_ERROR_INVALID_STATE. And you typically get this from&amp;nbsp;sd_ble_gap_connect() if a connection is allready in progress. You will also get this error cod eif you call&amp;nbsp;sd_ble_gap_scan_stop when it is not scanning (if you just wanted to terminate scanning if active you can shoose to ignore this error code, for instance by only calling APP_ERROR_CHECK if the error is different from NRF_ERROR_INVALID_STATE).&lt;/p&gt;
&lt;p&gt;But as the description you have match well with the central NUS example linked above, I suggest you test with this first and either base your client on that or copy-past the relevant parts into your application as that i a known working implementation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: About NUS connection</title><link>https://devzone.nordicsemi.com/thread/506018?ContentTypeID=1</link><pubDate>Mon, 14 Oct 2024 07:35:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e5189edf-8cc7-45d7-99bd-6e936d4d0628</guid><dc:creator>Junichi</dc:creator><description>&lt;p&gt;I solved the above post myself, but a new problem has arisen.&lt;/p&gt;
&lt;p&gt;I am trying to perform a BLE scan, find the specified Bluetooth ID, and perform an NUS connection.&lt;/p&gt;
&lt;p&gt;When I execute sd_ble_gap_connect, it returns 8, so I added sd_ble_gap_scan_stop before sd_ble_gap_connect, but sd_ble_gap_scan_stop also returns 8.&lt;/p&gt;
&lt;p&gt;The Scan event function is as follows.&lt;pre class="ui-code" data-mode="text"&gt;void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
    switch (p_scan_evt-&amp;gt;scan_evt_id)
    {
        case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT:
            NRF_LOG_INFO(&amp;quot;Scan timeout.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
            NRF_LOG_ERROR(&amp;quot;Connection error.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_CONNECTED:
            NRF_LOG_INFO(&amp;quot;Connected to device.&amp;quot;);
            break;

        case NRF_BLE_SCAN_EVT_FILTER_MATCH:
        {
            // アドバタイジングレポートを取得
            ble_gap_evt_adv_report_t const * p_adv_report = p_scan_evt-&amp;gt;params.filter_match.p_adv_report;

            // アドバタイジングデータを取得
            const uint8_t * p_data = p_adv_report-&amp;gt;data.p_data; // ble_data_t から p_data を取得
            uint16_t data_len = p_adv_report-&amp;gt;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);
//                if(name_len &amp;gt; 6)
//                {
//                    break;
//                }
//                if (name_len &amp;gt; BLE_GAP_DEVNAME_MAX_LEN) {
//                    name_len = BLE_GAP_DEVNAME_MAX_LEN; // 長さが最大を超えないようにする
//                }
                strncpy(device_name, (char *)p_name_data, name_len);
                device_name[name_len] = &amp;#39;\0&amp;#39;; // NULL で終端
                device_name[6] = &amp;#39;\0&amp;#39;;
                // デバイス名が一致する場合、接続を開始
                if (strcmp(device_name, TARGET_DEVICE_NAME) == 0)
                {
                    // 接続先デバイスのアドレスを設定
                    ble_gap_addr_t addr;
                    addr.addr_id_peer = 0; // プライベートアドレスでない場合は0
                    addr.addr_type = p_adv_report-&amp;gt;peer_addr.addr_type; // アドレスのタイプを設定
                    memcpy(addr.addr, p_adv_report-&amp;gt;peer_addr.addr, BLE_GAP_ADDR_LEN); // アドレスをコピー

                    // スキャンパラメータの初期化
                    ble_gap_scan_params_t scan_params;
                    memset(&amp;amp;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_1_25_MS); // スキャン間隔を設定
                    scan_params.window = MSEC_TO_UNITS(50, UNIT_1_25_MS); // スキャンウィンドウを設定
                    scan_params.timeout = 0; // タイムアウトなし

                    // 接続パラメータの初期化
                    ble_gap_conn_params_t conn_params;
                    memset(&amp;amp;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; // 接続スーパーバイスタイムアウト

                    // スキャンを停止
                    uint32_t err_code = sd_ble_gap_scan_stop();
                    if (err_code != NRF_SUCCESS){

                        // スキャン停止のエラーハンドリング
                        break;
                    }
                    // 接続を開始
                    err_code = sd_ble_gap_connect(&amp;amp;addr, &amp;amp;scan_params, &amp;amp;conn_params, con_cfg_tag);
                    device_name[6] = &amp;#39;\0&amp;#39;;
                    if (err_code != NRF_SUCCESS) 
                    {
                        // エラーハンドリング
                    }
                }
            }
            break;
        }
        default:
            break;
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>