Socket
Socket
Sign inDemoInstall

webbluetooth

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webbluetooth - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

docs/classes/emitter.html

48

lib/adapter.js

@@ -29,4 +29,12 @@ "use strict";

const noble = require("noble");
/**
* @hidden
*/
class NobleAdapter {
constructor() {
this.deviceHandles = {};
this.serviceHandles = {};
this.characteristicHandles = {};
this.descriptorHandles = {};
this.charNotifies = {};
this.foundFn = null;

@@ -38,3 +46,3 @@ this.initialised = false;

return completeFn();
noble.on("discover", this.discover);
noble.on("discover", this.discover.bind(this));
this.initialised = true;

@@ -131,3 +139,3 @@ completeFn();

else
stateCB(noble.state);
stateCB.call(this, noble.state);
});

@@ -142,3 +150,3 @@ }

baseDevice.once("connect", connectFn);
baseDevice.once("disconnect", function () {
baseDevice.once("disconnect", () => {
this.serviceHandles = {};

@@ -149,3 +157,3 @@ this.characteristicHandles = {};

disconnectFn();
}.bind(this));
});
baseDevice.connect(this.checkForError(errorFn));

@@ -158,5 +166,5 @@ }

const baseDevice = this.deviceHandles[handle];
baseDevice.discoverServices([], this.checkForError(errorFn, function (services) {
baseDevice.discoverServices([], this.checkForError(errorFn, services => {
const discovered = [];
services.forEach(function (serviceInfo) {
services.forEach(serviceInfo => {
const serviceUUID = helpers_1.getCanonicalUUID(serviceInfo.uuid);

@@ -172,9 +180,9 @@ if (serviceUUIDs.length === 0 || serviceUUIDs.indexOf(serviceUUID) >= 0) {

}
}, this);
});
completeFn(discovered);
}.bind(this)));
}));
}
discoverIncludedServices(handle, serviceUUIDs, completeFn, errorFn) {
const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverIncludedServices([], this.checkForError(errorFn, function (services) {
serviceInfo.discoverIncludedServices([], this.checkForError(errorFn, services => {
const discovered = [];

@@ -194,9 +202,9 @@ services.forEach(service => {

completeFn(discovered);
}.bind(this)));
}));
}
discoverCharacteristics(handle, characteristicUUIDs, completeFn, errorFn) {
const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverCharacteristics([], this.checkForError(errorFn, function (characteristics) {
serviceInfo.discoverCharacteristics([], this.checkForError(errorFn, characteristics => {
const discovered = [];
characteristics.forEach(function (characteristicInfo) {
characteristics.forEach(characteristicInfo => {
const charUUID = helpers_1.getCanonicalUUID(characteristicInfo.uuid);

@@ -221,3 +229,3 @@ if (characteristicUUIDs.length === 0 || characteristicUUIDs.indexOf(charUUID) >= 0) {

});
characteristicInfo.on("data", function (data, isNotification) {
characteristicInfo.on("data", (data, isNotification) => {
if (isNotification === true && typeof this.charNotifies[charUUID] === "function") {

@@ -227,13 +235,13 @@ const dataView = this.bufferToDataView(data);

}
}.bind(this));
});
}
}, this);
completeFn(discovered);
}.bind(this)));
}));
}
discoverDescriptors(handle, descriptorUUIDs, completeFn, errorFn) {
const characteristicInfo = this.characteristicHandles[handle];
characteristicInfo.discoverDescriptors(this.checkForError(errorFn, function (descriptors) {
characteristicInfo.discoverDescriptors(this.checkForError(errorFn, descriptors => {
const discovered = [];
descriptors.forEach(function (descriptorInfo) {
descriptors.forEach(descriptorInfo => {
const descUUID = helpers_1.getCanonicalUUID(descriptorInfo.uuid);

@@ -251,3 +259,3 @@ if (descriptorUUIDs.length === 0 || descriptorUUIDs.indexOf(descUUID) >= 0) {

completeFn(discovered);
}.bind(this)));
}));
}

@@ -302,3 +310,7 @@ readCharacteristic(handle, completeFn, errorFn) {

exports.NobleAdapter = NobleAdapter;
/**
* @hidden
*/
exports.adapter = new NobleAdapter();
//# sourceMappingURL=adapter.js.map

@@ -27,13 +27,23 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
class BluetoothRemoteGATTCharacteristic {
}
exports.BluetoothRemoteGATTCharacteristic = BluetoothRemoteGATTCharacteristic;
/*
// BluetoothRemoteGATTCharacteristic Object
var BluetoothRemoteGATTCharacteristic = function(properties) {
const emitter_1 = require("./emitter");
const descriptor_1 = require("./descriptor");
const helpers_1 = require("./helpers");
const adapter_1 = require("./adapter");
class BluetoothRemoteGATTCharacteristic extends emitter_1.Emitter {
/**
* @hidden
*/
constructor(init) {
super();
/**
* @hidden
*/
this._handle = null;
/**
* @hidden
*/
this._descriptors = null;
this.service = null;
this.uuid = null;
this.value = null;
this.properties = {

@@ -50,94 +60,115 @@ broadcast: false,

};
this.value = null;
mergeDictionary(this, properties);
};
BluetoothRemoteGATTCharacteristic.prototype.getDescriptor = function(descriptorUUID) {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("getDescriptor error: device not connected");
if (!descriptorUUID) return reject("getDescriptor error: no descriptor specified");
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
}
getDescriptor(descriptorUUID) {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("getDescriptor error: device not connected");
if (!descriptorUUID)
return reject("getDescriptor error: no descriptor specified");
this.getDescriptors(descriptorUUID)
.then(function(descriptors) {
if (descriptors.length !== 1) return reject("getDescriptor error: descriptor not found");
.then(descriptors => {
if (descriptors.length !== 1)
return reject("getDescriptor error: descriptor not found");
resolve(descriptors[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getDescriptor error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.getDescriptors = function(descriptorUUID) {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("getDescriptors error: device not connected");
});
}
getDescriptors(descriptorUUID) {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("getDescriptors error: device not connected");
function complete() {
if (!descriptorUUID) return resolve(this._descriptors);
var filtered = this._descriptors.filter(function(descriptor) {
return (descriptor.uuid === helpers.getDescriptorUUID(descriptorUUID));
if (!descriptorUUID)
return resolve(this._descriptors);
const filtered = this._descriptors.filter(descriptor => {
return (descriptor.uuid === helpers_1.getDescriptorUUID(descriptorUUID));
});
if (filtered.length !== 1) return reject("getDescriptors error: descriptor not found");
if (filtered.length !== 1)
return reject("getDescriptors error: descriptor not found");
resolve(filtered);
}
if (this._descriptors) return complete.call(this);
adapter.discoverDescriptors(this._handle, [], function(descriptors) {
this._descriptors = descriptors.map(function(descriptorInfo) {
if (this._descriptors)
return complete.call(this);
adapter_1.adapter.discoverDescriptors(this._handle, [], descriptors => {
this._descriptors = descriptors.map(descriptorInfo => {
descriptorInfo.characteristic = this;
return new BluetoothRemoteGATTDescriptor(descriptorInfo);
}.bind(this));
return new descriptor_1.BluetoothRemoteGATTDescriptor(descriptorInfo);
});
complete.call(this);
}.bind(this), wrapReject(reject, "getDescriptors error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.readValue = function() {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("readValue error: device not connected");
adapter.readCharacteristic(this._handle, function(dataView) {
}, error => {
reject(`getDescriptors error: ${error}`);
});
});
}
readValue() {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("readValue error: device not connected");
adapter_1.adapter.readCharacteristic(this._handle, dataView => {
this.value = dataView;
resolve(dataView);
this.dispatchEvent({ type: "characteristicvaluechanged", bubbles: true });
}.bind(this), wrapReject(reject, "readValue error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.writeValue = function(bufferSource) {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("writeValue error: device not connected");
var arrayBuffer = bufferSource.buffer || bufferSource;
var dataView = new DataView(arrayBuffer);
adapter.writeCharacteristic(this._handle, dataView, function() {
this.emit(BluetoothRemoteGATTCharacteristic.EVENT_CHANGED);
}, error => {
reject(`readValue error: ${error}`);
});
});
}
writeValue(bufferSource) {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("writeValue error: device not connected");
function isView(source) {
return source.buffer !== undefined;
}
const arrayBuffer = isView(bufferSource) ? bufferSource.buffer : bufferSource;
const dataView = new DataView(arrayBuffer);
adapter_1.adapter.writeCharacteristic(this._handle, dataView, () => {
this.value = dataView;
resolve();
}.bind(this), wrapReject(reject, "writeValue error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.startNotifications = function() {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("startNotifications error: device not connected");
adapter.enableNotify(this._handle, function(dataView) {
}, error => {
reject(`writeValue error: ${error}`);
});
});
}
startNotifications() {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("startNotifications error: device not connected");
adapter_1.adapter.enableNotify(this._handle, dataView => {
this.value = dataView;
this.dispatchEvent({ type: "characteristicvaluechanged", bubbles: true });
}.bind(this), function() {
this.emit(BluetoothRemoteGATTCharacteristic.EVENT_CHANGED);
}, () => {
resolve(this);
}.bind(this), wrapReject(reject, "startNotifications error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.stopNotifications = function() {
return new Promise(function(resolve, reject) {
if (!this.service.device.gatt.connected) return reject("stopNotifications error: device not connected");
adapter.disableNotify(this._handle, function() {
}, error => {
reject(`startNotifications error: ${error}`);
});
});
}
stopNotifications() {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected)
return reject("stopNotifications error: device not connected");
adapter_1.adapter.disableNotify(this._handle, () => {
resolve(this);
}.bind(this), wrapReject(reject, "stopNotifications error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.addEventListener = createListenerFn([
"characteristicvaluechanged"
]);
BluetoothRemoteGATTCharacteristic.prototype.removeEventListener = removeEventListener;
BluetoothRemoteGATTCharacteristic.prototype.dispatchEvent = dispatchEvent;
*/
}, error => {
reject(`stopNotifications error: ${error}`);
});
});
}
}
/**
* Characteristic Value Changed event
* @event
*/
BluetoothRemoteGATTCharacteristic.EVENT_CHANGED = "characteristicvaluechanged";
exports.BluetoothRemoteGATTCharacteristic = BluetoothRemoteGATTCharacteristic;
//# sourceMappingURL=characteristic.js.map

@@ -27,40 +27,53 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const adapter_1 = require("./adapter");
class BluetoothRemoteGATTDescriptor {
}
exports.BluetoothRemoteGATTDescriptor = BluetoothRemoteGATTDescriptor;
/*
// BluetoothRemoteGATTDescriptor Object
var BluetoothRemoteGATTDescriptor = function(properties) {
/**
* @hidden
*/
constructor(init) {
/**
* @hidden
*/
this._handle = null;
this.characteristic = null;
this.uuid = null;
this.value = null;
mergeDictionary(this, properties);
};
BluetoothRemoteGATTDescriptor.prototype.readValue = function() {
return new Promise(function(resolve, reject) {
if (!this.characteristic.service.device.gatt.connected) return reject("readValue error: device not connected");
adapter.readDescriptor(this._handle, function(dataView) {
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
}
readValue() {
return new Promise((resolve, reject) => {
if (!this.characteristic.service.device.gatt.connected)
return reject("readValue error: device not connected");
adapter_1.adapter.readDescriptor(this._handle, dataView => {
this.value = dataView;
resolve(dataView);
}.bind(this), wrapReject(reject, "readValue error"));
}.bind(this));
};
BluetoothRemoteGATTDescriptor.prototype.writeValue = function(bufferSource) {
return new Promise(function(resolve, reject) {
if (!this.characteristic.service.device.gatt.connected) return reject("writeValue error: device not connected");
var arrayBuffer = bufferSource.buffer || bufferSource;
var dataView = new DataView(arrayBuffer);
adapter.writeDescriptor(this._handle, dataView, function() {
}, error => {
reject(`readValue error: ${error}`);
});
});
}
writeValue(bufferSource) {
return new Promise((resolve, reject) => {
if (!this.characteristic.service.device.gatt.connected)
return reject("writeValue error: device not connected");
function isView(source) {
return source.buffer !== undefined;
}
const arrayBuffer = isView(bufferSource) ? bufferSource.buffer : bufferSource;
const dataView = new DataView(arrayBuffer);
adapter_1.adapter.writeDescriptor(this._handle, dataView, () => {
this.value = dataView;
resolve();
}.bind(this), wrapReject(reject, "writeValue error"));
}.bind(this));
};
*/
}, error => {
reject(`writeValue error: ${error}`);
});
});
}
}
exports.BluetoothRemoteGATTDescriptor = BluetoothRemoteGATTDescriptor;
//# sourceMappingURL=descriptor.js.map

@@ -27,6 +27,17 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const emitter_1 = require("./emitter");
const server_1 = require("./server");
class BluetoothDevice {
class BluetoothDevice extends emitter_1.Emitter {
/**
* @hidden
*/
constructor(init) {
super();
/**
* @hidden
*/
this._handle = null;
/**
* @hidden
*/
this._allowedServices = [];

@@ -52,31 +63,9 @@ this.id = "unknown";

}
/**
* Server Disconnected event
* @event
*/
BluetoothDevice.EVENT_DISCONNECTED = "gattserverdisconnected";
exports.BluetoothDevice = BluetoothDevice;
/*
// BluetoothDevice Object
var BluetoothDevice = function(properties) {
this._handle = null;
this._allowedServices = [];
this.id = "unknown";
this.name = null;
this.adData = {
appearance: null,
txPower: null,
rssi: null,
manufacturerData: new Map(),
serviceData: new Map()
};
this.gatt = new BluetoothRemoteGATTServer();
this.gatt.device = this;
this.uuids = [];
mergeDictionary(this, properties);
};
BluetoothDevice.prototype.addEventListener = createListenerFn([
"gattserverdisconnected",
]);
BluetoothDevice.prototype.removeEventListener = removeEventListener;
BluetoothDevice.prototype.dispatchEvent = dispatchEvent;
*/
//# sourceMappingURL=device.js.map

@@ -27,4 +27,11 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const device_1 = require("./device");
const service_1 = require("./service");
const helpers_1 = require("./helpers");
const adapter_1 = require("./adapter");
class BluetoothRemoteGATTServer {
constructor() {
/**
* @hidden
*/
this._services = null;

@@ -34,69 +41,69 @@ this.device = null;

}
}
exports.BluetoothRemoteGATTServer = BluetoothRemoteGATTServer;
/*
// BluetoothRemoteGATTServer Object
var BluetoothRemoteGATTServer = function() {
this._services = null;
this.device = null;
this.connected = false;
};
BluetoothRemoteGATTServer.prototype.connect = function() {
return new Promise(function(resolve, reject) {
if (this.connected) return reject("connect error: device already connected");
adapter.connect(this.device._handle, function() {
connect() {
return new Promise((resolve, reject) => {
if (this.connected)
return reject("connect error: device already connected");
adapter_1.adapter.connect(this.device._handle, () => {
this.connected = true;
resolve(this);
}.bind(this), function() {
}, () => {
this._services = null;
this.connected = false;
this.device.dispatchEvent({ type: "gattserverdisconnected", bubbles: true });
}.bind(this), wrapReject(reject, "connect error"));
}.bind(this));
};
BluetoothRemoteGATTServer.prototype.disconnect = function() {
adapter.disconnect(this.device._handle);
this.device.emit(device_1.BluetoothDevice.EVENT_DISCONNECTED);
}, error => {
reject(`connect Error: ${error}`);
});
});
}
disconnect() {
adapter_1.adapter.disconnect(this.device._handle);
this.connected = false;
};
BluetoothRemoteGATTServer.prototype.getPrimaryService = function(serviceUUID) {
return new Promise(function(resolve, reject) {
if (!this.connected) return reject("getPrimaryService error: device not connected");
if (!serviceUUID) return reject("getPrimaryService error: no service specified");
}
getPrimaryService(serviceUUID) {
return new Promise((resolve, reject) => {
if (!this.connected)
return reject("getPrimaryService error: device not connected");
if (!serviceUUID)
return reject("getPrimaryService error: no service specified");
this.getPrimaryServices(serviceUUID)
.then(function(services) {
if (services.length !== 1) return reject("getPrimaryService error: service not found");
.then(services => {
if (services.length !== 1)
return reject("getPrimaryService error: service not found");
resolve(services[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getPrimaryService error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTServer.prototype.getPrimaryServices = function(serviceUUID) {
return new Promise(function(resolve, reject) {
if (!this.connected) return reject("getPrimaryServices error: device not connected");
});
}
getPrimaryServices(serviceUUID) {
return new Promise((resolve, reject) => {
if (!this.connected)
return reject("getPrimaryServices error: device not connected");
function complete() {
if (!serviceUUID) return resolve(this._services);
var filtered = this._services.filter(function(service) {
return (service.uuid === helpers.getServiceUUID(serviceUUID));
if (!serviceUUID)
return resolve(this._services);
const filtered = this._services.filter(service => {
return (service.uuid === helpers_1.getServiceUUID(serviceUUID));
});
if (filtered.length !== 1) return reject("getPrimaryServices error: service not found");
if (filtered.length !== 1)
return reject("getPrimaryServices error: service not found");
resolve(filtered);
}
if (this._services) return complete.call(this);
adapter.discoverServices(this.device._handle, this.device._allowedServices, function(services) {
this._services = services.map(function(serviceInfo) {
if (this._services)
return complete.call(this);
adapter_1.adapter.discoverServices(this.device._handle, this.device._allowedServices, services => {
this._services = services.map(serviceInfo => {
serviceInfo.device = this.device;
return new BluetoothRemoteGATTService(serviceInfo);
}.bind(this));
return new service_1.BluetoothRemoteGATTService(serviceInfo);
});
complete.call(this);
}.bind(this), wrapReject(reject, "getPrimaryServices error"));
}.bind(this));
};
*/
}, error => {
reject(`getPrimaryServices error: ${error}`);
});
});
}
}
exports.BluetoothRemoteGATTServer = BluetoothRemoteGATTServer;
//# sourceMappingURL=server.js.map

@@ -27,102 +27,140 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
class BluetoothRemoteGATTService {
}
exports.BluetoothRemoteGATTService = BluetoothRemoteGATTService;
/*
// BluetoothRemoteGATTService Object
var BluetoothRemoteGATTService = function(properties) {
const emitter_1 = require("./emitter");
const characteristic_1 = require("./characteristic");
const helpers_1 = require("./helpers");
const adapter_1 = require("./adapter");
class BluetoothRemoteGATTService extends emitter_1.Emitter {
/**
* @hidden
*/
constructor(init) {
super();
/**
* @hidden
*/
this._handle = null;
/**
* @hidden
*/
this._services = null;
/**
* @hidden
*/
this._characteristics = null;
this.device = null;
this.uuid = null;
this.isPrimary = false;
mergeDictionary(this, properties);
this.dispatchEvent({ type: "serviceadded", bubbles: true });
};
BluetoothRemoteGATTService.prototype.getCharacteristic = function(characteristicUUID) {
return new Promise(function(resolve, reject) {
if (!this.device.gatt.connected) return reject("getCharacteristic error: device not connected");
if (!characteristicUUID) return reject("getCharacteristic error: no characteristic specified");
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
this.emit(BluetoothRemoteGATTService.EVENT_ADDED);
}
getCharacteristic(characteristicUUID) {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected)
return reject("getCharacteristic error: device not connected");
if (!characteristicUUID)
return reject("getCharacteristic error: no characteristic specified");
this.getCharacteristics(characteristicUUID)
.then(function(characteristics) {
if (characteristics.length !== 1) return reject("getCharacteristic error: characteristic not found");
.then(characteristics => {
if (characteristics.length !== 1)
return reject("getCharacteristic error: characteristic not found");
resolve(characteristics[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getCharacteristic error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getCharacteristics = function(characteristicUUID) {
return new Promise(function(resolve, reject) {
if (!this.device.gatt.connected) return reject("getCharacteristics error: device not connected");
});
}
getCharacteristics(characteristicUUID) {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected)
return reject("getCharacteristics error: device not connected");
function complete() {
if (!characteristicUUID) return resolve(this._characteristics);
var filtered = this._characteristics.filter(function(characteristic) {
return (characteristic.uuid === helpers.getCharacteristicUUID(characteristicUUID));
if (!characteristicUUID)
return resolve(this._characteristics);
const filtered = this._characteristics.filter(characteristic => {
return (characteristic.uuid === helpers_1.getCharacteristicUUID(characteristicUUID));
});
if (filtered.length !== 1) return reject("getCharacteristics error: characteristic not found");
if (filtered.length !== 1)
return reject("getCharacteristics error: characteristic not found");
resolve(filtered);
}
if (this._characteristics) return complete.call(this);
adapter.discoverCharacteristics(this._handle, [], function(characteristics) {
this._characteristics = characteristics.map(function(characteristicInfo) {
if (this._characteristics)
return complete.call(this);
adapter_1.adapter.discoverCharacteristics(this._handle, [], characteristics => {
this._characteristics = characteristics.map(characteristicInfo => {
characteristicInfo.service = this;
return new BluetoothRemoteGATTCharacteristic(characteristicInfo);
}.bind(this));
return new characteristic_1.BluetoothRemoteGATTCharacteristic(characteristicInfo);
});
complete.call(this);
}.bind(this), wrapReject(reject, "getCharacteristics error"));
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getIncludedService = function(serviceUUID) {
return new Promise(function(resolve, reject) {
if (!this.device.gatt.connected) return reject("getIncludedService error: device not connected");
if (!serviceUUID) return reject("getIncludedService error: no service specified");
}, error => {
reject(`getCharacteristics error: ${error}`);
});
});
}
getIncludedService(serviceUUID) {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected)
return reject("getIncludedService error: device not connected");
if (!serviceUUID)
return reject("getIncludedService error: no service specified");
this.getIncludedServices(serviceUUID)
.then(function(services) {
if (services.length !== 1) return reject("getIncludedService error: service not found");
.then(services => {
if (services.length !== 1)
return reject("getIncludedService error: service not found");
resolve(services[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getIncludedService error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getIncludedServices = function(serviceUUID) {
return new Promise(function(resolve, reject) {
if (!this.device.gatt.connected) return reject("getIncludedServices error: device not connected");
});
}
getIncludedServices(serviceUUID) {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected)
return reject("getIncludedServices error: device not connected");
function complete() {
if (!serviceUUID) return resolve(this._services);
var filtered = this._services.filter(function(service) {
return (service.uuid === helpers.getServiceUUID(serviceUUID));
if (!serviceUUID)
return resolve(this._services);
const filtered = this._services.filter(service => {
return (service.uuid === helpers_1.getServiceUUID(serviceUUID));
});
if (filtered.length !== 1) return reject("getIncludedServices error: service not found");
if (filtered.length !== 1)
return reject("getIncludedServices error: service not found");
resolve(filtered);
}
if (this._services) return complete.call(this);
adapter.discoverIncludedServices(this._handle, this.device._allowedServices, function(services) {
this._services = services.map(function(serviceInfo) {
if (this._services)
return complete.call(this);
adapter_1.adapter.discoverIncludedServices(this._handle, this.device._allowedServices, services => {
this._services = services.map(serviceInfo => {
serviceInfo.device = this.device;
return new BluetoothRemoteGATTService(serviceInfo);
}.bind(this));
});
complete.call(this);
}.bind(this), wrapReject(reject, "getIncludedServices error"));
}.bind(this));
};
BluetoothRemoteGATTService.prototype.addEventListener = createListenerFn([
"serviceadded",
"servicechanged",
"serviceremoved"
]);
BluetoothRemoteGATTService.prototype.removeEventListener = removeEventListener;
BluetoothRemoteGATTService.prototype.dispatchEvent = dispatchEvent;
*/
}, error => {
reject(`getIncludedServices error: ${error}`);
});
});
}
}
/**
* Service Added event
* @event
*/
BluetoothRemoteGATTService.EVENT_ADDED = "serviceadded";
/**
* Service Changed event
* @event
*/
BluetoothRemoteGATTService.EVENT_CHANGED = "servicechanged";
/**
* Service Removed event
* @event
*/
BluetoothRemoteGATTService.EVENT_REMOVED = "serviceremoved";
exports.BluetoothRemoteGATTService = BluetoothRemoteGATTService;
//# sourceMappingURL=service.js.map
{
"name": "webbluetooth",
"version": "0.0.3",
"version": "0.0.4",
"description": "Node.js implementation of the Web Bluetooth Specification",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/thegecko/webbluetooth",

@@ -8,3 +8,3 @@ # Node Web Bluetooth

[Node.js > v4.0.0](https://nodejs.org), which includes `npm`.
[Node.js > v4.8.0](https://nodejs.org), which includes `npm`.

@@ -19,4 +19,4 @@ ## Installation

See the examples in [src/examples/](https://github.com/thegecko/webbluetooth/tree/master/src/examples/) or view the API documentation at:
See the examples in [examples/](https://github.com/thegecko/webbluetooth/tree/master/examples/) or view the API documentation at:
https://thegecko.github.io/webbluetooth/

@@ -33,2 +33,5 @@ /*

/**
* @hidden
*/
export interface Adapter {

@@ -51,9 +54,12 @@ startScan: (serviceUUIDs: Array<string>, foundFn: (device: Partial<BluetoothDevice>) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;

/**
* @hidden
*/
export class NobleAdapter implements Adapter {
private deviceHandles: {};
private serviceHandles: {};
private characteristicHandles: {};
private descriptorHandles: {};
private charNotifies: {};
private deviceHandles: {} = {};
private serviceHandles: {} = {};
private characteristicHandles: {} = {};
private descriptorHandles: {} = {};
private charNotifies: {} = {};
private foundFn: (device: Partial<BluetoothDevice>) => void = null;

@@ -64,3 +70,3 @@ private initialised: boolean = false;

if (this.initialised) return completeFn();
noble.on("discover", this.discover);
noble.on("discover", this.discover.bind(this));
this.initialised = true;

@@ -162,3 +168,3 @@ completeFn();

if (noble.state === "unknown") noble["once"]("stateChange", stateCB.bind(this));
else stateCB(noble.state);
else stateCB.call(this, noble.state);
});

@@ -175,3 +181,3 @@ }

baseDevice.once("connect", connectFn);
baseDevice.once("disconnect", function() {
baseDevice.once("disconnect", () => {
this.serviceHandles = {};

@@ -182,3 +188,3 @@ this.characteristicHandles = {};

disconnectFn();
}.bind(this));
});
baseDevice.connect(this.checkForError(errorFn));

@@ -193,6 +199,5 @@ }

const baseDevice = this.deviceHandles[handle];
baseDevice.discoverServices([], this.checkForError(errorFn, function(services) {
baseDevice.discoverServices([], this.checkForError(errorFn, services => {
const discovered = [];
services.forEach(function(serviceInfo) {
services.forEach(serviceInfo => {
const serviceUUID = getCanonicalUUID(serviceInfo.uuid);

@@ -209,6 +214,6 @@

}
}, this);
});
completeFn(discovered);
}.bind(this)));
}));
}

@@ -218,3 +223,3 @@

const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverIncludedServices([], this.checkForError(errorFn, function(services) {
serviceInfo.discoverIncludedServices([], this.checkForError(errorFn, services => {

@@ -237,3 +242,3 @@ const discovered = [];

completeFn(discovered);
}.bind(this)));
}));
}

@@ -243,6 +248,6 @@

const serviceInfo = this.serviceHandles[handle];
serviceInfo.discoverCharacteristics([], this.checkForError(errorFn, function(characteristics) {
serviceInfo.discoverCharacteristics([], this.checkForError(errorFn, characteristics => {
const discovered = [];
characteristics.forEach(function(characteristicInfo) {
characteristics.forEach(characteristicInfo => {
const charUUID = getCanonicalUUID(characteristicInfo.uuid);

@@ -269,3 +274,3 @@

characteristicInfo.on("data", function(data, isNotification) {
characteristicInfo.on("data", (data, isNotification) => {
if (isNotification === true && typeof this.charNotifies[charUUID] === "function") {

@@ -275,3 +280,3 @@ const dataView = this.bufferToDataView(data);

}
}.bind(this));
});
}

@@ -281,3 +286,3 @@ }, this);

completeFn(discovered);
}.bind(this)));
}));
}

@@ -287,6 +292,6 @@

const characteristicInfo = this.characteristicHandles[handle];
characteristicInfo.discoverDescriptors(this.checkForError(errorFn, function(descriptors) {
characteristicInfo.discoverDescriptors(this.checkForError(errorFn, descriptors => {
const discovered = [];
descriptors.forEach(function(descriptorInfo) {
descriptors.forEach(descriptorInfo => {
const descUUID = getCanonicalUUID(descriptorInfo.uuid);

@@ -306,3 +311,3 @@

completeFn(discovered);
}.bind(this)));
}));
}

@@ -322,3 +327,3 @@

public enableNotify(handle: string, notifyFn: () => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
public enableNotify(handle: string, notifyFn: (value: DataView) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void {
if (this.charNotifies[handle]) {

@@ -360,1 +365,6 @@ this.charNotifies[handle] = notifyFn;

}
/**
* @hidden
*/
export const adapter = new NobleAdapter();

@@ -26,30 +26,56 @@ /*

export class BluetoothRemoteGATTCharacteristic {
}
import { Emitter } from "./emitter";
import { BluetoothRemoteGATTService } from "./service";
import { BluetoothRemoteGATTDescriptor } from "./descriptor";
import { getDescriptorUUID } from "./helpers";
import { adapter } from "./adapter";
/*
// BluetoothRemoteGATTCharacteristic Object
var BluetoothRemoteGATTCharacteristic = function(properties) {
this._handle = null;
this._descriptors = null;
export class BluetoothRemoteGATTCharacteristic extends Emitter {
this.service = null;
this.uuid = null;
this.properties = {
broadcast: false,
read: false,
writeWithoutResponse: false,
write: false,
notify: false,
indicate: false,
authenticatedSignedWrites: false,
reliableWrite: false,
writableAuxiliaries: false
};
this.value = null;
/**
* Characteristic Value Changed event
* @event
*/
public static EVENT_CHANGED: string = "characteristicvaluechanged";
mergeDictionary(this, properties);
/**
* @hidden
*/
public _handle: string = null;
/**
* @hidden
*/
public _descriptors: Array<BluetoothRemoteGATTDescriptor> = null;
public service: BluetoothRemoteGATTService = null;
public uuid = null;
public value = null;
public properties = {
broadcast: false,
read: false,
writeWithoutResponse: false,
write: false,
notify: false,
indicate: false,
authenticatedSignedWrites: false,
reliableWrite: false,
writableAuxiliaries: false
};
BluetoothRemoteGATTCharacteristic.prototype.getDescriptor = function(descriptorUUID) {
return new Promise(function(resolve, reject) {
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTCharacteristic>) {
super();
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
}
public getDescriptor(descriptorUUID): Promise<BluetoothRemoteGATTDescriptor> {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("getDescriptor error: device not connected");

@@ -59,13 +85,14 @@ if (!descriptorUUID) return reject("getDescriptor error: no descriptor specified");

this.getDescriptors(descriptorUUID)
.then(function(descriptors) {
.then(descriptors => {
if (descriptors.length !== 1) return reject("getDescriptor error: descriptor not found");
resolve(descriptors[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getDescriptor error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.getDescriptors = function(descriptorUUID) {
return new Promise(function(resolve, reject) {
});
}
public getDescriptors(descriptorUUID): Promise<Array<BluetoothRemoteGATTDescriptor>> {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("getDescriptors error: device not connected");

@@ -75,67 +102,86 @@

if (!descriptorUUID) return resolve(this._descriptors);
var filtered = this._descriptors.filter(function(descriptor) {
return (descriptor.uuid === helpers.getDescriptorUUID(descriptorUUID));
const filtered = this._descriptors.filter(descriptor => {
return (descriptor.uuid === getDescriptorUUID(descriptorUUID));
});
if (filtered.length !== 1) return reject("getDescriptors error: descriptor not found");
resolve(filtered);
}
if (this._descriptors) return complete.call(this);
adapter.discoverDescriptors(this._handle, [], function(descriptors) {
this._descriptors = descriptors.map(function(descriptorInfo) {
adapter.discoverDescriptors(this._handle, [], descriptors => {
this._descriptors = descriptors.map(descriptorInfo => {
descriptorInfo.characteristic = this;
return new BluetoothRemoteGATTDescriptor(descriptorInfo);
}.bind(this));
});
complete.call(this);
}.bind(this), wrapReject(reject, "getDescriptors error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.readValue = function() {
return new Promise(function(resolve, reject) {
}, error => {
reject(`getDescriptors error: ${error}`);
});
});
}
public readValue(): Promise<DataView> {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("readValue error: device not connected");
adapter.readCharacteristic(this._handle, function(dataView) {
adapter.readCharacteristic(this._handle, dataView => {
this.value = dataView;
resolve(dataView);
this.dispatchEvent({ type: "characteristicvaluechanged", bubbles: true });
}.bind(this), wrapReject(reject, "readValue error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.writeValue = function(bufferSource) {
return new Promise(function(resolve, reject) {
this.emit(BluetoothRemoteGATTCharacteristic.EVENT_CHANGED);
}, error => {
reject(`readValue error: ${error}`);
});
});
}
public writeValue(bufferSource: ArrayBuffer | ArrayBufferView) {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("writeValue error: device not connected");
var arrayBuffer = bufferSource.buffer || bufferSource;
var dataView = new DataView(arrayBuffer);
adapter.writeCharacteristic(this._handle, dataView, function() {
function isView(source: ArrayBuffer | ArrayBufferView): source is ArrayBufferView {
return (source as ArrayBufferView).buffer !== undefined;
}
const arrayBuffer = isView(bufferSource) ? bufferSource.buffer : bufferSource;
const dataView = new DataView(arrayBuffer);
adapter.writeCharacteristic(this._handle, dataView, () => {
this.value = dataView;
resolve();
}.bind(this), wrapReject(reject, "writeValue error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.startNotifications = function() {
return new Promise(function(resolve, reject) {
}, error => {
reject(`writeValue error: ${error}`);
});
});
}
public startNotifications(): Promise<BluetoothRemoteGATTCharacteristic> {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("startNotifications error: device not connected");
adapter.enableNotify(this._handle, function(dataView) {
adapter.enableNotify(this._handle, dataView => {
this.value = dataView;
this.dispatchEvent({ type: "characteristicvaluechanged", bubbles: true });
}.bind(this), function() {
this.emit(BluetoothRemoteGATTCharacteristic.EVENT_CHANGED);
}, () => {
resolve(this);
}.bind(this), wrapReject(reject, "startNotifications error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.stopNotifications = function() {
return new Promise(function(resolve, reject) {
}, error => {
reject(`startNotifications error: ${error}`);
});
});
}
public stopNotifications(): Promise<BluetoothRemoteGATTCharacteristic> {
return new Promise((resolve, reject) => {
if (!this.service.device.gatt.connected) return reject("stopNotifications error: device not connected");
adapter.disableNotify(this._handle, function() {
adapter.disableNotify(this._handle, () => {
resolve(this);
}.bind(this), wrapReject(reject, "stopNotifications error"));
}.bind(this));
};
BluetoothRemoteGATTCharacteristic.prototype.addEventListener = createListenerFn([
"characteristicvaluechanged"
]);
BluetoothRemoteGATTCharacteristic.prototype.removeEventListener = removeEventListener;
BluetoothRemoteGATTCharacteristic.prototype.dispatchEvent = dispatchEvent;
*/
}, error => {
reject(`stopNotifications error: ${error}`);
});
});
}
}

@@ -26,38 +26,58 @@ /*

import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
import { adapter } from "./adapter";
export class BluetoothRemoteGATTDescriptor {
}
/**
* @hidden
*/
public _handle: string = null;
/*
// BluetoothRemoteGATTDescriptor Object
var BluetoothRemoteGATTDescriptor = function(properties) {
this._handle = null;
public characteristic: BluetoothRemoteGATTCharacteristic = null;
public uuid = null;
public value = null;
this.characteristic = null;
this.uuid = null;
this.value = null;
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTDescriptor>) {
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
}
mergeDictionary(this, properties);
};
BluetoothRemoteGATTDescriptor.prototype.readValue = function() {
return new Promise(function(resolve, reject) {
public readValue(): Promise<DataView> {
return new Promise((resolve, reject) => {
if (!this.characteristic.service.device.gatt.connected) return reject("readValue error: device not connected");
adapter.readDescriptor(this._handle, function(dataView) {
adapter.readDescriptor(this._handle, dataView => {
this.value = dataView;
resolve(dataView);
}.bind(this), wrapReject(reject, "readValue error"));
}.bind(this));
};
BluetoothRemoteGATTDescriptor.prototype.writeValue = function(bufferSource) {
return new Promise(function(resolve, reject) {
}, error => {
reject(`readValue error: ${error}`);
});
});
}
public writeValue(bufferSource: ArrayBuffer | ArrayBufferView) {
return new Promise((resolve, reject) => {
if (!this.characteristic.service.device.gatt.connected) return reject("writeValue error: device not connected");
var arrayBuffer = bufferSource.buffer || bufferSource;
var dataView = new DataView(arrayBuffer);
adapter.writeDescriptor(this._handle, dataView, function() {
function isView(source: ArrayBuffer | ArrayBufferView): source is ArrayBufferView {
return (source as ArrayBufferView).buffer !== undefined;
}
const arrayBuffer = isView(bufferSource) ? bufferSource.buffer : bufferSource;
const dataView = new DataView(arrayBuffer);
adapter.writeDescriptor(this._handle, dataView, () => {
this.value = dataView;
resolve();
}.bind(this), wrapReject(reject, "writeValue error"));
}.bind(this));
};
*/
}, error => {
reject(`writeValue error: ${error}`);
});
});
}
}

@@ -26,9 +26,25 @@ /*

import { Emitter } from "./emitter";
import { BluetoothRemoteGATTServer } from "./server";
export class BluetoothDevice {
export class BluetoothDevice extends Emitter {
/**
* Server Disconnected event
* @event
*/
public static EVENT_DISCONNECTED: string = "gattserverdisconnected";
/**
* @hidden
*/
public _handle: string = null;
/**
* @hidden
*/
public _allowedServices: Array<string> = [];
public id: string = "unknown";
public name: string = null;

@@ -45,3 +61,7 @@ // public adData: {

/**
* @hidden
*/
constructor(init?: Partial<BluetoothDevice>) {
super();
for (const key in init) {

@@ -56,29 +76,1 @@ if (init.hasOwnProperty(key)) {

}
/*
// BluetoothDevice Object
var BluetoothDevice = function(properties) {
this._handle = null;
this._allowedServices = [];
this.id = "unknown";
this.name = null;
this.adData = {
appearance: null,
txPower: null,
rssi: null,
manufacturerData: new Map(),
serviceData: new Map()
};
this.gatt = new BluetoothRemoteGATTServer();
this.gatt.device = this;
this.uuids = [];
mergeDictionary(this, properties);
};
BluetoothDevice.prototype.addEventListener = createListenerFn([
"gattserverdisconnected",
]);
BluetoothDevice.prototype.removeEventListener = removeEventListener;
BluetoothDevice.prototype.dispatchEvent = dispatchEvent;
*/

@@ -6,3 +6,3 @@ # Node Web Bluetooth

[Node.js > v4.0.0](https://nodejs.org), which includes `npm`.
[Node.js > v4.8.0](https://nodejs.org), which includes `npm`.

@@ -9,0 +9,0 @@ ## Installation

@@ -27,37 +27,39 @@ /*

import { BluetoothDevice } from "./device";
import { BluetoothRemoteGATTService } from "./service";
import { getServiceUUID } from "./helpers";
import { adapter } from "./adapter";
export class BluetoothRemoteGATTServer {
public _services: Array<string> = null;
/**
* @hidden
*/
public _services: Array<BluetoothRemoteGATTService> = null;
public device: BluetoothDevice = null;
public connected: boolean = false;
}
/*
// BluetoothRemoteGATTServer Object
var BluetoothRemoteGATTServer = function() {
this._services = null;
this.device = null;
this.connected = false;
};
BluetoothRemoteGATTServer.prototype.connect = function() {
return new Promise(function(resolve, reject) {
public connect(): Promise<BluetoothRemoteGATTServer> {
return new Promise((resolve, reject) => {
if (this.connected) return reject("connect error: device already connected");
adapter.connect(this.device._handle, function() {
adapter.connect(this.device._handle, () => {
this.connected = true;
resolve(this);
}.bind(this), function() {
}, () => {
this._services = null;
this.connected = false;
this.device.dispatchEvent({ type: "gattserverdisconnected", bubbles: true });
}.bind(this), wrapReject(reject, "connect error"));
}.bind(this));
};
BluetoothRemoteGATTServer.prototype.disconnect = function() {
this.device.emit(BluetoothDevice.EVENT_DISCONNECTED);
}, error => {
reject(`connect Error: ${error}`);
});
});
}
public disconnect() {
adapter.disconnect(this.device._handle);
this.connected = false;
};
BluetoothRemoteGATTServer.prototype.getPrimaryService = function(serviceUUID) {
return new Promise(function(resolve, reject) {
}
public getPrimaryService(serviceUUID): Promise<BluetoothRemoteGATTService> {
return new Promise((resolve, reject) => {
if (!this.connected) return reject("getPrimaryService error: device not connected");

@@ -67,13 +69,14 @@ if (!serviceUUID) return reject("getPrimaryService error: no service specified");

this.getPrimaryServices(serviceUUID)
.then(function(services) {
.then(services => {
if (services.length !== 1) return reject("getPrimaryService error: service not found");
resolve(services[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getPrimaryService error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTServer.prototype.getPrimaryServices = function(serviceUUID) {
return new Promise(function(resolve, reject) {
});
}
public getPrimaryServices(serviceUUID): Promise<Array<BluetoothRemoteGATTService>> {
return new Promise((resolve, reject) => {
if (!this.connected) return reject("getPrimaryServices error: device not connected");

@@ -83,18 +86,25 @@

if (!serviceUUID) return resolve(this._services);
var filtered = this._services.filter(function(service) {
return (service.uuid === helpers.getServiceUUID(serviceUUID));
const filtered = this._services.filter(service => {
return (service.uuid === getServiceUUID(serviceUUID));
});
if (filtered.length !== 1) return reject("getPrimaryServices error: service not found");
resolve(filtered);
}
if (this._services) return complete.call(this);
adapter.discoverServices(this.device._handle, this.device._allowedServices, function(services) {
this._services = services.map(function(serviceInfo) {
adapter.discoverServices(this.device._handle, this.device._allowedServices, services => {
this._services = services.map(serviceInfo => {
serviceInfo.device = this.device;
return new BluetoothRemoteGATTService(serviceInfo);
}.bind(this));
});
complete.call(this);
}.bind(this), wrapReject(reject, "getPrimaryServices error"));
}.bind(this));
};
*/
}, error => {
reject(`getPrimaryServices error: ${error}`);
});
});
}
}

@@ -26,21 +26,62 @@ /*

export class BluetoothRemoteGATTService {
}
import { Emitter } from "./emitter";
import { BluetoothDevice } from "./device";
import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
import { getCharacteristicUUID, getServiceUUID } from "./helpers";
import { adapter } from "./adapter";
/*
// BluetoothRemoteGATTService Object
var BluetoothRemoteGATTService = function(properties) {
this._handle = null;
this._services = null;
this._characteristics = null;
export class BluetoothRemoteGATTService extends Emitter {
this.device = null;
this.uuid = null;
this.isPrimary = false;
/**
* Service Added event
* @event
*/
public static EVENT_ADDED: string = "serviceadded";
mergeDictionary(this, properties);
this.dispatchEvent({ type: "serviceadded", bubbles: true });
};
BluetoothRemoteGATTService.prototype.getCharacteristic = function(characteristicUUID) {
return new Promise(function(resolve, reject) {
/**
* Service Changed event
* @event
*/
public static EVENT_CHANGED: string = "servicechanged";
/**
* Service Removed event
* @event
*/
public static EVENT_REMOVED: string = "serviceremoved";
/**
* @hidden
*/
public _handle: string = null;
/**
* @hidden
*/
public _services: Array<BluetoothRemoteGATTService> = null;
/**
* @hidden
*/
public _characteristics: Array<BluetoothRemoteGATTCharacteristic> = null;
public device: BluetoothDevice = null;
public uuid = null;
public isPrimary = false;
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTService>) {
super();
for (const key in init) {
if (init.hasOwnProperty(key)) {
this[key] = init[key];
}
}
this.emit(BluetoothRemoteGATTService.EVENT_ADDED);
}
public getCharacteristic(characteristicUUID): Promise<BluetoothRemoteGATTCharacteristic> {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected) return reject("getCharacteristic error: device not connected");

@@ -50,13 +91,14 @@ if (!characteristicUUID) return reject("getCharacteristic error: no characteristic specified");

this.getCharacteristics(characteristicUUID)
.then(function(characteristics) {
.then(characteristics => {
if (characteristics.length !== 1) return reject("getCharacteristic error: characteristic not found");
resolve(characteristics[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getCharacteristic error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getCharacteristics = function(characteristicUUID) {
return new Promise(function(resolve, reject) {
});
}
public getCharacteristics(characteristicUUID): Promise<Array<BluetoothRemoteGATTCharacteristic>> {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected) return reject("getCharacteristics error: device not connected");

@@ -66,20 +108,28 @@

if (!characteristicUUID) return resolve(this._characteristics);
var filtered = this._characteristics.filter(function(characteristic) {
return (characteristic.uuid === helpers.getCharacteristicUUID(characteristicUUID));
const filtered = this._characteristics.filter(characteristic => {
return (characteristic.uuid === getCharacteristicUUID(characteristicUUID));
});
if (filtered.length !== 1) return reject("getCharacteristics error: characteristic not found");
resolve(filtered);
}
if (this._characteristics) return complete.call(this);
adapter.discoverCharacteristics(this._handle, [], function(characteristics) {
this._characteristics = characteristics.map(function(characteristicInfo) {
adapter.discoverCharacteristics(this._handle, [], characteristics => {
this._characteristics = characteristics.map(characteristicInfo => {
characteristicInfo.service = this;
return new BluetoothRemoteGATTCharacteristic(characteristicInfo);
}.bind(this));
});
complete.call(this);
}.bind(this), wrapReject(reject, "getCharacteristics error"));
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getIncludedService = function(serviceUUID) {
return new Promise(function(resolve, reject) {
}, error => {
reject(`getCharacteristics error: ${error}`);
});
});
}
public getIncludedService(serviceUUID): Promise<BluetoothRemoteGATTService> {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected) return reject("getIncludedService error: device not connected");

@@ -89,13 +139,14 @@ if (!serviceUUID) return reject("getIncludedService error: no service specified");

this.getIncludedServices(serviceUUID)
.then(function(services) {
.then(services => {
if (services.length !== 1) return reject("getIncludedService error: service not found");
resolve(services[0]);
})
.catch(function(error) {
reject(error);
.catch(error => {
reject(`getIncludedService error: ${error}`);
});
}.bind(this));
};
BluetoothRemoteGATTService.prototype.getIncludedServices = function(serviceUUID) {
return new Promise(function(resolve, reject) {
});
}
public getIncludedServices(serviceUUID): Promise<Array<BluetoothRemoteGATTService>> {
return new Promise((resolve, reject) => {
if (!this.device.gatt.connected) return reject("getIncludedServices error: device not connected");

@@ -105,25 +156,25 @@

if (!serviceUUID) return resolve(this._services);
var filtered = this._services.filter(function(service) {
return (service.uuid === helpers.getServiceUUID(serviceUUID));
const filtered = this._services.filter(service => {
return (service.uuid === getServiceUUID(serviceUUID));
});
if (filtered.length !== 1) return reject("getIncludedServices error: service not found");
resolve(filtered);
}
if (this._services) return complete.call(this);
adapter.discoverIncludedServices(this._handle, this.device._allowedServices, function(services) {
this._services = services.map(function(serviceInfo) {
adapter.discoverIncludedServices(this._handle, this.device._allowedServices, services => {
this._services = services.map(serviceInfo => {
serviceInfo.device = this.device;
return new BluetoothRemoteGATTService(serviceInfo);
}.bind(this));
});
complete.call(this);
}.bind(this), wrapReject(reject, "getIncludedServices error"));
}.bind(this));
};
BluetoothRemoteGATTService.prototype.addEventListener = createListenerFn([
"serviceadded",
"servicechanged",
"serviceremoved"
]);
BluetoothRemoteGATTService.prototype.removeEventListener = removeEventListener;
BluetoothRemoteGATTService.prototype.dispatchEvent = dispatchEvent;
*/
}, error => {
reject(`getIncludedServices error: ${error}`);
});
});
}
}

@@ -5,2 +5,5 @@ import { BluetoothDevice } from "./device";

import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
/**
* @hidden
*/
export interface Adapter {

@@ -22,2 +25,5 @@ startScan: (serviceUUIDs: Array<string>, foundFn: (device: Partial<BluetoothDevice>) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void) => void;

}
/**
* @hidden
*/
export declare class NobleAdapter implements Adapter {

@@ -46,3 +52,3 @@ private deviceHandles;

writeCharacteristic(handle: string, value: DataView, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void;
enableNotify(handle: string, notifyFn: () => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void;
enableNotify(handle: string, notifyFn: (value: DataView) => void, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void;
disableNotify(handle: string, completeFn?: () => void, errorFn?: (errorMsg: string) => void): void;

@@ -52,1 +58,5 @@ readDescriptor(handle: string, completeFn: (value: DataView) => void, errorFn?: (errorMsg: string) => void): void;

}
/**
* @hidden
*/
export declare const adapter: NobleAdapter;

@@ -1,2 +0,42 @@

export declare class BluetoothRemoteGATTCharacteristic {
import { Emitter } from "./emitter";
import { BluetoothRemoteGATTService } from "./service";
import { BluetoothRemoteGATTDescriptor } from "./descriptor";
export declare class BluetoothRemoteGATTCharacteristic extends Emitter {
/**
* Characteristic Value Changed event
* @event
*/
static EVENT_CHANGED: string;
/**
* @hidden
*/
_handle: string;
/**
* @hidden
*/
_descriptors: Array<BluetoothRemoteGATTDescriptor>;
service: BluetoothRemoteGATTService;
uuid: any;
value: any;
properties: {
broadcast: boolean;
read: boolean;
writeWithoutResponse: boolean;
write: boolean;
notify: boolean;
indicate: boolean;
authenticatedSignedWrites: boolean;
reliableWrite: boolean;
writableAuxiliaries: boolean;
};
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTCharacteristic>);
getDescriptor(descriptorUUID: any): Promise<BluetoothRemoteGATTDescriptor>;
getDescriptors(descriptorUUID: any): Promise<Array<BluetoothRemoteGATTDescriptor>>;
readValue(): Promise<DataView>;
writeValue(bufferSource: ArrayBuffer | ArrayBufferView): Promise<{}>;
startNotifications(): Promise<BluetoothRemoteGATTCharacteristic>;
stopNotifications(): Promise<BluetoothRemoteGATTCharacteristic>;
}

@@ -0,2 +1,16 @@

import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
export declare class BluetoothRemoteGATTDescriptor {
/**
* @hidden
*/
_handle: string;
characteristic: BluetoothRemoteGATTCharacteristic;
uuid: any;
value: any;
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTDescriptor>);
readValue(): Promise<DataView>;
writeValue(bufferSource: ArrayBuffer | ArrayBufferView): Promise<{}>;
}

@@ -0,4 +1,16 @@

import { Emitter } from "./emitter";
import { BluetoothRemoteGATTServer } from "./server";
export declare class BluetoothDevice {
export declare class BluetoothDevice extends Emitter {
/**
* Server Disconnected event
* @event
*/
static EVENT_DISCONNECTED: string;
/**
* @hidden
*/
_handle: string;
/**
* @hidden
*/
_allowedServices: Array<string>;

@@ -9,3 +21,6 @@ id: string;

uuids: Array<string>;
/**
* @hidden
*/
constructor(init?: Partial<BluetoothDevice>);
}
import { BluetoothDevice } from "./device";
import { BluetoothRemoteGATTService } from "./service";
export declare class BluetoothRemoteGATTServer {
_services: Array<string>;
/**
* @hidden
*/
_services: Array<BluetoothRemoteGATTService>;
device: BluetoothDevice;
connected: boolean;
connect(): Promise<BluetoothRemoteGATTServer>;
disconnect(): void;
getPrimaryService(serviceUUID: any): Promise<BluetoothRemoteGATTService>;
getPrimaryServices(serviceUUID: any): Promise<Array<BluetoothRemoteGATTService>>;
}

@@ -1,2 +0,43 @@

export declare class BluetoothRemoteGATTService {
import { Emitter } from "./emitter";
import { BluetoothDevice } from "./device";
import { BluetoothRemoteGATTCharacteristic } from "./characteristic";
export declare class BluetoothRemoteGATTService extends Emitter {
/**
* Service Added event
* @event
*/
static EVENT_ADDED: string;
/**
* Service Changed event
* @event
*/
static EVENT_CHANGED: string;
/**
* Service Removed event
* @event
*/
static EVENT_REMOVED: string;
/**
* @hidden
*/
_handle: string;
/**
* @hidden
*/
_services: Array<BluetoothRemoteGATTService>;
/**
* @hidden
*/
_characteristics: Array<BluetoothRemoteGATTCharacteristic>;
device: BluetoothDevice;
uuid: any;
isPrimary: boolean;
/**
* @hidden
*/
constructor(init?: Partial<BluetoothRemoteGATTService>);
getCharacteristic(characteristicUUID: any): Promise<BluetoothRemoteGATTCharacteristic>;
getCharacteristics(characteristicUUID: any): Promise<Array<BluetoothRemoteGATTCharacteristic>>;
getIncludedService(serviceUUID: any): Promise<BluetoothRemoteGATTService>;
getIncludedServices(serviceUUID: any): Promise<Array<BluetoothRemoteGATTService>>;
}

Sorry, the diff of this file is too big to display

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

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

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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc