src/windows/bleProxy.js
function (successCallback, errorCallback, deviceHandle, charHandle) {

			winble.logger.logDebug("enableCharacteristicNotification", "deviceHandle='" + deviceHandle + ", charHandle='" + charHandle + "'");

			// Find the requested device in our list of discovered devices
			var device = winble.deviceManager.getDeviceFromHandle(deviceHandle, "enableCharacteristicNotification", errorCallback);
			if (device == null)
				return;

			// Find the requested characteristic in the device's list of characteristics
			var characteristic = winble.deviceManager.getCharacteristicFromHandle(device, charHandle, "enableCharacteristicNotification", errorCallback);
			if (characteristic == null)
				return;

			// Make sure this characteristic supports sending of notifications, notify caller if not.
			if (!(characteristic.gattChar.characteristicProperties & winble.gatt.GattCharacteristicProperties.notify)) {
				var msg = "This characteristic does not support notifications";
				winble.logger.logDebug("enableCharacteristicNotification", msg);
				errorCallback(msg);
				return;
			}

			// Create callback to handle notifications; here we will get the new value into a UTF8 buffer and send it to the caller
			characteristic.onValueChanged = function (args) {
				winble.logger.logDebug("enableCharacteristicNotification", "** Windows called our callback! **");
				var data = Uint8Array(args.characteristicValue.length);
				Windows.Storage.Streams.DataReader.fromBuffer(args.characteristicValue).readBytes(data);
				successCallback(data, { keepCallback: true });
			};

			// Register the callback
			try {
				characteristic.gattChar.addEventListener("valuechanged", characteristic.onValueChanged, false);
			} catch (e) {
				var msg = "Could not add event listener:" + e;
				winble.logger.logError("enableCharacteristicNotification", msg);
				errorCallback(msg);
				return;
			}

			// Tell the characteristic to start sending us notifications.
			// In order to avoid unnecessary communication with the device, first determine if the device is already
			// correctly configured to send notifications. By default ReadClientCharacteristicConfigurationDescriptorAsync
			// will attempt to get the current value from the system cache, so physical communication with the device
			// is not typically required.
			characteristic.gattChar.readClientCharacteristicConfigurationDescriptorAsync().then(
				function (currentDescriptorValue) {

					// No need to configure characteristic to send notifications if it's already configured
					if ((currentDescriptorValue.status !== winble.gatt.GattCommunicationStatus.success) ||
					(currentDescriptorValue.clientCharacteristicConfigurationDescriptor !==
						winble.gatt.GattClientCharacteristicConfigurationDescriptorValue.notify)) {

						// Set the Client Characteristic Configuration Descriptor to enable the device to send
						// notifications when the Characteristic value changes.
						winble.logger.logDebug("enableCharacteristicNotification", "Configuring characteristic for notifications");
						characteristic.gattChar.writeClientCharacteristicConfigurationDescriptorAsync(
							winble.gatt.GattClientCharacteristicConfigurationDescriptorValue.notify).then(
							function (commStatus) {
								if (commStatus == winble.gatt.GattCommunicationStatus.success) {
									winble.logger.logDebug("enableCharacteristicNotification", "gattChar.writeClientCharacteristicConfigurationDescriptorAsync completed with success");
								} else {
									var msg = "Could not configure characteristic for notifications, device unreachable. GattCommunicationStatus = " + commStatus;
									winble.logger.logError("enableCharacteristicNotification", "enableCharacteristicNotification", msg);
									errorCallback(msg);
								}
							});
					} else {
						winble.logger.logDebug("enableCharacteristicNotification", "Characteristic is already configured for notifications");
					}
				});
		}
function (successCallback, errorCallback, deviceHandle, charHandle) {

			winble.logger.logDebug("enableCharacteristicNotification", "deviceHandle='" + deviceHandle + ", charHandle='" + charHandle + "'");

			// Find the requested device in our list of discovered devices
			var device = winble.deviceManager.getDeviceFromHandle(deviceHandle, "enableCharacteristicNotification", errorCallback);
			if (device == null)
				return;

			// Find the requested characteristic in the device's list of characteristics
			var characteristic = winble.deviceManager.getCharacteristicFromHandle(device, charHandle, "enableCharacteristicNotification", errorCallback);
			if (characteristic == null)
				return;

			// Make sure this characteristic supports sending of notifications, notify caller if not.
			if (!(characteristic.gattChar.characteristicProperties & winble.gatt.GattCharacteristicProperties.notify)) {
				var msg = "This characteristic does not support notifications";
				winble.logger.logDebug("enableCharacteristicNotification", msg);
				errorCallback(msg);
				return;
			}

			// Create callback to handle notifications; here we will get the new value into a UTF8 buffer and send it to the caller
			characteristic.onValueChanged = function (args) {
				winble.logger.logDebug("enableCharacteristicNotification", "** Windows called our callback! **");
				var data = Uint8Array(args.characteristicValue.length);
				Windows.Storage.Streams.DataReader.fromBuffer(args.characteristicValue).readBytes(data);
				successCallback(data, { keepCallback: true });
			};

			// Register the callback
			try {
				characteristic.gattChar.addEventListener("valuechanged", characteristic.onValueChanged, false);
			} catch (e) {
				var msg = "Could not add event listener:" + e;
				winble.logger.logError("enableCharacteristicNotification", msg);
				errorCallback(msg);
				return;
			}

			// Tell the characteristic to start sending us notifications.
			// In order to avoid unnecessary communication with the device, first determine if the device is already
			// correctly configured to send notifications. By default ReadClientCharacteristicConfigurationDescriptorAsync
			// will attempt to get the current value from the system cache, so physical communication with the device
			// is not typically required.
			characteristic.gattChar.readClientCharacteristicConfigurationDescriptorAsync().then(
				function (currentDescriptorValue) {

					// No need to configure characteristic to send notifications if it's already configured
					if ((currentDescriptorValue.status !== winble.gatt.GattCommunicationStatus.success) ||
					(currentDescriptorValue.clientCharacteristicConfigurationDescriptor !==
						winble.gatt.GattClientCharacteristicConfigurationDescriptorValue.notify)) {

						// Set the Client Characteristic Configuration Descriptor to enable the device to send
						// notifications when the Characteristic value changes.
						winble.logger.logDebug("enableCharacteristicNotification", "Configuring characteristic for notifications");
						characteristic.gattChar.writeClientCharacteristicConfigurationDescriptorAsync(
							winble.gatt.GattClientCharacteristicConfigurationDescriptorValue.notify).then(
							function (commStatus) {
								if (commStatus == winble.gatt.GattCommunicationStatus.success) {
									winble.logger.logDebug("enableCharacteristicNotification", "gattChar.writeClientCharacteristicConfigurationDescriptorAsync completed with success");
								} else {
									var msg = "Could not configure characteristic for notifications, device unreachable. GattCommunicationStatus = " + commStatus;
									winble.logger.logError("enableCharacteristicNotification", "enableCharacteristicNotification", msg);
									errorCallback(msg);
								}
							});
					} else {
						winble.logger.logDebug("enableCharacteristicNotification", "Characteristic is already configured for notifications");
					}
				});
		}
function ( successCallback , errorCallback , deviceHandle , charHandle ) { winble . logger . logDebug ( "enableCharacteristicNotification" , "deviceHandle='" + deviceHandle + ", charHandle='" + charHandle + "'" ) ; // Find the requested device in our list of discovered devices var device = winble . deviceManager . getDeviceFromHandle ( deviceHandle , "enableCharacteristicNotification" , errorCallback ) ; if ( device == null ) return ; // Find the requested characteristic in the device's list of characteristics var characteristic = winble . deviceManager . getCharacteristicFromHandle ( device , charHandle , "enableCharacteristicNotification" , errorCallback ) ; if ( characteristic == null ) return ; // Make sure this characteristic supports sending of notifications, notify caller if not. if ( ! ( characteristic . gattChar . characteristicProperties & winble . gatt . GattCharacteristicProperties . notify ) ) { var msg = "This characteristic does not support notifications" ; winble . logger . logDebug ( "enableCharacteristicNotification" , msg ) ; errorCallback ( msg ) ; return ; } // Create callback to handle notifications; here we will get the new value into a UTF8 buffer and send it to the caller characteristic . onValueChanged = function ( args ) { winble . logger . logDebug ( "enableCharacteristicNotification" , "** Windows called our callback! **" ) ; var data = Uint8Array ( args . characteristicValue . length ) ; Windows . Storage . Streams . DataReader . fromBuffer ( args . characteristicValue ) . readBytes ( data ) ; successCallback ( data , { keepCallback : true } ) ; } ; // Register the callback try { characteristic . gattChar . addEventListener ( "valuechanged" , characteristic . onValueChanged , false ) ; } catch ( e ) { var msg = "Could not add event listener:" + e ; winble . logger . logError ( "enableCharacteristicNotification" , msg ) ; errorCallback ( msg ) ; return ; } // Tell the characteristic to start sending us notifications. // In order to avoid unnecessary communication with the device, first determine if the device is already // correctly configured to send notifications. By default ReadClientCharacteristicConfigurationDescriptorAsync // will attempt to get the current value from the system cache, so physical communication with the device // is not typically required. characteristic . gattChar . readClientCharacteristicConfigurationDescriptorAsync ( ) . then ( function ( currentDescriptorValue ) { // No need to configure characteristic to send notifications if it's already configured if ( ( currentDescriptorValue . status !== winble . gatt . GattCommunicationStatus . success ) || ( currentDescriptorValue . clientCharacteristicConfigurationDescriptor !== winble . gatt . GattClientCharacteristicConfigurationDescriptorValue . notify ) ) { // Set the Client Characteristic Configuration Descriptor to enable the device to send // notifications when the Characteristic value changes. winble . logger . logDebug ( "enableCharacteristicNotification" , "Configuring characteristic for notifications" ) ; characteristic . gattChar . writeClientCharacteristicConfigurationDescriptorAsync ( winble . gatt . GattClientCharacteristicConfigurationDescriptorValue . notify ) . then ( function ( commStatus ) { if ( commStatus == winble . gatt . GattCommunicationStatus . success ) { winble . logger . logDebug ( "enableCharacteristicNotification" , "gattChar.writeClientCharacteristicConfigurationDescriptorAsync completed with success" ) ; } else { var msg = "Could not configure characteristic for notifications, device unreachable. GattCommunicationStatus = " + commStatus ; winble . logger . logError ( "enableCharacteristicNotification" , "enableCharacteristicNotification" , msg ) ; errorCallback ( msg ) ; } } ) ; } else { winble . logger . logDebug ( "enableCharacteristicNotification" , "Characteristic is already configured for notifications" ) ; } } ) ; }
Called from the enableCharacteristicNotification API; set things up such that the caller's callback function is invoked whenever the value of the specified characteristic changes.
Called from the enableCharacteristicNotification API ; set things up such that the caller s callback function is invoked whenever the value of the specified characteristic changes .