This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Central connection with password

Hello.

My application serve as a central that connect to device by its name. I use the example \ble_central\ble_app_uart_c

On the device side I have password for connecting.

I want to connect only to device that has the require password.

I know on phone I do it by hand and type the password.

But how can I do it in the nRF52840 automatically?

Is there a place to write the require password , and what setting I have to add to the central to use that password when connect?

Thanks

Bar.

Parents Reply Children
  • Thanks Sigurd.

    I get progress and place the events and the request according the link you gave me. 

    static void _blenus_ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        uint32_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected");
           // signaling connection
            for(uint8_t s=0;s<4;s++)
            {
                nrf_gpio_pin_set(41);// Green LED
                nrf_delay_ms(500);
                nrf_gpio_pin_clear(41);
            }
    //            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
    //            APP_ERROR_CHECK(err_code);
    			
                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected");
                // LED indication will be changed when advertising starts.
    			_driver_bundle->G.is_connected = 0u;
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
                // signaling disconnection
                for(uint8_t s=0;s<4;s++)
                {
                    nrf_gpio_pin_set(9);// Red LED
                    nrf_delay_ms(500);
                    nrf_gpio_pin_clear(9);
                }	
                if(!_driver_bundle->PHY.if_scan)// if not central
                {
                    if(_driver_bundle->G.is_bonded){
                    // Change advertising mode to whitelist advertising
                    sd_ble_gap_adv_stop(m_advertising.adv_handle);
                    _blenus_advertising_init();
                    ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
                    }
                }
                else // if central need to told the PC on disconnect and doesn't connect again automaticly
                {
                   ;
                }
                break;
    
            case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
            {
                NRF_LOG_DEBUG("PHY update request.");
                ble_gap_phys_t const phys =
                {
                    .rx_phys = BLE_GAP_PHY_AUTO,
                    .tx_phys = BLE_GAP_PHY_AUTO,
                };
                err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
                APP_ERROR_CHECK(err_code);
            } break;
    		
    		case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
    			NRF_LOG_DEBUG("Connection interval request: [%d, %d]",
    				p_ble_evt->evt.gap_evt.params.conn_param_update_request.conn_params.min_conn_interval,
    				p_ble_evt->evt.gap_evt.params.conn_param_update_request.conn_params.max_conn_interval);
    			break;
    
            case BLE_GAP_EVT_CONN_PARAM_UPDATE:
    			NRF_LOG_DEBUG("Connection interval: [%d, %d]",
    				p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.min_conn_interval,
    				p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params.max_conn_interval);
    			break;
    			
    //		case PM_EVT_CONN_SEC_SUCCEEDED:
    //        	// Sent after the connection has been secured,
    //        	NRF_LOG_INFO("Bonded successfully");
    //        	_driver_bundle->G.is_bonded = true;
    //        	pm_whitelist_set(&(p_evt->peer_id), 1);
    //        	pm_sec_params_set(NULL);	// Disallow new bonds
    //        	break;
    
            case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
                // Pairing not supported
    //            err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
    //            APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
    //            err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
    //            APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_TIMEOUT:
                // Disconnect on GATT Server timeout event.
                err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                                 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
                APP_ERROR_CHECK(err_code);
                break;
            case BLE_GAP_EVT_AUTH_KEY_REQUEST:// request to send a keypass which write in teh periph
                {
                    NRF_LOG_INFO("Request password from central");
                    err_code = sd_ble_gap_auth_key_reply(m_conn_handle,BLE_GAP_AUTH_KEY_TYPE_PASSKEY ,_driver_bundle->PHY.passkey);
                }
                break;
            case BLE_GAP_EVT_CONN_SEC_UPDATE: // when accept the keypass the central send to the periph
                {
                   NRF_LOG_INFO("accept the password from central"); 
                }
                break;
            case BLE_GAP_EVT_AUTH_STATUS: // seccuss connect to periph with passkey
                {
                   NRF_LOG_INFO("seccuss connect with password"); 
                    nrf_gpio_pin_set(12);// Blue LED
                    nrf_delay_ms(500);
                    nrf_gpio_pin_clear(12);
                }
                break;
            default:
                // No implementation needed.
                break;
        }
    }

    I get the connection and after it it just jump to the gatt and set thing there also my peripheral has a keypass enable and request its identical (we use it against phone where it request it there).

    Here are the my debug terminal during connection process

     [0m[00:00:00.000,000] <debug> ble_scan: Connecting[0m
    [0m[00:00:00.000,000] <debug> ble_scan: Connection status: 0[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: Requesting to update ATT MTU to 247 bytes on connection 0x0.[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: Updating data length to 251 on connection 0x0.[0m
    [1;32m[00:00:00.000,000] <info> blenus: Connected[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 247 bytes.[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: Updating ATT MTU to 247 bytes (desired: 247) on connection 0x0.[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: ATT MTU updated to 247 bytes on connection 0x0 (response).[0m
    [1;32m[00:00:00.000,000] <info> blenus: Data len is set to 0xF4(244)[0m
    [0m[00:00:00.000,000] <debug> blenus: ATT MTU exchange completed. central 0xF7 peripheral 0xF7[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: max_rx_octets: 251[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: max_tx_octets: 251[0m
    [0m[00:00:00.000,000] <debug> nrf_ble_gatt: max_rx_time: 2120[0m

    It doesn't get to 

     case BLE_GAP_EVT_AUTH_KEY_REQUEST:

    Is there any more setting I need to do at the central init in the peer_manager to let him know it should also wait for passkey change?

    Thanks

    Bar. 

  • Hi,

    Typically you initialize the Peer Manager like shown in the snippet below. Try to set SEC_PARAM_IO_CAPABILITIES to BLE_GAP_IO_CAPS_KEYBOARD_ONLY on the central.

    /**@brief Function for the Peer Manager initialization.
     */
    static void peer_manager_init(void)
    {
        ble_gap_sec_params_t sec_param;
        ret_code_t err_code;
    
        err_code = pm_init();
        APP_ERROR_CHECK(err_code);
    
        memset(&sec_param, 0, sizeof(ble_gap_sec_params_t));
    
        // Security parameters to be used for all security procedures.
        sec_param.bond           = SEC_PARAM_BOND;
        sec_param.mitm           = SEC_PARAM_MITM;
        sec_param.lesc           = SEC_PARAM_LESC;
        sec_param.keypress       = SEC_PARAM_KEYPRESS;
        sec_param.io_caps        = SEC_PARAM_IO_CAPABILITIES;
        sec_param.oob            = SEC_PARAM_OOB;
        sec_param.min_key_size   = SEC_PARAM_MIN_KEY_SIZE;
        sec_param.max_key_size   = SEC_PARAM_MAX_KEY_SIZE;
        sec_param.kdist_own.enc  = 1;
        sec_param.kdist_own.id   = 1;
        sec_param.kdist_peer.enc = 1;
        sec_param.kdist_peer.id  = 1;
    
        err_code = pm_sec_params_set(&sec_param);
        APP_ERROR_CHECK(err_code);
    
        err_code = pm_register(pm_evt_handler);
        APP_ERROR_CHECK(err_code);
    }

    See "Passkey bonding with keyboard capabilities:" at this link: https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.2/lib_pm_usage.html#lib_pm_usage_security

  • Hello.

    I follow the prievies answer.

    I found that compare to the link you specifay before https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.s140.api.v7.2.0%2Fgroup___b_l_e___g_a_p___c_e_n_t_r_a_l___b_o_n_d_i_n_g___p_k___p_e_r_i_p_h___o_o_b___m_s_c.html

    I need to use event also from the pm handle. For example instead of  BLE_GAP_EVT_SEC_PARAMS_REQUEST I see I get event PM_EVT_CONN_SEC_PARAMS_REQso I need to use pm_conn_sec_params_reply. 

    I also use the pm_peers_delete(); but on run every time it told me it connect to all ready bond unit. After that in get to disconnect by BLE_GAP_EVT_DISCONNECTED:

    attach the log (use the Insert->code)

    [00:00:00.000,000] <info> blenus: Setting BLE scan init filter name is WizmiPixi_18BD22
    [00:00:00.000,000] <debug> ble_scan: Scanning
    [00:00:00.000,000] <info> blenus:  BLE scan start with error 0x0
    [00:00:00.000,000] <debug> ble_scan: Connecting
    [00:00:00.000,000] <debug> ble_scan: Connection status: 0
    [00:00:00.000,000] <debug> nrf_ble_gatt: Requesting to update ATT MTU to 247 bytes on connection 0x0.
    [00:00:00.000,000] <debug> nrf_ble_gatt: Updating data length to 251 on connection 0x0.
    [00:00:00.000,000] <info> blenus: Connected
    [00:00:00.000,000] <debug> peer_manager_handler: Connected, securing connection. conn_handle: 0
    [00:00:00.000,000] <info> blenus: Request for secure parameters from central.
    [00:00:00.000,000] <info> blenus: Start secure connection.
    [00:00:00.000,000] <info> blenus: Connected to a previously bonded device.
    [00:00:00.000,000] <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 247 bytes.
    [00:00:00.000,000] <debug> nrf_ble_gatt: ATT MTU updated to 247 bytes on connection 0x0 (response).
    [00:00:00.000,000] <info> blenus: Data len is set to 0xF4(244)
    [00:00:00.000,000] <debug> blenus: ATT MTU exchange completed. central 0xF7 peripheral 0xF7
    [00:00:00.000,000] <warning> blenus: Failed to secure
    

    So I don't know why after connect it disconnect from the device.

    Any hint where to look for?

    Bar.

  • I do some more work around it.

    1. I block on the BLE event BLE_GAP_EVT_SEC_PARAMS_REQUEST so it only get replay from PM_EVT_CONN_SEC_PARAMS_REQ with pm_conn_sec_params_reply, because when both was open it get first to the PM event and then to the BLE evt by on relay get err = 0x08 

    NRF_ERROR_INVALID_STATE.

    THen I add those 3 function at start of PM event handler copy from rscs App

    pm_handler_on_pm_evt(p_evt);
    pm_handler_disconnect_on_sec_failure(p_evt);
    pm_handler_flash_clean(p_evt);

    So I get also more LOG information and 2 of the PM event handle here.

    The next print is the log I get on my run when try to connect to device with the request name (filter by name).

    It is look that it get almost till the end by PM_EVT_PEER_DATA_UPDATE_SUCCEEDED event but then from nowhere I get to BLE  event BLE_GAP_EVT_DISCONNECTED.

    I also write all the BLE unhandled event  and I get at the end 2 which not part of the BLE_GAP_EVTS enum 

    <debug> blenus: Unhandled BLE event 0x35
    <debug> blenus: Unhandled BLE event 0x36

    It is far away from the list I have around 55 where the enum get only till 38. What events are those?

    <debug> nrf_ble_gatt: Requesting to update ATT MTU to 247 bytes on connection 0x0.
    <debug> nrf_ble_gatt: Updating data length to 251 on connection 0x0.
    <info> blenus: BLE Connected
    <debug> peer_manager_handler: Connected, securing connection. conn_handle: 0
    <debug> peer_manager_handler: Event PM_EVT_CONN_SEC_PARAMS_REQ
    <debug> peer_manager_handler: Security parameter request
    <info> blenus: PM Request for secure parameters from central.
    <debug> peer_manager_handler: Event PM_EVT_CONN_SEC_START
    <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 247 bytes.
    <debug> nrf_ble_gatt: Updating ATT MTU to 247 bytes (desired: 247) on connection 0x0.
    <debug> blenus: Unhandled BLE event 0x55
    <debug> nrf_ble_gatt: ATT MTU updated to 247 bytes on connection 0x0 (response).
    <debug> peer_manager_handler: Event PM_EVT_CONN_SEC_PARAMS_REQ
    <debug> peer_manager_handler: Security parameter request
    <info> blenus: PM Request for secure parameters from central.
    <debug> blenus: Unhandled BLE event 0x13
    <info> blenus: BLE Request password from central
    <info> blenus: BLE accept the password from central
    <debug> peer_manager_handler: Event PM_EVT_CONN_SEC_SUCCEEDED
    <info> peer_manager_handler: Connection secured: role: Central, conn_handle: 0, procedure: Bonding
    <debug> peer_manager_handler: pm_peer_rank_highest() returned NRF_ERROR_NOT_SUPPORTED for peer id 0
    <info> blenus: PM Connection secured: role: Central, conn_handle: 0, procedure: Bonding, Peer ID: 0
    <debug> peer_manager_handler: Event PM_EVT_PEER_DATA_UPDATE_SUCCEEDED
    <info> peer_manager_handler: Peer data updated in flash: peer_id: 0, data_id: Bonding data, action: Update
    <info> blenus: PM New Bond, add the peer to the whitelist if possible
    <info> blenus: m_whitelist_peer_cnt 2, MAX_PEERS_WLIST 8
    <debug> blenus: Unhandled BLE event 0x35
    <debug> blenus: Unhandled BLE event 0x36
    <debug> peer_manager_handler: Event PM_EVT_PEER_DATA_UPDATE_SUCCEEDED
    <info> peer_manager_handler: Peer data updated in flash: peer_id: 0, data_id: Central address resolution, action: Update

  • Hi,

    For ble_app_uart_c code with passkey and Peer Manger, you can find some example code here:

    https://devzone.nordicsemi.com/f/nordic-q-a/44658/communication-problems-between-ble_peripheral-and-ble_central-in-nrf52-project

    Bar said:
    It is far away from the list I have around 55 where the enum get only till 38. What events are those?

     If it's in hex, then it's BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP and BLE_GATTC_EVT_READ_RSP

    If it's in dec, then it's BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST and BLE_GAP_EVT_DATA_LENGTH_UPDATE.

Related