Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for discovering ONLY one service when desired. Fix broken… #2

Merged
merged 4 commits into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions lib/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,37 @@ NobleBindings.prototype.discoverServices = function (deviceUuid, filterServiceUu
throw new Error('Device is not connected. UUID: ' + deviceUuid);
}

rt.promisify(device.getGattServicesAsync, device)(
BluetoothCacheMode.uncached).then(result => {
if (filterServiceUuids && filterServiceUuids.length === 1) {
rt.promisify(device.getGattServicesForUuidAsync, device)(
addDashesToUuid(filterServiceUuids[0])).then(result => {
checkCommunicationResult(deviceUuid, result);

let services = rt.trackDisposables(deviceUuid, rt.toArray(result.services));
let serviceUuids = services.map(s => uuidToString(s.uuid))
.filter(filterUuids(filterServiceUuids));
let serviceUuids = services.map(s => uuidToString(s.uuid));

debug(deviceUuid + ' services: %o', serviceUuids);
this.emit('servicesDiscover', deviceUuid, serviceUuids);
}).catch(ex => {
debug('failed to get GATT services for device %s: %s', deviceUuid, ex.stack);
this.emit('servicesDiscover', deviceUuid, ex);
});
return;
}

rt.promisify(device.getGattServicesAsync, device)(
BluetoothCacheMode.uncached).then(result => {
checkCommunicationResult(deviceUuid, result);

let services = rt.trackDisposables(deviceUuid, rt.toArray(result.services));
let serviceUuids = services.map(s => uuidToString(s.uuid))
.filter(filterUuids(filterServiceUuids));

debug(deviceUuid + ' services: %o', serviceUuids);
this.emit('servicesDiscover', deviceUuid, serviceUuids);
}).catch(ex => {
debug('failed to get GATT services for device %s: %s', deviceUuid, ex.stack);
this.emit('servicesDiscover', deviceUuid, ex);
});
};

NobleBindings.prototype.discoverIncludedServices =
Expand Down Expand Up @@ -516,6 +533,7 @@ NobleBindings.prototype._onAdvertisementWatcherReceived = function (sender, e) {
}

let connectable;
debug(' advertisement type: %s', e.advertisementType);
switch (e.advertisementType) {
case BluetoothLEAdvertisementType.connectableUndirected:
case BluetoothLEAdvertisementType.connectableDirected:
Expand All @@ -540,7 +558,7 @@ NobleBindings.prototype._onAdvertisementWatcherReceived = function (sender, e) {
debug(' data section: %s',
(getEnumName(BluetoothLEAdvertisementDataTypes, dataSection.dataType) ||
dataSection.dataType));
// https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
// https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile
switch (dataSection.dataType) {
case BluetoothLEAdvertisementDataTypes.completeService16BitUuids:
var id = Buffer.allocUnsafe(2);
Expand Down Expand Up @@ -592,6 +610,9 @@ NobleBindings.prototype._onAdvertisementWatcherReceived = function (sender, e) {
this._deviceMap[deviceUuid] = deviceRecord;
}

// update connectable status!
deviceRecord.connectable = connectable;

if (e.advertisement.localName) {
deviceRecord.name = e.advertisement.localName;
}
Expand Down Expand Up @@ -827,6 +848,10 @@ function stringToUuid(uuid) {
}
}

function addDashesToUuid(i) {
return i.substr(0,8)+"-"+i.substr(8,4)+"-"+i.substr(12,4)+"-"+i.substr(16,4)+"-"+i.substr(20);
}

function uuidToString(uuid) {
if (!uuid) {
return uuid;
Expand Down