Skip to content

Commit 1fdaabf

Browse files
author
Daisuke Baba
committed
Merge branch 'develop' of https://github.com/CANDY-LINE/node-red-contrib-generic-ble into develop
2 parents 01389c2 + 697c7b7 commit 1fdaabf

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

src/generic-ble.html

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@
161161
return peripheral.uuid;
162162
}
163163
const state = {
164-
fetchBleDevicesTask: null
164+
fetchBleDevicesTask: null,
165+
lastBledevRequest: null
165166
};
166167
RED.nodes.registerType('Generic BLE',{
167168
category: 'config',
@@ -187,9 +188,15 @@
187188
},
188189
oneditcancel: function() {
189190
state.fetchBleDevicesTask = null;
191+
if (state.lastBledevRequest) {
192+
state.lastBledevRequest.abort();
193+
}
190194
},
191195
oneditsave: function() {
192196
state.fetchBleDevicesTask = null;
197+
if (state.lastBledevRequest) {
198+
state.lastBledevRequest.abort();
199+
}
193200
var address = $('#node-config-input-address').val();
194201
if (address) {
195202
$('#node-config-input-address').val(address.toLowerCase());
@@ -283,7 +290,6 @@
283290
return current ? toDeviceKey(current) : '';
284291
}
285292

286-
var lastBledevRequest = null;
287293
function applyBleDevice() {
288294
var selected = getBleDevice();
289295
if (!selected) {
@@ -299,16 +305,16 @@
299305
{style:'display:block; margin-left:auto; margin-right:auto;'})
300306
.appendTo($('#node-config-input-characteristics-container'));
301307

302-
if (lastBledevRequest) {
303-
lastBledevRequest.abort();
304-
lastBledevRequest = null;
308+
if (state.lastBledevRequest) {
309+
state.lastBledevRequest.abort();
310+
state.lastBledevRequest = null;
305311
}
306-
lastBledevRequest = $.ajax({
312+
state.lastBledevRequest = $.ajax({
307313
cache: false,
308314
url: '__bledev/' + encodeURIComponent(getUuid(selected)),
309315
dataType: 'json',
310316
success: function(bleDevice) {
311-
lastBledevRequest = null;
317+
state.lastBledevRequest = null;
312318
spin.remove();
313319
if (getBleDeviceKey() === selectedId) {
314320
clearCharacteristics();
@@ -333,7 +339,7 @@
333339
}
334340
},
335341
error: function () {
336-
lastBledevRequest = null;
342+
state.lastBledevRequest = null;
337343
spin.remove();
338344
if (getBleDeviceKey() === selectedId) {
339345
clearCharacteristics();

src/generic-ble.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ async function toDetailedObject(peripheral, RED) {
215215
};
216216
peripheral.once('disconnect', discoveryInterrupted);
217217
peripheral.discoverAllServicesAndCharacteristics((err, services) => {
218+
debug(
219+
`<toDetailedObject${peripheral.uuid}:discoverAllServicesAndCharacteristics> Callback OK!`
220+
);
218221
peripheral.removeListener('disconnect', discoveryInterrupted);
219222
if (err) {
220223
return reject(err);

src/noble/lib/bluez/bindings.js

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import debugLogger from 'debug';
2020
import dbus from 'dbus-next';
2121

2222
const debug = debugLogger('node-red-contrib-generic-ble:noble:bluez');
23-
const CHRARACTERISTICS_DISCOVERY_TIMEOUT_MS = parseInt(
24-
process.env.CHRARACTERISTICS_DISCOVERY_TIMEOUT_MS || '500'
25-
);
2623

2724
// Workaround for a Jest Issue
2825
// https://github.com/kulshekhar/ts-jest/issues/727#issuecomment-422747294
@@ -360,48 +357,30 @@ class BluezBindings extends EventEmitter {
360357
debug(
361358
`discoverCharacteristics:deviceUuid=>${deviceUuid},serviceUuid=>${serviceUuid},characteristicUuids=>${characteristicUuids}`
362359
);
363-
let timeout = 0;
364-
let discoveredCharacteristics = await this._listCharacteristics(
360+
const discoveredCharacteristics = await this._listCharacteristics(
365361
deviceUuid,
366362
serviceUuid,
367363
characteristicUuids
368364
);
369-
if (!discoveredCharacteristics) {
370-
timeout = CHRARACTERISTICS_DISCOVERY_TIMEOUT_MS;
371-
}
372-
setTimeout(async () => {
373-
if (!discoveredCharacteristics) {
374-
discoveredCharacteristics = await this._listCharacteristics(
375-
deviceUuid,
376-
serviceUuid,
377-
characteristicUuids
378-
);
365+
const resultChrs = Object.values(discoveredCharacteristics || {}).map(
366+
(chr) => {
367+
return {
368+
uuid: this._stripDashes(chr.UUID.value),
369+
properties: chr.Flags.value,
370+
};
379371
}
380-
const resultChrs = Object.values(discoveredCharacteristics || {}).map(
381-
(chr) => {
382-
return {
383-
uuid: this._stripDashes(chr.UUID.value),
384-
properties: chr.Flags.value,
385-
};
386-
}
372+
);
373+
debug(`resultChrs => ${JSON.stringify(resultChrs)}`);
374+
try {
375+
this.emit('characteristicsDiscover', deviceUuid, serviceUuid, resultChrs);
376+
debug(
377+
`[${deviceUuid}] OK. Found ${resultChrs.length} Characteristics. characteristicsDiscover event`
387378
);
388-
debug(`resultChrs => ${JSON.stringify(resultChrs)}`);
389-
try {
390-
this.emit(
391-
'characteristicsDiscover',
392-
deviceUuid,
393-
serviceUuid,
394-
resultChrs
395-
);
396-
debug(
397-
`[${deviceUuid}] OK. Found ${resultChrs.length} Characteristics.`
398-
);
399-
} catch (err) {
400-
debug(
401-
`Failed to emit 'characteristicsDiscover' event. message:${err.message}`
402-
);
403-
}
404-
}, timeout);
379+
} catch (err) {
380+
debug(
381+
`Failed to emit 'characteristicsDiscover' event. message:${err.message}`
382+
);
383+
}
405384
}
406385

407386
async read(deviceUuid, serviceUuid, characteristicUuid) {

0 commit comments

Comments
 (0)