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

Problem with sending data from central to peripheral

Hello,

recently i made software for peripheral with custom service and five custom characteristics and everything works just fine. Now I want to make central capable of writing to my custom characteristics on perpiheral. I'm working with Central/Uart example from SDK. I changed example and now i can discover all my characteristics properly, but writing to them doesn't work. i'm using  "ble_nus_string_send(&m_nus, data, sizeof(data))" but I'm not receiving anything on peripheral when I use discovered characteristics handles. What is strange when I use handle value 0x0003 I'm receiving event on peripheral, but only with this value. Can anyone tell what I'm doing wrong?

on_write handle on peripheral:

static void on_write(ble_config_t *p_config, ble_evt_t const *p_ble_evt)
{
    ret_code_t err_code;
    ble_config_evt_t evt;
    ble_config_client_context_t *p_client;
    ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;

    err_code = blcm_link_ctx_get(p_config->p_link_ctx_storage,
        p_ble_evt->evt.gatts_evt.conn_handle,
        (void *)&p_client);
    if (err_code != NRF_SUCCESS)
    {
        SEGGER_RTT_printf(0, "Link context for 0x%02X connection handle could not be fetched.",
            p_ble_evt->evt.gatts_evt.conn_handle);
    }

    memset(&evt, 0, sizeof(ble_config_evt_t));
    evt.p_config = p_config;
    evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
    evt.p_link_ctx = p_client;

    if ((p_evt_write->handle == p_config->rx_handlesID.value_handle) &&
        (p_config->data_handler != NULL))
    {
        evt.type = BLE_CONFIG_EVT_RX_ID_DATA;
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_config->data_handler(&evt);
    }
    else if ((p_evt_write->handle == p_config->rx_handlesMeasurment_interval.value_handle) &&
             (p_config->data_handler != NULL))
    {
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_config->data_handler(&evt);
    }
    else if ((p_evt_write->handle == p_config->rx_handlesAdvertising_interval.value_handle) &&
             (p_config->data_handler != NULL))
    {
        evt.type = BLE_CONFIG_EVT_ADVERTISING_INTERVAL;
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_config->data_handler(&evt);
    }
    else if ((p_evt_write->handle == p_config->rx_handlesLow_alarm.value_handle) &&
             (p_config->data_handler != NULL))
    {
        evt.type = BLE_CONFIG_EVT_LOW_ALARM;
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_config->data_handler(&evt);
    }
    else if ((p_evt_write->handle == p_config->rx_handlesHigh_alarm.value_handle) &&
             (p_config->data_handler != NULL))
    {
        evt.type = BLE_CONFIG_EVT_HIGH_ALARM;
        evt.params.rx_data.p_data = p_evt_write->data;
        evt.params.rx_data.length = p_evt_write->len;

        p_config->data_handler(&evt);
    }
    else
    {
        // Do Nothing. This event is not relevant for this service.
    }
}

Parents Reply Children
  • characteristics creating:

    uint32_t ble_config_init(ble_config_t *p_config, ble_config_init_t const *p_config_init)
    {
        ret_code_t err_code;
        ble_uuid_t ble_uuid;
        ble_uuid128_t config_base_uuid = BLE_UUID_CONFIG_SERVICE_BASE_UUID;
        ble_add_char_params_t add_char_params;
    
        VERIFY_PARAM_NOT_NULL(p_config);
        VERIFY_PARAM_NOT_NULL(p_config_init);
    
        // Initialize the service structure.
        p_config->data_handler = p_config_init->data_handler;
    
        /**@snippet [Adding proprietary Service to the SoftDevice] */
        // Add a custom base UUID.
        err_code = sd_ble_uuid_vs_add(&config_base_uuid, &p_config->uuid_type);
        VERIFY_SUCCESS(err_code);
    
        ble_uuid.type = p_config->uuid_type;
        ble_uuid.uuid = BLE_UUID_CONFIG_SERVICE;
       
    
        // Add the service.
        err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
            &ble_uuid,
            &p_config->service_handle);
        /**@snippet [Adding proprietary Service to the SoftDevice] */
        VERIFY_SUCCESS(err_code);
    
        // Add the RX Characteristic.
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid = BLE_UUID_CONFIG_ID_RX_CHARACTERISTIC;
        add_char_params.uuid_type = p_config->uuid_type;
        add_char_params.max_len = BLE_CONFIG_MAX_RX_ID_CHAR_LEN;
        add_char_params.init_len = sizeof(uint8_t);
        add_char_params.is_var_len = true;
        add_char_params.char_props.write = 1;
        add_char_params.char_props.write_wo_resp = 1;
        add_char_params.char_props.read = 1;
    
        add_char_params.read_access = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        err_code = characteristic_add(p_config->service_handle, &add_char_params, &p_config->rx_handlesID);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        // Add the RX Characteristic.
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid = BLE_UUID_CONFIG_MEASURMENT_INTERVAL_CHARACTERISTIC;
        add_char_params.uuid_type = p_config->uuid_type;
        add_char_params.max_len = BLE_CONFIG_MAX_RX_MEASURMENT_INTERVA_CHAR_LEN;
        add_char_params.init_len = sizeof(uint8_t);
        add_char_params.is_var_len = true;
        add_char_params.char_props.write = 1;
        add_char_params.char_props.write_wo_resp = 1;
        add_char_params.char_props.read = 1;
      
        add_char_params.read_access = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        err_code = characteristic_add(p_config->service_handle, &add_char_params, &p_config->rx_handlesMeasurment_interval);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
        // Add the RX Characteristic.
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid = BLE_UUID_CONFIG_ADVERTISING_INTERVAL_CHARACTERISTIC;
        add_char_params.uuid_type = p_config->uuid_type;
        add_char_params.max_len = BLE_CONFIG_MAX_RX_ADVERTISING_INTERVA_CHAR_LEN;
        add_char_params.init_len = sizeof(uint16_t);
        add_char_params.is_var_len = true;
        add_char_params.char_props.write = 1;
        add_char_params.char_props.write_wo_resp = 1;
        add_char_params.char_props.read = 1;
    
        add_char_params.read_access = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        err_code = characteristic_add(p_config->service_handle, &add_char_params, &p_config->rx_handlesAdvertising_interval);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        // Add the RX Characteristic.
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid = BLE_UUID_CONFIG_LOW_ALARM_CHARACTERISTIC;
        add_char_params.uuid_type = p_config->uuid_type;
        add_char_params.max_len = BLE_CONFIG_MAX_RX_LOW_ALARM_CHAR_LEN;
        add_char_params.init_len = sizeof(uint8_t);
        add_char_params.is_var_len = true;
        add_char_params.char_props.write = 1;
        add_char_params.char_props.write_wo_resp = 1;
        add_char_params.char_props.read = 1;
    
        add_char_params.read_access = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        err_code = characteristic_add(p_config->service_handle, &add_char_params, &p_config->rx_handlesLow_alarm);
        if (err_code != NRF_SUCCESS)
        {
            return err_code;
        }
    
        memset(&add_char_params, 0, sizeof(add_char_params));
        add_char_params.uuid = BLE_UUID_CONFIG_HIGH_ALARM_CHARACTERISTIC;
        add_char_params.uuid_type = p_config->uuid_type;
        add_char_params.max_len = BLE_CONFIG_MAX_RX_HIGH_ALARM_CHAR_LEN;
        add_char_params.init_len = sizeof(uint8_t);
        add_char_params.is_var_len = true;
        add_char_params.char_props.write = 1;
        add_char_params.char_props.write_wo_resp = 1;
        add_char_params.char_props.read = 1;
    
        add_char_params.read_access = SEC_OPEN;
        add_char_params.write_access = SEC_OPEN;
    
        err_code = characteristic_add(p_config->service_handle, &add_char_params, &p_config->rx_handlesHigh_alarm);
        return err_code;
    
        // Add the RX Characteristic.
    }

  • You need to "hover" over the different characteristics to find which is handle 0x0003, for instance for a random BLE peripheral I could find:

    The service changed characteristic (handle 0x0003) is indicate only, and is not writeable.

    Kenneth

  • Ok so handle 0x0003 is asigned to Device Name in Generic Access service. My custom characteristics are as follow: 0x000D, 0x000F, 0x0011, 0x0013, 0x0015.

  • And you can write to them from the nRF Connect app? They are writeable?

    Kenneth

Related