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

Return success/failure on BLE characteristic write (nRf52832)

Hi, I want to create one system. When master writes data of Nordic, I receive callback with BLE_GATTS_EVT_WRITE case. Here I want to write return to master with success/ failure based on write data, How we can control of write success/failure from Nordic?

Thanks

Parents Reply Children
  • Yes, you could have something like this:

    case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
    	switch (p_ble_evt->evt.gatts_evt.params.authorize_request.type) {
    		case BLE_GATTS_AUTHORIZE_TYPE_READ:
        		on_read_auth(p_ble_evt);
        		break;
    		case BLE_GATTS_AUTHORIZE_TYPE_WRITE:
        		on_write_auth(p_ble_evt);
        		break;
    	}
    	break;

  • Yes, I am doing kinda similar. Calling on_disp_write, but if condition is failing. Handlers values are different.

    static void on_disp_write(ble_cus_t *p_cus, ble_evt_t const *p_ble_evt) {
    
      ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
      ble_gatts_rw_authorize_reply_params_t auth_reply = {0};
      auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
    
      // Custom Value Characteristic Written to.
      if (p_evt_write->handle == p_cus->custom_value_handles.value_handle) {
      }
    }

  • I don't know where you're getting that value handle from. You must be setting it wrong if you're having problems with that.

  • When I tried same without writes with authorization, same part is working perfect. Can you please share on_write_auth for reference ?

  • static void on_write_auth(ble_evt_t const * p_ble_evt) {
    	ret_code_t err_code = NRF_SUCCESS;
    	ble_gatts_rw_authorize_reply_params_t auth_reply;
    	uint8_t * p_data = (uint8_t *)(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.data);
    	uint16_t len = p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.len;
    
    	uint16_t char_UUID = p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.uuid.uuid;
    	uint16_t offset = p_ble_evt->evt.gatts_evt.params.authorize_request.request.write.offset;
    
    	err_code = parse_ble_data(char_UUID, p_data, len, offset);
    
    	if (err_code == NRF_SUCCESS) {
    		auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
    		auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
    		auth_reply.params.write.update = 1;
    		auth_reply.params.write.len = len;
    		auth_reply.params.write.offset = offset;
    		auth_reply.params.write.p_data = p_data;
    	} else {
    		auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
    		auth_reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED;
    	}
    	err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle, &auth_reply);
    	APP_ERROR_CHECK(err_code);
    }

Related