Skip to content

Persistent Pairing data and keys examples? #2624

@tbandtg

Description

@tbandtg

When I use the sdk to create an example none of them seem to store pairing data and keys in flash. I have searched for a good example and nothing really seems to work. Pairing and connections work the first time but are lost on reset. You would think there would be thousands of these.

Is there an example out there that shows the PICO W as a ble device that can

  1. Pair and connect
  2. ON reset reconnect to that same host
  3. Delete pairing so that a new host can be used.

I am including where I am there is some commented code because I thought I would have to implement a tlv but that didnt help.

**
 * @brief Function to initialize the BLE driver
 * @returns sErrorStruct_t structure with error code and line number.
 */
sErrorStruct_t initializeBLE(void)
{
    sErrorStruct_t retValue = BLANK_ERROR_STRUCT;
    uint16_t adv_int_min = 0x0030;
    uint16_t adv_int_max = 0x0030;
    uint8_t adv_type = 0;
    bd_addr_t null_addr;
    //retValue = initializeBLEStorage();

    bool bondedDevice = hasBondingData();
    if(ERROR_NONE == retValue.errorCode)
    {

        DEBUG_PRINT("BLE bonding data %s\n", bondedDevice ? "exists" : "does not exist");
        btstack_memory_init();        


        // register for HCI events
        HCIEventCallbackRegistration.callback = (btstack_packet_handler_t)(&packetHandler);
        hci_add_event_handler(&HCIEventCallbackRegistration);

        // register for SM events
        SMEventCallbackRegistration.callback = (btstack_packet_handler_t)(&smEventHandler);
        sm_add_event_handler(&SMEventCallbackRegistration);
        btstack_tlv_get_instance(&tlv, &tlv_ctx);
        if (tlv && tlv_ctx){
            // Tell LE device DB (bond store) to use TLV in flash
            le_device_db_tlv_configure(tlv, tlv_ctx);
        } else {
            // Fallback (RAM only) — not persistent
            DEBUG_PRINT("WARNING: No TLV backend available; bonding will NOT persist!\n");
        }   
        // Initialize LE Device Database for bonding
        //le_device_db_tlv_configure(ble_tlv_get_instance(), NULL);

        // l2cap inititialization
        l2cap_init();
        // Secuurity manager initialization
        sm_init();

        sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);


        sm_set_authentication_requirements(AUTH_REQ);
        sm_set_encryption_key_size_range(MIN_ENC_KEY_SIZE, MAX_ENC_KEY_SIZE);
        sm_set_request_security(1);                    // request encryption on connection if possible
        sm_set_accepted_stk_generation_methods( SM_STK_GENERATION_METHOD_JUST_WORKS
                                            |SM_STK_GENERATION_METHOD_PASSKEY ); // adjust as needed
        // setup ATT server
        att_server_init(profile_data, NULL, NULL);

        // setup battery service
        battery_service_server_init(batteryPercentage);

        // setup device information service
        device_information_service_server_init();

        // setup HID Device service
        hids_device_init(0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode));

        // setup advertisements
        memset(null_addr, 0, 6);

        gap_advertisements_set_params(adv_int_min, adv_int_max, adv_type, 0, null_addr, 0x07, 0x00);
        
        gap_advertisements_set_data(advertisementDataLength, (uint8_t*) advertisementData);

        setBLEAdvertising(true);

        // Initialize the queue for HID keyboard reports
        queue_init_with_spinlock(&hidKeyboardReportQueue, sizeof(hid_keyboard_report_t), REPORTS_IN_QUEUE, spin_lock_claim_unused(true)); // 10 reports in the queue
        // turn on!
        hci_power_control(HCI_POWER_ON);
    }

    return retValue;
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions