set: function(v) {
try {
data_ = BluetoothManager_toDOMString(v);
- }
- catch (err) {
+ } catch (err) {
data_ = Converter.toString(v);
}
}
if (native.isSuccess(result)) {
var resultObject = native.getResultObject(result);
resultObject.forEach(function(c) {
- if (!T.isNullOrUndefined(c)) {
- characteristics.push(new BluetoothGATTCharacteristic(c, address_));
- }
+ if (!T.isNullOrUndefined(c)) {
+ characteristics.push(new BluetoothGATTCharacteristic(c, address_));
+ }
});
}
return characteristics;
writable: true
});
+function _getIncludedServicesAndItsComponentsIdsRecursively(service) {
+ var ids = [];
+
+ for (var serviceIndex = 0; serviceIndex < service.services.length; ++serviceIndex) {
+ ids.push(service.services[serviceIndex]._id);
+ ids = ids.concat(
+ _getIncludedServicesAndItsComponentsIdsRecursively(
+ service.services[serviceIndex]
+ )
+ );
+ }
+
+ for (
+ var characteristicIndex = 0;
+ characteristicIndex < service.characteristics.length;
+ ++characteristicIndex
+ ) {
+ ids.push(service.characteristics[characteristicIndex]._id);
+
+ for (
+ var descriptorIndex = 0;
+ descriptorIndex <
+ service.characteristics[characteristicIndex].descriptors.length;
+ ++descriptorIndex
+ ) {
+ ids.push(
+ service.characteristics[characteristicIndex].descriptors[descriptorIndex]
+ ._id
+ );
+ }
+ }
+
+ return ids;
+}
+
+var BluetoothGATTServer_valid_unregisterService_errors = [
+ 'InvalidStateError',
+ 'AbortError'
+];
+var BluetoothGATTServer_valid_unregisterService_exceptions = [
+ 'TypeMismatchError',
+ 'SecurityError'
+];
+
+BluetoothGATTServerService.prototype.unregister = function() {
+ var args = AV.validateArgs(arguments, [
+ {
+ name: 'successCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ },
+ {
+ name: 'errorCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }
+ ]);
+
+ var serviceIndex = _BluetoothGATTServerServices.findIndex(
+ function(service) {
+ return service._id === this._id;
+ }.bind(this)
+ );
+
+ if (serviceIndex === -1) {
+ throw new WebAPIException(
+ 'AbortError',
+ 'The service is not registered in the local GATT server'
+ );
+ }
+
+ function removeFromJSArrayAndCallSuccessCb() {
+ _BluetoothGATTServerServices.splice(serviceIndex, 1);
+ native.callIfPossible(args.successCallback);
+ }
+
+ if (!_BluetoothGATTServerServicesRegisteredInNativeLayer[this._id]) {
+ removeFromJSArrayAndCallSuccessCb();
+ return;
+ }
+
+ var callback = function(result) {
+ if (native.isFailure(result)) {
+ native.callIfPossible(
+ args.errorCallback,
+ native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_unregisterService_errors,
+ AbortError
+ )
+ );
+ } else {
+ delete _BluetoothGATTServerServicesRegisteredInNativeLayer[this._id];
+ removeFromJSArrayAndCallSuccessCb();
+ }
+ }.bind(this);
+
+ var callArgs = {
+ _id: this._id,
+ idsToRemoveFromNativeLayer: _getIncludedServicesAndItsComponentsIdsRecursively(
+ this
+ )
+ };
+ var result = native.call('BluetoothGATTServerUnregisterService', callArgs, callback);
+
+ if (native.isFailure(result)) {
+ throw native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_unregisterService_errors,
+ AbortError
+ );
+ }
+};
+
var numberArrayToByteArray = function(array) {
var d = [];
//class BluetoothGATTCharacteristic ///////////////////////////
var BluetoothGATTCharacteristic = function(data, address) {
if (!T.isObject(data)) {
- return null;
+ return null;
}
var address_ = address;
var handle_ = data.handle;
var descriptors_ = data.descriptors.map(function(descriptor_data) {
return new BluetoothGATTDescriptor(descriptor_data, address_);
- });
+ });
var isBroadcast_ = data.isBroadcast;
var hasExtendedProperties_ = data.hasExtendedProperties;
var isNotify_ = data.isNotify;
var callArgs = { handle: handle_, address: address_ };
- var result = native.call('BluetoothGATTClientServiceReadValue', callArgs, callback);
+ var result = native.call(
+ 'BluetoothGATTClientServiceReadValue',
+ callArgs,
+ callback
+ );
if (native.isFailure(result)) {
throw native.getErrorObject(result);
this.writeValue = function(value, successCallback, errorCallback) {
privUtils_.log('Entered BluetoothGATTCharacteristic.writeValue()');
- var args = AV.validateArgs([successCallback, errorCallback], [
- {
- name: 'successCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- },
- {
- name: 'errorCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- }
- ]);
+ var args = AV.validateArgs(
+ [successCallback, errorCallback],
+ [
+ {
+ name: 'successCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ },
+ {
+ name: 'errorCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }
+ ]
+ );
var callback = function(result) {
if (native.isFailure(result)) {
address: address_
};
- var result = native.call('BluetoothGATTClientServiceWriteValue', callArgs, callback);
+ var result = native.call(
+ 'BluetoothGATTClientServiceWriteValue',
+ callArgs,
+ callback
+ );
if (native.isFailure(result)) {
throw native.getErrorObject(result);
);
};
- this.removeValueChangeListener = function() { /* Intended no operation */ };
+ this.removeValueChangeListener = function() {
+ /* Intended no operation */
+ };
};
-BluetoothGATTServerCharacteristic.prototype = Object.create(BluetoothGATTCharacteristic.prototype);
+BluetoothGATTServerCharacteristic.prototype = Object.create(
+ BluetoothGATTCharacteristic.prototype
+);
Object.defineProperty(BluetoothGATTServerCharacteristic.prototype, 'constructor', {
value: BluetoothGATTServerCharacteristic,
var callArgs = { handle: handle_, address: address_ };
- var result = native.call('BluetoothGATTClientServiceReadValue', callArgs, callback);
+ var result = native.call(
+ 'BluetoothGATTClientServiceReadValue',
+ callArgs,
+ callback
+ );
if (native.isFailure(result)) {
throw native.getErrorObject(result);
this.writeValue = function(value, successCallback, errorCallback) {
privUtils_.log('Entered BluetoothGATTDescriptor.writeValue()');
- var args = AV.validateArgs([successCallback, errorCallback], [
- {
- name: 'successCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- },
- {
- name: 'errorCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- }
- ]);
+ var args = AV.validateArgs(
+ [successCallback, errorCallback],
+ [
+ {
+ name: 'successCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ },
+ {
+ name: 'errorCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }
+ ]
+ );
var callback = function(result) {
if (native.isFailure(result)) {
address: address_
};
- var result = native.call('BluetoothGATTClientServiceWriteValue', callArgs, callback);
+ var result = native.call(
+ 'BluetoothGATTClientServiceWriteValue',
+ callArgs,
+ callback
+ );
if (native.isFailure(result)) {
throw native.getErrorObject(result);
};
};
-BluetoothGATTServerDescriptor.prototype = Object.create(BluetoothGATTDescriptor.prototype);
+BluetoothGATTServerDescriptor.prototype = Object.create(
+ BluetoothGATTDescriptor.prototype
+);
Object.defineProperty(BluetoothGATTServerDescriptor.prototype, 'constructor', {
value: BluetoothGATTServerDescriptor,
var _allSucceeded = true;
var _error;
- this.successCallback = function (){
- _callbacksNum--;
+ this.successCallback = function() {
+ _callbacksNum--;
- if (!_callbacksNum) {
- if (_allSucceeded) {
- onAllSucceeded();
- } else {
- onFailure(_error);
+ if (!_callbacksNum) {
+ if (_allSucceeded) {
+ onAllSucceeded();
+ } else {
+ onFailure(_error);
+ }
}
- }
};
this.errorCallback = function(error) {
- _callbacksNum--;
- _allSucceeded = false;
- _error = error;
+ _callbacksNum--;
+ _allSucceeded = false;
+ _error = error;
- if (!_callbacksNum) {
- onFailure(_error);
- }
+ if (!_callbacksNum) {
+ onFailure(_error);
+ }
};
};
-var BluetoothGATTServer_valid_start_errors = ['InvalidStateError', 'NotSupportedError', 'AbortError'];
-var BluetoothGATTServer_valid_start_exceptions = ['InvalidStateError', 'TypeMismatchError', 'SecurityError'];
+var BluetoothGATTServer_valid_start_errors = [
+ 'InvalidStateError',
+ 'NotSupportedError',
+ 'AbortError'
+];
+var BluetoothGATTServer_valid_start_exceptions = [
+ 'InvalidStateError',
+ 'TypeMismatchError',
+ 'SecurityError'
+];
BluetoothGATTServer.prototype.start = function() {
privUtils_.log('Entered BluetoothGATTServer.start()');
var startServerCallback = function(result) {
if (native.isFailure(result)) {
- native.callIfPossible(args.errorCallback,
- native.getErrorObjectAndValidate(result,
- BluetoothGATTServer_valid_start_errors, AbortError));
+ native.callIfPossible(
+ args.errorCallback,
+ native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_start_errors,
+ AbortError
+ )
+ );
} else {
native.callIfPossible(args.successCallback);
}
};
if (servicesUnregisteredInNativeLayer.length) {
- var registerServiceCallbacksAggregator = new ResultCallbacksAggregator(servicesUnregisteredInNativeLayer.length,
- function onAllSucceeded() {
-
- var result = native.call('BluetoothGATTServerStart', {}, startServerCallback);
-
- if (native.isFailure(result)) {
- throw native.getErrorObjectAndValidate(result, BluetoothGATTServer_valid_start_exceptions, AbortError);
- }
- },
- function onFailure(error) {
- native.callIfPossible(args.errorCallback,
- native.getErrorObjectAndValidate(error,
- BluetoothGATTServer_valid_start_errors, AbortError));
- });
-
- var registerServicesCallback = function(result) {
- if (native.isFailure(result)) {
- registerServiceCallbacksAggregator.errorCallback(
- native.getErrorObjectAndValidate(result,
- BluetoothGATTServer_valid_start_errors, AbortError));
- } else {
- registerServiceCallbacksAggregator.successCallback();
- }
- };
-
- for (var i = 0; i < servicesUnregisteredInNativeLayer.length; ++i) {
- var result = native.call('BluetoothGATTServerRegisterService', servicesUnregisteredInNativeLayer[i], registerServicesCallback);
- if (native.isFailure(result)) {
- throw native.getErrorObjectAndValidate(
- result,
- BluetoothGATTServer_valid_registerService_exceptions,
- AbortError
+ var registerServiceCallbacksAggregator = new ResultCallbacksAggregator(
+ servicesUnregisteredInNativeLayer.length,
+ function onAllSucceeded() {
+ var result = native.call(
+ 'BluetoothGATTServerStart',
+ {},
+ startServerCallback
+ );
+
+ if (native.isFailure(result)) {
+ throw native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_start_exceptions,
+ AbortError
+ );
+ }
+ },
+ function onFailure(error) {
+ native.callIfPossible(
+ args.errorCallback,
+ native.getErrorObjectAndValidate(
+ error,
+ BluetoothGATTServer_valid_start_errors,
+ AbortError
+ )
+ );
+ }
+ );
+
+ var registerServicesCallback = function(result) {
+ if (native.isFailure(result)) {
+ registerServiceCallbacksAggregator.errorCallback(
+ native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_start_errors,
+ AbortError
+ )
+ );
+ } else {
+ registerServiceCallbacksAggregator.successCallback();
+ }
+ };
+
+ for (var i = 0; i < servicesUnregisteredInNativeLayer.length; ++i) {
+ var result = native.call(
+ 'BluetoothGATTServerRegisterService',
+ servicesUnregisteredInNativeLayer[i],
+ registerServicesCallback
);
+ if (native.isFailure(result)) {
+ throw native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_registerService_exceptions,
+ AbortError
+ );
+ }
}
- }
} else {
var result = native.call('BluetoothGATTServerStart', {}, startServerCallback);
if (native.isFailure(result)) {
- throw native.getErrorObjectAndValidate(result, BluetoothGATTServer_valid_start_exceptions, AbortError);
+ throw native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_start_exceptions,
+ AbortError
+ );
}
}
-}
+};
-var BluetoothGATTServer_valid_stop_errors = ['InvalidStateError', 'NotSupportedError', 'AbortError'];
-var BluetoothGATTServer_valid_stop_exceptions = ['InvalidStateError', 'TypeMismatchError', 'SecurityError'];
+var BluetoothGATTServer_valid_stop_errors = [
+ 'InvalidStateError',
+ 'NotSupportedError',
+ 'AbortError'
+];
+var BluetoothGATTServer_valid_stop_exceptions = [
+ 'InvalidStateError',
+ 'TypeMismatchError',
+ 'SecurityError'
+];
BluetoothGATTServer.prototype.stop = function() {
privUtils_.log('Entered BluetoothGATTServer.stop()');
var callback = function(result) {
if (native.isFailure(result)) {
- native.callIfPossible(args.errorCallback,
- native.getErrorObjectAndValidate(result,
- BluetoothGATTServer_valid_stop_errors, AbortError));
+ native.callIfPossible(
+ args.errorCallback,
+ native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_stop_errors,
+ AbortError
+ )
+ );
} else {
_BluetoothGATTServerServicesRegisteredInNativeLayer = {};
native.callIfPossible(args.successCallback);
var result = native.call('BluetoothGATTServerStop', {}, callback);
if (native.isFailure(result)) {
- throw native.getErrorObjectAndValidate(result, BluetoothGATTServer_valid_stop_exceptions, AbortError);
+ throw native.getErrorObjectAndValidate(
+ result,
+ BluetoothGATTServer_valid_stop_exceptions,
+ AbortError
+ );
}
-}
+};
var BluetoothGATTServer_valid_getConnectionMtu_errors = ['InvalidStateError', 'NotSupportedError', 'UnknownError'];
var BluetoothGATTServer_valid_getConnectionMtu_exceptions = ['TypeMismatchError', 'SecurityError'];
if (hexString.startsWith('0x')) {
hexString = hexString.substring(2);
}
- if (!(/^[0-9a-f]+$/.test(hexString))) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, 'Given string is not hexadecimal value');
+ if (!/^[0-9a-f]+$/.test(hexString)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Given string is not hexadecimal value'
+ );
}
if ('0' === hexString) {
- return '00';
+ return '00';
}
- if (1 === hexString.length%2) {
+ if (1 === hexString.length % 2) {
// to save consistency with BluetoothLEManufacturerData, last character is omitted
- hexString = hexString.replace(/.$/,'');
+ hexString = hexString.replace(/.$/, '');
}
return hexString;
-}
+};
var BluetoothManager_HexStringToUint8Array = function(hexString) {
hexString = BluetoothManager_checkAndNormalizeHexString(hexString);
if (0 === hexString.length) {
return new Uint8Array([]);
}
- var data = hexString.match(/[0-9a-f]{2}/g).map(function(byte) {return parseInt(byte, 16)});
+ var data = hexString.match(/[0-9a-f]{2}/g).map(function(byte) {
+ return parseInt(byte, 16);
+ });
return new Uint8Array(data);
-}
+};
var BluetoothManager_byteArrayToHexString = function(bytes) {
if (0 == bytes.length) {
return '';
}
- return '0x' + Array.prototype.map.call(bytes, function(byte) {
- return ('0' + (byte & 0xFF).toString(16)).slice(-2);
- }).join('');
-}
+ return (
+ '0x' +
+ Array.prototype.map
+ .call(bytes, function(byte) {
+ return ('0' + (byte & 0xff).toString(16)).slice(-2);
+ })
+ .join('')
+ );
+};
var BluetoothManager_toByteArray = function(data) {
if (data && String === data.constructor) {
} else {
try {
data = numberArrayToByteArray(data);
- }
- catch(err) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, 'argument is not a valid Bytes type');
+ } catch (err) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'argument is not a valid Bytes type'
+ );
}
}
return data;
return BluetoothManager_HexStringToUint8Array(data);
} else {
try {
- data = new Uint8Array(numberArrayToByteArray(data));
- }
- catch(err) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR, 'argument is not a valid Bytes type');
+ data = new Uint8Array(numberArrayToByteArray(data));
+ } catch (err) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'argument is not a valid Bytes type'
+ );
}
}
return data;