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

Cannot remove the custom service

MCU: nRF52832
SDK VERSION: 17.0.0_9d13099
SOFTDEVICE: 7.0.1 (s132)

I have an application with some custom services (4) with different characteristics number and behavior (read/write/notify) and everything works just fine. The problem started when I wanted to create different modes of operation in which not every service is initialized e.g. mode 1 when everything is operational and mode 2 where only DIS, BAS and one of the custom services is on. When entering mode 2 (which now is just flashing new code with services init lines commented) and trying to connect, I am getting error

"Failed to get services (BLE_ERROR_INVALID_CONN_HANDLE). 

Funny is, if I don't init standard services everything is just fine. The problem only appears when I don't init my custom service/s. I've tested resetting the device, removing power from the board and full erasing memory and flashing everything (bootloader, softdevice and application) again - the same result. I've tried with sd_ble_uuid_vs_remove but also with no result.

The only option to achieve my goal is to call custom service init and within it comment the service and characteristics adding lines. So the "fake" initialization looks like this:

void StsService_init(void){
	ret_code_t	errorCode;
	ble_sts_init_t	stsInitStructure;
	errorsNotyficationsEnabled = false;
	statusNotyficationsEnabled = false;
	memset(&stsInitStructure, 0, sizeof(stsInitStructure));
	stsInitStructure.evt_handler = stsEventHandler;

	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_error_value_char_attr_md.cccd_write_perm);
	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_error_value_char_attr_md.read_perm);

	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_status_value_char_attr_md.cccd_write_perm);
	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&stsInitStructure.sts_status_value_char_attr_md.read_perm);

	errorCode = ble_sts_init(&m_sts, &stsInitStructure);
	APP_ERROR_CHECK(errorCode);
}

and my ble_sts_init is: 

uint32_t ble_sts_init(ble_sts_t * p_sts, const ble_sts_init_t * p_sts_init){
	if (p_sts == NULL || p_sts_init == NULL){
		return NRF_ERROR_NULL;
	}
	uint32_t   err_code;
	ble_uuid_t ble_uuid;

	// Initialize service structure
	p_sts->conn_handle = BLE_CONN_HANDLE_INVALID;
	p_sts->evt_handler = p_sts_init->evt_handler;

	// Add Status Service UUID
//	ble_uuid128_t base_uuid = {STATUS_SERVICE_UUID_BASE};
//	err_code =  sd_ble_uuid_vs_add(&base_uuid, &p_sts->uuid_type);
//	VERIFY_SUCCESS(err_code);
//	ble_uuid.type = p_sts->uuid_type;
//	ble_uuid.uuid = STATUS_SERVICE_UUID;

//	// Add the Status Service
//	err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_sts->service_handle);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}
//	// Add the States Characteristic
//	err_code = sts_status_char_add(p_sts,p_sts_init);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}
//	// Add the Errors Characteristic
//	err_code = sts_error_char_add(p_sts, p_sts_init);
//	if (err_code != NRF_SUCCESS){
//		return err_code;
//	}

	return NRF_SUCCESS;
}

This produces the result I wanted - the service isn't visible. My question is - why simply removing StsService_init() from code doesn't work? Why do I have to do special workaround? If i comment line 9 and 10 in ble_sts_init it will also produce error. 

Any help will be appreciated. 

Regards

  • Ah, I see. Then this is probably the phone caching the services.

    Wrong guess Wink As I mentioned before: 

    I thought that maybe I need to reset BLE dongle and NRF connect but it didn't help me at all. 

    I am using USB dongle and NRF Connect for desktop all the time . I don't quite trust mobile app... to be honest Slight smile So as I mentioned I thought maybe I need to delete bond information or just replug dongle and reset NRFConnect. Didn't help. Full chip erase deleted bonds in the device and reset app deleted bonds in it. I guess it must be something else. 

    Thanks for responses anyway. It works as I wanted so I moving forward. But I'll keep in mind to investigate this further in the future. 

Related