bluetooth-terminal
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -7,3 +7,2 @@ /** | ||
* Create preconfigured Bluetooth Terminal instance. | ||
* | ||
* @param {!(number|string)} [serviceUuid=0xFFE0] - Service UUID | ||
@@ -18,6 +17,6 @@ * @param {!(number|string)} [characteristicUuid=0xFFE1] - Characteristic UUID | ||
// Used private variables | ||
this._receiveBuffer = ''; // Buffer containing not separated data | ||
this._receiveBuffer = ''; // Buffer containing not separated data | ||
this._maxCharacteristicValueLength = 20; // Max characteristic value length | ||
this._device = null; // Device object cache | ||
this._characteristic = null; // Characteristic object cache | ||
this._device = null; // Device object cache | ||
this._characteristic = null; // Characteristic object cache | ||
@@ -39,3 +38,2 @@ // Bound functions used to add and remove appropriate event handlers | ||
* Set number or string representing service UUID used. | ||
* | ||
* @param {!(number|string)} uuid - Service UUID | ||
@@ -46,7 +44,7 @@ */ | ||
!(typeof uuid === 'string' || uuid instanceof String)) { | ||
throw 'UUID type is neither a number nor a string'; | ||
throw new Error('UUID type is neither a number nor a string'); | ||
} | ||
if (!uuid) { | ||
throw 'UUID cannot be a null'; | ||
throw new Error('UUID cannot be a null'); | ||
} | ||
@@ -59,3 +57,2 @@ | ||
* Set number or string representing characteristic UUID used. | ||
* | ||
* @param {!(number|string)} uuid - Characteristic UUID | ||
@@ -66,7 +63,7 @@ */ | ||
!(typeof uuid === 'string' || uuid instanceof String)) { | ||
throw 'UUID type is neither a number nor a string'; | ||
throw new Error('UUID type is neither a number nor a string'); | ||
} | ||
if (!uuid) { | ||
throw 'UUID cannot be a null'; | ||
throw new Error('UUID cannot be a null'); | ||
} | ||
@@ -80,3 +77,2 @@ | ||
* device, end of line for example. | ||
* | ||
* @param {string} separator - Receive separator with length equal to one | ||
@@ -87,7 +83,7 @@ * character | ||
if (!(typeof separator === 'string' || separator instanceof String)) { | ||
throw 'Separator type is not a string'; | ||
throw new Error('Separator type is not a string'); | ||
} | ||
if (separator.length !== 1) { | ||
throw 'Separator length must be equal to one character'; | ||
throw new Error('Separator length must be equal to one character'); | ||
} | ||
@@ -101,3 +97,2 @@ | ||
* device, end of line for example. | ||
* | ||
* @param {string} separator - Send separator | ||
@@ -107,7 +102,7 @@ */ | ||
if (!(typeof separator === 'string' || separator instanceof String)) { | ||
throw 'Separator type is not a string'; | ||
throw new Error('Separator type is not a string'); | ||
} | ||
if (separator.length !== 1) { | ||
throw 'Separator length must be equal to one character'; | ||
throw new Error('Separator length must be equal to one character'); | ||
} | ||
@@ -120,3 +115,2 @@ | ||
* Set delay between chunks of long data sending. | ||
* | ||
* @param {!number} delay - Delay in milliseconds | ||
@@ -126,7 +120,7 @@ */ | ||
if (!Number.isInteger(delay)) { | ||
throw 'Delay type is not a number'; | ||
throw new Error('Delay type is not a number'); | ||
} | ||
if (delay <= 0) { | ||
throw 'Delay must be more than a null'; | ||
throw new Error('Delay must be more than a null'); | ||
} | ||
@@ -139,5 +133,4 @@ | ||
* Launch Bluetooth device chooser and connect to the selected device. | ||
* | ||
* @returns {Promise} Promise which will be fulfilled when notifications will | ||
* be started or rejected if something went wrong | ||
* @return {Promise} Promise which will be fulfilled when notifications will | ||
* be started or rejected if something went wrong | ||
*/ | ||
@@ -166,3 +159,2 @@ connect() { | ||
* the connected device, override it to handle incoming data. | ||
* | ||
* @param {string} data - Data | ||
@@ -176,7 +168,5 @@ */ | ||
* Send data to the connected device. | ||
* | ||
* @param {string} data - Data | ||
* | ||
* @returns {Promise} Promise which will be fulfilled when data will be sent | ||
* or rejected if something went wrong | ||
* @return {Promise} Promise which will be fulfilled when data will be sent or | ||
* rejected if something went wrong | ||
*/ | ||
@@ -231,4 +221,3 @@ send(data) { | ||
* Get the connected device name. | ||
* | ||
* @returns {string} Device name or empty string if not connected | ||
* @return {string} Device name or empty string if not connected | ||
*/ | ||
@@ -243,9 +232,20 @@ getDeviceName() { | ||
/** | ||
* Connect to device. | ||
* @param {Object} device | ||
* @return {Promise} | ||
* @private | ||
*/ | ||
_connectToDevice(device) { | ||
return (device ? Promise.resolve(device) : this._requestBluetoothDevice()). | ||
then(device => this._connectDeviceAndCacheCharacteristic(device)). | ||
then(characteristic => this._startNotifications(characteristic)). | ||
catch(error => this._log(error)); | ||
then((device) => this._connectDeviceAndCacheCharacteristic(device)). | ||
then((characteristic) => this._startNotifications(characteristic)). | ||
catch((error) => this._log(error)); | ||
} | ||
/** | ||
* Disconnect from device. | ||
* @param {Object} device | ||
* @private | ||
*/ | ||
_disconnectFromDevice(device) { | ||
@@ -272,2 +272,7 @@ if (!device) { | ||
/** | ||
* Request bluetooth device. | ||
* @return {Promise} | ||
* @private | ||
*/ | ||
_requestBluetoothDevice() { | ||
@@ -279,3 +284,3 @@ this._log('Requesting bluetooth device...'); | ||
}). | ||
then(device => { | ||
then((device) => { | ||
this._log('"' + device.name + '" bluetooth device selected'); | ||
@@ -291,4 +296,11 @@ | ||
/** | ||
* Connect device and cache characteristic. | ||
* @param {Object} device | ||
* @return {Promise} | ||
* @private | ||
*/ | ||
_connectDeviceAndCacheCharacteristic(device) { | ||
if (device.gatt.connected && this._characteristic) { // check remembered characteristic | ||
// Check remembered characteristic | ||
if (device.gatt.connected && this._characteristic) { | ||
return Promise.resolve(this._characteristic); | ||
@@ -300,3 +312,3 @@ } | ||
return device.gatt.connect(). | ||
then(server => { | ||
then((server) => { | ||
this._log('GATT server connected', 'Getting service...'); | ||
@@ -306,3 +318,3 @@ | ||
}). | ||
then(service => { | ||
then((service) => { | ||
this._log('Service found', 'Getting characteristic...'); | ||
@@ -312,3 +324,3 @@ | ||
}). | ||
then(characteristic => { | ||
then((characteristic) => { | ||
this._log('Characteristic found'); | ||
@@ -322,2 +334,8 @@ | ||
/** | ||
* Start notifications. | ||
* @param {Object} characteristic | ||
* @return {Promise} | ||
* @private | ||
*/ | ||
_startNotifications(characteristic) { | ||
@@ -335,2 +353,8 @@ this._log('Starting notifications...'); | ||
/** | ||
* Stop notifications. | ||
* @param {Object} characteristic | ||
* @return {Promise} | ||
* @private | ||
*/ | ||
_stopNotifications(characteristic) { | ||
@@ -348,2 +372,7 @@ this._log('Stopping notifications...'); | ||
/** | ||
* Handle disconnection. | ||
* @param {Object} event | ||
* @private | ||
*/ | ||
_handleDisconnection(event) { | ||
@@ -356,6 +385,11 @@ let device = event.target; | ||
this._connectDeviceAndCacheCharacteristic(device). | ||
then(characteristic => this._startNotifications(characteristic)). | ||
catch(error => this._log(error)); | ||
then((characteristic) => this._startNotifications(characteristic)). | ||
catch((error) => this._log(error)); | ||
} | ||
/** | ||
* Handle characteristic value changed. | ||
* @param {Object} event | ||
* @private | ||
*/ | ||
_handleCharacteristicValueChanged(event) { | ||
@@ -372,4 +406,3 @@ let value = new TextDecoder().decode(event.target.value); | ||
} | ||
} | ||
else { | ||
} else { | ||
this._receiveBuffer += c; | ||
@@ -380,2 +413,8 @@ } | ||
/** | ||
* Write to characteristic. | ||
* @param {Object} characteristic | ||
* @param {string} data | ||
* @private | ||
*/ | ||
_writeToCharacteristic(characteristic, data) { | ||
@@ -385,6 +424,18 @@ characteristic.writeValue(new TextEncoder().encode(data)); | ||
/** | ||
* Log. | ||
* @param {Array} messages | ||
* @private | ||
*/ | ||
_log(...messages) { | ||
console.log(...messages); | ||
console.log(...messages); // eslint-disable-line no-console | ||
} | ||
/** | ||
* Split by length. | ||
* @param {string} string | ||
* @param {number} length | ||
* @return {Array} | ||
* @private | ||
*/ | ||
static _splitByLength(string, length) { | ||
@@ -391,0 +442,0 @@ return string.match(new RegExp('(.|[\r\n]){1,' + length + '}', 'g')); |
{ | ||
"name": "bluetooth-terminal", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "ES6 class for serial communication with Bluetooth Low Energy (Smart) devices", | ||
@@ -26,2 +26,4 @@ "keywords": [ | ||
"coveralls": "^3.0.0", | ||
"eslint": "^4.12.0", | ||
"eslint-config-google": "^0.9.1", | ||
"istanbul": "^0.4.5", | ||
@@ -33,4 +35,5 @@ "mocha": "^4.0.1" | ||
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"lint": "eslint BluetoothTerminal.js test/", | ||
"test": "mocha" | ||
} | ||
} |
@@ -34,5 +34,5 @@ const assert = require('assert'); | ||
it('should throw an error if value is null', () => { | ||
it('should throw an error if value is 0', () => { | ||
assert.throws(() => { | ||
bt.setServiceUuid(null); | ||
bt.setServiceUuid(0); | ||
}); | ||
@@ -62,5 +62,5 @@ }); | ||
it('should throw an error if value is null', () => { | ||
it('should throw an error if value is 0', () => { | ||
assert.throws(() => { | ||
bt.setCharacteristicUuid(null); | ||
bt.setCharacteristicUuid(0); | ||
}); | ||
@@ -70,2 +70,64 @@ }); | ||
describe('setReceiveSeparator', () => { | ||
it('should set string', () => { | ||
const value = '\n'; | ||
bt.setReceiveSeparator(value); | ||
assert.strictEqual(bt._receiveSeparator, value); | ||
}); | ||
it('should throw an error if value is not a string', () => { | ||
assert.throws(() => { | ||
bt.setReceiveSeparator(42); | ||
}); | ||
}); | ||
it('should throw an error if value length is more than one character', | ||
() => { | ||
assert.throws(() => { | ||
bt.setReceiveSeparator('\r\n'); | ||
}); | ||
}); | ||
}); | ||
describe('setSendSeparator', () => { | ||
it('should set string', () => { | ||
const value = '\n'; | ||
bt.setSendSeparator(value); | ||
assert.strictEqual(bt._receiveSeparator, value); | ||
}); | ||
it('should throw an error if value is not a string', () => { | ||
assert.throws(() => { | ||
bt.setSendSeparator(42); | ||
}); | ||
}); | ||
it('should throw an error if value length is more than one character', | ||
() => { | ||
assert.throws(() => { | ||
bt.setSendSeparator('\r\n'); | ||
}); | ||
}); | ||
}); | ||
describe('setSendDelay', () => { | ||
it('should set number', () => { | ||
const value = 42; | ||
bt.setSendDelay(value); | ||
assert.strictEqual(bt._sendDelay, value); | ||
}); | ||
it('should throw an error if value is not a number', () => { | ||
assert.throws(() => { | ||
bt.setSendDelay(NaN); | ||
}); | ||
}); | ||
it('should throw an error if value is less than zero', () => { | ||
assert.throws(() => { | ||
bt.setSendDelay(-42); | ||
}); | ||
}); | ||
}); | ||
describe('splitByLength', function() { | ||
@@ -72,0 +134,0 @@ it('should split string shorter than specified length to one chunk', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
28386
18
486
5