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

Enabling Notification on a Characteristic

Hi, I was trying to send notification from nRF51822 to an android central when notification is enabled on the nordic uart rx characteristic and I discovered that if I enable notification on the characteristic once it has been discovered by the android app, nothing is received by the app but when I enable notification more than once, for example each time, the battery level characteristic is read, then I would start receiving data. I found out that in the source code of nrftoolbox, precisely in the hrs code, notification is enabled on the hrm characteristic each time the battery level characteristic is read:

	public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
		if (status == BluetoothGatt.GATT_SUCCESS) {
			if (characteristic.getUuid().equals(HR_SENSOR_LOCATION_CHARACTERISTIC_UUID)) {
				final String sensorPosition = getBodySensorPosition(characteristic.getValue()[0]);
				//This will send callback to HRSActicity when HR sensor position on body is found in HR device
				mCallbacks.onHRSensorPositionFound(sensorPosition);

				if (mBatteryCharacteritsic != null) {
					readBatteryLevel();
				} else {
					enableHRNotification();
				}
			}
			if (characteristic.getUuid().equals(BATTERY_LEVEL_CHARACTERISTIC)) {
				int batteryValue = characteristic.getValue()[0];
				//This will send callback to HRSActicity when Battery value is received from HR device
				mCallbacks.onBatteryValueReceived(batteryValue);

				enableHRNotification();
			}
		} else {
			mCallbacks.onError(ERROR_READ_CHARACTERISTIC, status);
		}
	}

So why wont notification be sent when it is enable just once?

Parents
  • @Augustio: CCCD should be set just once on every connection if it's not bonded. If the connection is bonded previously, the CCCD value will automatically be restored on both side. No write command needed.

    What you were seeing in the source code came from the way we implemented service discovery and CCCD setting in nRFToolbox. First, we do service discovery, after we done with service discovery, we read the HR sensor location characteristic. When we have the call back "onCharacteristicRead()", if it's the HR_SENSOR_LOCATION_CHARACTERISTIC_UUID returned, we read the battery level characteristic. And only after either we receive battery level (or if battery level is not exist) we will enable CCCD on HR value.

    We don't do further characteristic read in this app. So the CCCD set is only perform once.

    You can also take a look at the nRFUART, where we also only enable CCCD once.

    I would suggest you to capture a sniffer trace, and can find what happened over the air.

Reply
  • @Augustio: CCCD should be set just once on every connection if it's not bonded. If the connection is bonded previously, the CCCD value will automatically be restored on both side. No write command needed.

    What you were seeing in the source code came from the way we implemented service discovery and CCCD setting in nRFToolbox. First, we do service discovery, after we done with service discovery, we read the HR sensor location characteristic. When we have the call back "onCharacteristicRead()", if it's the HR_SENSOR_LOCATION_CHARACTERISTIC_UUID returned, we read the battery level characteristic. And only after either we receive battery level (or if battery level is not exist) we will enable CCCD on HR value.

    We don't do further characteristic read in this app. So the CCCD set is only perform once.

    You can also take a look at the nRFUART, where we also only enable CCCD once.

    I would suggest you to capture a sniffer trace, and can find what happened over the air.

Children
No Data
Related