Skip to content

Help wanted: How to re-enable notifications after re-connect #318

Open
@goofy2k

Description

@goofy2k

I successfully implemented a client-server pair. I use NimBLEDevice::init("Async-Client") to establish a connection and also can receive notifications from the server.

When I test loss of connection and re-connect again by temp switching off the power of the server, the client onConnect callback reports success. However, I do not get incoming notifications from the server after this.

I concluded that I have to re-enable notifications. Is that correct?

Currently my code to enable notifications is in the main routine of my client app. I want to move that code inside the onConnect routine but I can't get it working. Some help is appreciated!

Here is the code for subscription to notifications:

		 //move this functionality to the onConnect callback
		 printf(">>>>> Outside onConnect: Evaluate MIDI service ('CAFE') <<<<<\n");
		 // look for a service "CAFE"
		 pSvc = pSeqClient->getService("CAFE");
		  printf("Assigned pSvc\n");
		 if (!pSvc) {
			 ESP_LOGE(TAG, "Sequencer service 'CAFE' not found!");
		 } else {
			 ESP_LOGW(TAG, "Sequencer service 'CAFE' found!");
			 pChr = pSvc->getCharacteristic("5ADE");
			 if (pChr) {
				 ESP_LOGW(TAG, "Sequencer characteristic 'CAFE / 5ADE' found!");

				 if (pChr->canNotify()) {
					 ESP_LOGW(TAG,
							  "Characteristic can NOTIFY!");
					 if (!pChr->subscribe(true, midiNotifyCB)) {
						 ESP_LOGW(TAG, "Characteristic SUBSCRIPTION NOT SUCCESSFULL!");
						 pSeqClient->disconnect();
						 // return false;
					 } else {
						 ESP_LOGW(TAG,
							 "Characteristic SUBSCRIPTION SUCCESSFULL!");
					 }
				 } else if (pChr->canIndicate()) {
					 /** Send false as first argument to subscribe to
					  * indications instead of notifications */
					 if (!pChr->subscribe(false, notifyCB)) {
						 pSeqClient->disconnect();
						 // return false;
					 }
				 }
			 }
			 ESP_LOGW(TAG, "Sequencer Notifications in service 'CAFE' enabled!");
		 } // service CAFE found 

The output:

Found Our Device
I (9645) NIMBLE: Connected to: 84:cc:a8:0d:3b:72

I (9655) app_main: AFTER connectToSequencer(pSeqClient)
I (9655) app_main: Client available!
>>>>> Outside onConnect: Evaluate MIDI service ('CAFE') <<<<<
Assigned pSvc
W (9855) app_main: Sequencer service 'CAFE' found!
W (10055) app_main: Sequencer characteristic 'CAFE / 5ADE' found!
W (10055) app_main: Characteristic can NOTIFY!
W (10285) app_main: Characteristic SUBSCRIPTION SUCCESSFULL!
W (10285) app_main: Sequencer Notifications in service 'CAFE' enabled!

The code for onConnect, with the subscription mode now moved into it:

	void onConnect(NimBLEClient *pClient) override {
		connected = true;      //global
		pSeqClient = pClient;  //assign to global
		ESP_LOGI("NIMBLE", "Connected to: %s\n",
				 pClient->getPeerAddress().toString().c_str());		
		//update connection status for TFT screen doesnt work here.... why not?
		setConnectionIndicator(GREEN);

		 // move this functionality to the onConnect callback
		printf(">>>>>In onConnect: Evaluate MIDI service ('CAFE') <<<<<\n");
		// look for a service "CAFE"
		pSvc = pSeqClient->getService("CAFE");
		//pSvc = pClient->getService("CAFE");
		printf("Assigned pSvc\n");
		if (!pSvc) {
			ESP_LOGE(TAG, "Sequencer service 'CAFE' not found!");
		} else {
			ESP_LOGW(TAG, "Sequencer service 'CAFE' found!");
			pChr = pSvc->getCharacteristic("5ADE");
			if (pChr) {
				ESP_LOGW(TAG, "Sequencer characteristic 'CAFE / 5ADE' found!");

				if (pChr->canNotify()) {
					ESP_LOGW(TAG, "Characteristic can NOTIFY!");
					if (!pChr->subscribe(true, midiNotifyCB)) {
						ESP_LOGW(
							TAG,
							"Characteristic SUBSCRIPTION NOT SUCCESSFULL!");
						pSeqClient->disconnect();
						// return false;
					} else {
						ESP_LOGW(TAG,
								 "Characteristic SUBSCRIPTION SUCCESSFULL!");
					}
				} else if (pChr->canIndicate()) {
					/** Send false as first argument to subscribe to
					 * indications instead of notifications */
					if (!pChr->subscribe(false, notifyCB)) {
						pSeqClient->disconnect();
						// return false;
					}
				}
			}
			ESP_LOGW(TAG, "Sequencer Notifications in service 'CAFE' enabled!");
		} // service CAFE found
	} //onConnect

The output in the new situation:

Found Our Device
I (9675) NIMBLE: Connected to: 84:cc:a8:0d:3b:72

>>>>>In onConnect: Evaluate MIDI service ('CAFE') <<<<<
....... then it stops inside onConnect .....

...... but execution does not hang ....
I (9685) app_main: AFTER connectToSequencer(pSeqClient)
I (9685) app_main: Client available!

In the new situation, client and server both report a connection, but it looks like the code in onConnect is not executed from pSvc = pSeqClient->getService("CAFE");

Any suggestions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions