nRF52840, Pairing through passkey

Hello, 

I have the peripheral BLE device. It is always had different mac addresses on disconnection/on reboot.

I use my mobile phone with Android OS to initiate pairing. 
On the device I can see passkey - 6 digits. So I use them on my mobile phone to finish pairing. 
Also, I have a software on my mobile phone for communication with that device. I did sniffering of their communication, 

My idea is to communicate with this device from my device(nRF52840) as central device.

But I stuck on the pairing stage. After I get in 'connected' callback, how can I correctly initiate pairing procedure from my side?

Could you please navigate me for any example / docs?

connect02.pcapng

Parents
  • Hi, 

    My colleague Kenneth explains how you can enable static passkey pairing on the peripheral and central in this thread:  Zephyr BLE static passkey between central and peripheral on two nrf52840s

    Regards,
    Amanda H.

  • Hi,

    But my case isn't about a static passkey. It's a dynamic passkey)
    Actually, I did pairing.
    I used bt_conn_auth_passkey_entry() to enter the passkey for the corresponding connection. But it would be better to see an example..

  • Hi, 

    You can take a look at this example Central UART and test with Peripheral UART

    -Amanda H.

  • Hi, 
    I have a problem with pairing. For some reason, a got an issue in callback 'security_changed '. 

    static bt_conn_cb conn_cb{
            .security_changed = OnSecurityChanged,
    };

    void Ble::OnSecurityChanged(bt_conn* conn, bt_security_t level, bt_security_err err)
    {
        char addr[BT_ADDR_LE_STR_LEN];

        bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

        if (!err)
        {
            printk("Security is changed: %s level %u\n", addr, level);
        }
        else
        {
            printk("Security is failed: %s level %u err %d\n", addr, level, err);
        }
    }
    So, I got in the log
    'Security is failed: 5A:B2:93:34:7A:61 (random) level 1 err 9.'
    But sometimes I didn't get that issue and pairing ends successfully.
    ncs v2.1.2
  • H  
    This is an example for peripheral device. 
    The project was on pause for some time and now it continues.
    So, my device is central device and I couldn't get stable pairing with peripheral device with display which provides passkey code. 
    The scenario: 
    1. From the start, I get a successful pairing (34931_pairing_OK_212.txt 34931_pairing_OK_212.txt).
    2. If I reboot my device and try to reconnect, I get a pairing error (34931_pairing_fail2_212.txt

    [00:01:43.048,736] <inf> UartTerminal: Uart frame
                                           7e 0a 00 01 03 00 06 73  58 53 4f ee 34 ed 7c 7e |~......s XSO.4.|~
    [00:01:43.100,555] <inf> ble: Provisioning mac
                                  34 ee 4f 53 58 73                                |4.OSXs           
    [00:01:43.100,585] <inf> App: Callback result, true
    [00:01:43.313,690] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 0, AD data len 28, RSSI -57
    
    [00:01:43.314,300] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 4, AD data len 6, RSSI -57
    
    [00:01:43.455,230] <inf> UartTerminal: Uart frame
                                           7e 0d 00 01 04 00 08 34  ee 4f 53 58 73 05 01 05 |~......4 .OSXs...
                                           52 77 7e                                         |Rw~              
    [00:01:43.504,791] <inf> ble: Process command to 'Start process', mac: 7358534fee34, type: eSurgeGuard
    
    [00:01:43.504,852] <inf> App: Callback result, true
    [00:01:43.567,718] <inf> ble: Attempt connection
    [00:01:43.568,054] <inf> bt_conn: 
    [00:01:43.568,145] <inf> bt_conn: disconnected -> connecting
    [00:01:43.568,145] <inf> bt_conn: handle 0 ref 1 -> 2
    [00:01:43.568,389] <inf> bt_conn: 
    [00:01:43.568,878] <inf> bt_conn: 
    [00:01:43.568,908] <inf> bt_conn: handle 0 ref 2 -> 3
    [00:01:43.569,000] <inf> ble: Create process for mac: 7358534fee34
    [00:01:43.569,030] <inf> bt_conn: handle 0 ref 3 -> 4
    [00:01:43.569,122] <inf> registry: OnPaired set flag, false
    [00:01:43.569,152] <inf> ble: Process was created
    [00:01:43.569,183] <inf> bt_conn: handle 0 ref 4 -> 5
    [00:01:43.569,366] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 0, AD data len 28, RSSI -54
    
    [00:01:43.569,458] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:43.569,458] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.080,963] <inf> bt_conn: conn handle 0 buf len 11 cb (nil) user_data (nil)
    --- 15 messages dropped ---
    Security level is setted
    
    [00:01:44.081,146] <inf> bt_conn: handle 0 ref 6 -> 7
    [00:01:44.081,207] <inf> ble: Connected: 7358534fee34
    
    [00:01:44.081,329] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.081,359] <inf> bt_conn: conn 0x20002ba8 buf 0x20012028 len 11
    [00:01:44.081,359] <inf> bt_conn: conn 0x20002ba8 buf 0x20012028 len 11 flags 0x02
    [00:01:44.081,787] <inf> bt_conn: handle 0 ref 7 -> 8
    [00:01:44.081,817] <inf> bt_conn: handle 0 ref 8 -> 7
    [00:01:44.082,000] <inf> bt_conn: 
    [00:01:44.082,000] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,305] <inf> bt_conn: 
    [00:01:44.082,305] li<inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,672] <inf> bt_conn: 
    [00:01:44.082,702] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,977] <inf> bt_conn: 
    [00:01:44.083,007] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.083,038] <inf> bt_conn: handle 0 ref 7 -> 6
    [00:01:44.083,099] <inf> bt_conn: handle 0 ref 6 -> 7
    [00:01:44.083,099] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.083,129] <inf> bt_conn: tx 0x200040d8 cb 0x52261 user_data 0x200042e4
    [00:01:44.083,160] <inf> bt_conn: handle 0 len 6 flags 02
    [00:01:44.083,190] <inf> bt_conn: First, len 6 final 2
    [00:01:44.083,190] <inf> bt_conn: Successfully parsed 6 byte L2CAP packet
    [00:01:44.083,221] <inf> bt_smp: Received SMP code 0x0b len 1
    [00:01:44.083,221] <inf> bt_smp: 
    [00:01:44.083,251] <inf> bt_conn: handle 0 ref 7 -> 6
    [00:01:44.083,312] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.083,312] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.108,001] <inf> registry: OnConnected set flag
    [00:01:44.108,032] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.141,815] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.141,845] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.201,965] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.201,965] <inf> ble: le_data_len_updated
    [00:01:44.201,965] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.202,026] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.202,056] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.202,056] <inf> bt_conn: handle 0 len 7 flags 02
    [00:01:44.202,056] <inf> bt_conn: First, len 7 final 3
    [00:01:44.202,087] <inf> bt_conn: Successfully parsed 7 byte L2CAP packet
    MTU exchange successful
    
    Current MTU: 64
    
    [00:01:44.202,178] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,411] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.321,441] <inf> bt_conn: connected -> disconnect-complete
    [00:01:44.321,441] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,655] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.321,655] <inf> bt_conn: disconnect-complete -> disconnected
    [00:01:44.321,685] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.321,685] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,746] <inf> bt_conn: 
    [00:01:44.321,807] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.321,807] <inf> bt_smp: chan 0x20002fc4 cid 0x0006
    [00:01:44.321,838] <inf> bt_smp: status 0x8
    [00:01:44.321,868] <inf> bt_smp: chan 0x20002fc4 conn 0x20002ba8 handle 0 encrypt 0x00 hci status 0x1f
    Security is failed: 73:58:53:4F:EE:34 (random) level 1 err 9
    
    [00:01:44.322,052] <inf> ble: Pairing is failed, reason code: (9). Disconnecting.
    [00:01:44.322,052] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.322,082] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.322,174] <inf> ble: Disconnected: 7358534fee34 (reason 0x05)
    
    [00:01:44.322,204] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.322,204] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.322,235] <inf> bt_conn: handle 0 ref 5 -> 4
    [00:01:44.408,416] <inf> App: Push to output queue, size: 11
    [00:01:44.408,569] <inf> ble: Process is finished,  addr: 7358534fee34, result: failed
    
    [00:01:44.408,599] <inf> bt_conn: handle 0 ref 4 -> 3
    [00:01:44.408,630] <inf> ble: Process is removed
    [00:01:44.408,660] <inf> bt_conn: handle 0 ref 3 -> 2
    [00:01:44.408,691] <inf> bt_conn: handle 0 ref 2 -> 1
    )

    So, I get event 'BT_HCI_EVT_DISCONN_COMPLETE' for some reason. 
    I get the handler there from the function 'bt_recv' (hci_core.c). 
    And I couldn't understand the reason. Could you please help with that issue?
     

Reply
  • H  
    This is an example for peripheral device. 
    The project was on pause for some time and now it continues.
    So, my device is central device and I couldn't get stable pairing with peripheral device with display which provides passkey code. 
    The scenario: 
    1. From the start, I get a successful pairing (34931_pairing_OK_212.txt 34931_pairing_OK_212.txt).
    2. If I reboot my device and try to reconnect, I get a pairing error (34931_pairing_fail2_212.txt

    [00:01:43.048,736] <inf> UartTerminal: Uart frame
                                           7e 0a 00 01 03 00 06 73  58 53 4f ee 34 ed 7c 7e |~......s XSO.4.|~
    [00:01:43.100,555] <inf> ble: Provisioning mac
                                  34 ee 4f 53 58 73                                |4.OSXs           
    [00:01:43.100,585] <inf> App: Callback result, true
    [00:01:43.313,690] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 0, AD data len 28, RSSI -57
    
    [00:01:43.314,300] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 4, AD data len 6, RSSI -57
    
    [00:01:43.455,230] <inf> UartTerminal: Uart frame
                                           7e 0d 00 01 04 00 08 34  ee 4f 53 58 73 05 01 05 |~......4 .OSXs...
                                           52 77 7e                                         |Rw~              
    [00:01:43.504,791] <inf> ble: Process command to 'Start process', mac: 7358534fee34, type: eSurgeGuard
    
    [00:01:43.504,852] <inf> App: Callback result, true
    [00:01:43.567,718] <inf> ble: Attempt connection
    [00:01:43.568,054] <inf> bt_conn: 
    [00:01:43.568,145] <inf> bt_conn: disconnected -> connecting
    [00:01:43.568,145] <inf> bt_conn: handle 0 ref 1 -> 2
    [00:01:43.568,389] <inf> bt_conn: 
    [00:01:43.568,878] <inf> bt_conn: 
    [00:01:43.568,908] <inf> bt_conn: handle 0 ref 2 -> 3
    [00:01:43.569,000] <inf> ble: Create process for mac: 7358534fee34
    [00:01:43.569,030] <inf> bt_conn: handle 0 ref 3 -> 4
    [00:01:43.569,122] <inf> registry: OnPaired set flag, false
    [00:01:43.569,152] <inf> ble: Process was created
    [00:01:43.569,183] <inf> bt_conn: handle 0 ref 4 -> 5
    [00:01:43.569,366] <inf> ble: [DEVICE]: 73:58:53:4F:EE:34 (random), AD evt type 0, AD data len 28, RSSI -54
    
    [00:01:43.569,458] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:43.569,458] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.080,963] <inf> bt_conn: conn handle 0 buf len 11 cb (nil) user_data (nil)
    --- 15 messages dropped ---
    Security level is setted
    
    [00:01:44.081,146] <inf> bt_conn: handle 0 ref 6 -> 7
    [00:01:44.081,207] <inf> ble: Connected: 7358534fee34
    
    [00:01:44.081,329] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.081,359] <inf> bt_conn: conn 0x20002ba8 buf 0x20012028 len 11
    [00:01:44.081,359] <inf> bt_conn: conn 0x20002ba8 buf 0x20012028 len 11 flags 0x02
    [00:01:44.081,787] <inf> bt_conn: handle 0 ref 7 -> 8
    [00:01:44.081,817] <inf> bt_conn: handle 0 ref 8 -> 7
    [00:01:44.082,000] <inf> bt_conn: 
    [00:01:44.082,000] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,305] <inf> bt_conn: 
    [00:01:44.082,305] li<inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,672] <inf> bt_conn: 
    [00:01:44.082,702] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.082,977] <inf> bt_conn: 
    [00:01:44.083,007] <inf> bt_conn: Adding conn 0x20002ba8 to poll list
    [00:01:44.083,038] <inf> bt_conn: handle 0 ref 7 -> 6
    [00:01:44.083,099] <inf> bt_conn: handle 0 ref 6 -> 7
    [00:01:44.083,099] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.083,129] <inf> bt_conn: tx 0x200040d8 cb 0x52261 user_data 0x200042e4
    [00:01:44.083,160] <inf> bt_conn: handle 0 len 6 flags 02
    [00:01:44.083,190] <inf> bt_conn: First, len 6 final 2
    [00:01:44.083,190] <inf> bt_conn: Successfully parsed 6 byte L2CAP packet
    [00:01:44.083,221] <inf> bt_smp: Received SMP code 0x0b len 1
    [00:01:44.083,221] <inf> bt_smp: 
    [00:01:44.083,251] <inf> bt_conn: handle 0 ref 7 -> 6
    [00:01:44.083,312] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.083,312] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.108,001] <inf> registry: OnConnected set flag
    [00:01:44.108,032] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.141,815] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.141,845] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.201,965] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.201,965] <inf> ble: le_data_len_updated
    [00:01:44.201,965] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.202,026] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.202,056] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.202,056] <inf> bt_conn: handle 0 len 7 flags 02
    [00:01:44.202,056] <inf> bt_conn: First, len 7 final 3
    [00:01:44.202,087] <inf> bt_conn: Successfully parsed 7 byte L2CAP packet
    MTU exchange successful
    
    Current MTU: 64
    
    [00:01:44.202,178] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,411] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.321,441] <inf> bt_conn: connected -> disconnect-complete
    [00:01:44.321,441] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,655] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.321,655] <inf> bt_conn: disconnect-complete -> disconnected
    [00:01:44.321,685] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.321,685] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.321,746] <inf> bt_conn: 
    [00:01:44.321,807] <inf> bt_conn: conn 0x20002ba8
    [00:01:44.321,807] <inf> bt_smp: chan 0x20002fc4 cid 0x0006
    [00:01:44.321,838] <inf> bt_smp: status 0x8
    [00:01:44.321,868] <inf> bt_smp: chan 0x20002fc4 conn 0x20002ba8 handle 0 encrypt 0x00 hci status 0x1f
    Security is failed: 73:58:53:4F:EE:34 (random) level 1 err 9
    
    [00:01:44.322,052] <inf> ble: Pairing is failed, reason code: (9). Disconnecting.
    [00:01:44.322,052] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.322,082] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.322,174] <inf> ble: Disconnected: 7358534fee34 (reason 0x05)
    
    [00:01:44.322,204] <inf> bt_conn: handle 0 ref 5 -> 6
    [00:01:44.322,204] <inf> bt_conn: handle 0 ref 6 -> 5
    [00:01:44.322,235] <inf> bt_conn: handle 0 ref 5 -> 4
    [00:01:44.408,416] <inf> App: Push to output queue, size: 11
    [00:01:44.408,569] <inf> ble: Process is finished,  addr: 7358534fee34, result: failed
    
    [00:01:44.408,599] <inf> bt_conn: handle 0 ref 4 -> 3
    [00:01:44.408,630] <inf> ble: Process is removed
    [00:01:44.408,660] <inf> bt_conn: handle 0 ref 3 -> 2
    [00:01:44.408,691] <inf> bt_conn: handle 0 ref 2 -> 1
    )

    So, I get event 'BT_HCI_EVT_DISCONN_COMPLETE' for some reason. 
    I get the handler there from the function 'bt_recv' (hci_core.c). 
    And I couldn't understand the reason. Could you please help with that issue?
     

Children
Related