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

how to make the characteristics can be write by the smartphone and how know the characteristics was written by the smartphone?

hi

i have added new characteristics on a existing service,and i do not know how to make this characteristics can be change by the smartphone and how to know this characteristics are changed by the smartphone.

the characteristics i added as showed in below.could someone can help me where can i set the characteristics can be changed by the smartphone?

static uint32_t temperature_char_add(ble_bas_t * p_bas, const ble_bas_init_t * p_bas_init) { uint32_t err_code; ble_gatts_char_md_t char_md; ble_gatts_attr_md_t cccd_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; uint8_t initial_temperature; uint8_t encoded_report_ref[BLE_SRV_ENCODED_REPORT_REF_LEN]; uint8_t init_len;

// Add temperature characteristic
if (p_bas->is_notification_supported)
{
    memset(&cccd_md, 0, sizeof(cccd_md));

    // According to BAS_SPEC_V10, the read operation on cccd should be possible without
    // authentication.
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);   
		 
    cccd_md.write_perm = p_bas_init->temperature_char_attr_md.cccd_write_perm;    
		
    cccd_md.vloc = BLE_GATTS_VLOC_STACK;    //value location
}

memset(&char_md, 0, sizeof(char_md));   //CHARACTERISTICS METE DATA

char_md.char_properties.read   = 1;     
char_md.char_properties.notify = (p_bas->is_notification_supported) ? 1 : 0; 
char_md.p_char_user_desc       = NULL;   
char_md.p_char_pf              = NULL;
char_md.p_user_desc_md         = NULL;   
char_md.p_cccd_md              = (p_bas->is_notification_supported) ? &cccd_md : NULL;
char_md.p_sccd_md              = NULL;
	

BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_TEMPERATURE_SERVICE);

memset(&attr_md, 0, sizeof(attr_md));

attr_md.read_perm  = p_bas_init->temperature_char_attr_md.read_perm;
attr_md.write_perm = p_bas_init->temperature_char_attr_md.write_perm;
attr_md.vloc       = BLE_GATTS_VLOC_STACK;
attr_md.rd_auth    = 0;
attr_md.wr_auth    = 0;
attr_md.vlen       = 0;

initial_temperature= p_bas_init->initial_temperature;

memset(&attr_char_value, 0, sizeof(attr_char_value));

attr_char_value.p_uuid       = &ble_uuid;
attr_char_value.p_attr_md    = &attr_md;
attr_char_value.init_len     = sizeof(uint8_t);
attr_char_value.init_offs    = 0;
attr_char_value.max_len      = sizeof(uint8_t);
attr_char_value.p_value      = &initial_temperature;

err_code = ble_gatts_characteristic_add(p_bas->service_handle, &char_md,
                                        &attr_char_value,
                                        &p_bas->temperature_handles);
if (err_code != NRF_SUCCESS)
{
    return err_code;
}

if (p_bas_init->p_report_ref != NULL)
{
    // Add Report Reference descriptor
    BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_REPORT_REF_DESCR);
    
    memset(&attr_md, 0, sizeof(attr_md));

    attr_md.read_perm = p_bas_init->temperature_report_read_perm;
    BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);

    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 0;
    
    init_len = ble_srv_report_ref_encode(encoded_report_ref, p_bas_init->p_report_ref);
    
    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid       = &ble_uuid;
    attr_char_value.p_attr_md    = &attr_md;
    attr_char_value.init_len     = init_len;
    attr_char_value.init_offs    = 0;
    attr_char_value.max_len      = attr_char_value.init_len;
    attr_char_value.p_value      = encoded_report_ref;
    
    err_code = ble_gatts_descriptor_add(p_bas->temperature_handles.value_handle,
                                        &attr_char_value,
                                        &p_bas->report_ref_handle);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
}
else
{
    p_bas->report_ref_handle = BLE_GATT_HANDLE_INVALID;
}

return NRF_SUCCESS;

}

can we use the function BLE_GAP_CONN_SEC_MODE_SET_OPEN?

i know all the BLE example code has the function to add characteristics ,but it is really hard for fresh man to understand. tks!

Related