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

How to correct a value of characteristic in spec. range

HI,

I add a readable and writeable characteristic with :

	attr_md.vloc = BLE_GATTS_VLOC_USER;
attr_md.vlen = 1;
attr_md.wr_auth = 1;

and

	attr_char_value.p_attr_md = &attr_md;
attr_char_value.init_len = sizeof(my_value);
attr_char_value.init_offs = 0;
attr_char_value.max_len = sizeof(m_value);;
attr_char_value.p_value = (uint8_t *)&my_value;

I want to correct the value that write from App, when value out of the spec. range, for example, If value > 100, the value will be corrected to 100.

In RW_AUTH_REQUEST Event:

		if ( p_evt_write->handle==m_beacon_period_char_handles.value_handle ) {
			if ( p_evt_write->len==1 ) {
				if ( p_evt_write->data[0]>100 ) {
					// TODO: correct the value to 100
					my_value = 100;
				}
				bUpdate = true;
			} else {
				write_authorize_reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_INVALID_ATT_VAL_LENGTH;
			}
		}

But, when I set the value to 200 by MCP of Android, the MCP still show the incorrect value (200). How to update the value to 100 for "read" ?

Thanks

Parents
  • Hi Jason, Setting my_value = 100 won't help to change the value will be written to your attribute. The assignment attr_char_value.p_value = (uint8_t *)&my_value; is only set the initial value of the attribute.

    When you call sd_ble_gatts_rw_authorize_reply with gatt status = NRF_SUCCESS the value that the central set will be written to the atrribute value.

    When the value being written is out of the range, you should better to reject the write command and then either wait for another write request with in-range value or set the value to maximum 100 locally by using sd_ble_gatts_value_set().

Reply
  • Hi Jason, Setting my_value = 100 won't help to change the value will be written to your attribute. The assignment attr_char_value.p_value = (uint8_t *)&my_value; is only set the initial value of the attribute.

    When you call sd_ble_gatts_rw_authorize_reply with gatt status = NRF_SUCCESS the value that the central set will be written to the atrribute value.

    When the value being written is out of the range, you should better to reject the write command and then either wait for another write request with in-range value or set the value to maximum 100 locally by using sd_ble_gatts_value_set().

Children
Related