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

HID demo restart cound't control any more

I programmed to nRF51 DK, the first time the device is connected to mobile phone.It works, but if i turn off the nRF51 DK and then turn it on.it is connected but it cound't control any more. This is my demo nRF51822_HID_KEYBOARD_MOUSE_C_20170926.rar.

#define DM_GATT_CCCD_COUNT 9

Ithink here have a question.

  • Hi,

    It could be that some of the CCCD states is not saved on the BLE_HIDS_EVT_NOTIF_ENABLED event. From the sd_ble_gatts_hvx() i get NRF_ERROR_INVALID_STATE, which means that a Invalid Connection State or notifications and/or indications not enabled in the CCCD.

  • hi, Thank you for your reply. I don't understand,So What should I do.

  • Hi,

    In the function device_manager_evt_handler() in main.c

    Change the function, so it looks like this:

    static uint32_t device_manager_evt_handler(dm_handle_t const *p_handle,
                                               dm_event_t const * p_event,
                                               ret_code_t         event_result)
    {
        APP_ERROR_CHECK(event_result);
        switch (p_event->event_id)
        {
            case DM_EVT_CONNECTION:
                m_peer_handle = (*p_handle);
                security_timer_start(&m_peer_handle);
                break;
    
            case DM_EVT_DEVICE_CONTEXT_LOADED: // Fall through.
            case DM_EVT_SECURITY_SETUP_COMPLETE:
                m_bonded_peer_handle = (*p_handle);
                break;
    
            default:
                // No implementation needed.
                break;
        }
    
        return NRF_SUCCESS;
    }
    
  • Also change the function on_hids_evt() in main.c so it looks like this:

    static void on_hids_evt(ble_hids_t *p_hids, ble_hids_evt_t *p_evt)
    {
        switch (p_evt->evt_type)
        {
            case BLE_HIDS_EVT_BOOT_MODE_ENTERED:
                m_in_boot_mode = true;
                break;
    
            case BLE_HIDS_EVT_REPORT_MODE_ENTERED:
                m_in_boot_mode = false;
                break;
            case BLE_HIDS_EVT_REP_CHAR_WRITE:
                on_hid_rep_char_write(p_evt);
                break;
    
            case BLE_HIDS_EVT_NOTIF_ENABLED:
            {
                dm_service_context_t service_context;
                service_context.service_type        = DM_PROTOCOL_CNTXT_GATT_SRVR_ID;
                service_context.context_data.len    = 0;
                service_context.context_data.p_data = NULL;
                if (m_in_boot_mode)
                {
                    // Protocol mode is Boot Protocol mode.
                    if (
                        p_evt->params.notification.char_id.uuid == BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR || p_evt->params.notification.char_id.uuid == BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR)
                    {
                        // The notification of boot mouse input report has been enabled.
                        // Save the system attribute (CCCD) information into the flash.
                        uint32_t err_code;
    
                        err_code = dm_service_context_set(&m_bonded_peer_handle, &service_context);
                        if (err_code != NRF_ERROR_INVALID_STATE)
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                        else
                        {
                            // The system attributes could not be written to the flash because
                            // the connected central is not a new central. The system attributes
                            // will only be written to flash only when disconnected from this central.
                            // Do nothing now.
                        }
                    }
                    else
                    {
                        // Do nothing.
                    }
                }
                else if (
                    (p_evt->params.notification.char_id.rep_index == INPUT_REP_MOVEMENT_INDEX) &&
                    (p_evt->params.notification.char_id.rep_type == BLE_HIDS_REP_TYPE_INPUT))
                {
                    // The protocol mode is Report Protocol mode. And the CCCD for the report ID
                    // INPUT_REP_MOVEMENT_INDEX is changed. Since this application uses this report to
                    // send mouse movements, it is now time to store all the CCCD information (system
                    // attributes) into the flash.
                    uint32_t err_code;
    
                    err_code = dm_service_context_set(&m_bonded_peer_handle, &service_context);
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                    else
                    {
                        // The system attributes could not be written to the flash because
                        // the connected central is not a new central. The system attributes
                        // will only be written to flash only when disconnected from this central.
                        // Do nothing now.
                    }
                }
                else
                {
                    uint32_t err_code;
    
                    err_code = dm_service_context_set(&m_bonded_peer_handle, &service_context);
                    if (err_code != NRF_ERROR_INVALID_STATE)
                    {
                        APP_ERROR_CHECK(err_code);
                    }
                }
                break;
            }
    
            default:
                // No implementation needed.
                break;
        }
    }
    
Related