Skip to content

Commit b2be36c

Browse files
author
Daisuke Baba
committed
Fix an issue where __bledev request failed to connect the given peripheral because of the wrong precondition
1 parent 30831ea commit b2be36c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/generic-ble.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function disconnectPeripheral(peripheral, done, RED) {
407407
delete peripheral._lock;
408408
}
409409

410-
function connectToPeripheral(peripheral, RED) {
410+
function connectToPeripheral(peripheral, RED, forceConnect=false) {
411411
if (peripheral._lock) {
412412
if (TRACE) {
413413
RED.log.info(`<connectToPeripheral> <${peripheral.uuid}> Gave up to connect`);
@@ -416,11 +416,13 @@ function connectToPeripheral(peripheral, RED) {
416416
return Promise.reject(`<${peripheral.uuid}> Try again`);
417417
}
418418
let bleDevice = configBleDevices[getAddressOrUUID(peripheral)];
419-
if (!hasPendingOperations(bleDevice)) {
420-
if (TRACE) {
421-
RED.log.info(`<connectToPeripheral> <${peripheral.uuid}> Skip to connect as there's nothing to do`);
419+
if (!forceConnect) {
420+
if (!hasPendingOperations(bleDevice)) {
421+
if (TRACE) {
422+
RED.log.info(`<connectToPeripheral> <${peripheral.uuid}> Skip to connect as there's nothing to do`);
423+
}
424+
return Promise.resolve();
422425
}
423-
return Promise.resolve();
424426
}
425427
return new Promise((resolve, reject) => {
426428
let timeout;
@@ -526,7 +528,7 @@ function connectToPeripheral(peripheral, RED) {
526528
});
527529
}
528530

529-
function peripheralTask(uuid, task, done, RED) {
531+
function peripheralTask(uuid, task, done, RED, forceConnect=false) {
530532
return (next) => {
531533
if (TRACE) {
532534
RED.log.info(`<peripheralTask> <${uuid}> START`);
@@ -563,7 +565,7 @@ function peripheralTask(uuid, task, done, RED) {
563565
}, RED);
564566
}
565567

566-
connectToPeripheral(peripheral, RED).then((result) => {
568+
connectToPeripheral(peripheral, RED, forceConnect).then((result) => {
567569
if (!result) {
568570
return new Promise((resolve) => {
569571
setTimeout(() => {
@@ -580,11 +582,11 @@ function peripheralTask(uuid, task, done, RED) {
580582
};
581583
}
582584

583-
function schedulePeripheralTask(uuid, task, done, RED) {
585+
function schedulePeripheralTask(uuid, task, done, RED, forceConnect=false) {
584586
if (!task) {
585587
return;
586588
}
587-
q.push(peripheralTask(uuid, task, done, RED));
589+
q.push(peripheralTask(uuid, task, done, RED, forceConnect));
588590
}
589591

590592
function addErrorListenerToQueue(RED) {
@@ -1051,6 +1053,6 @@ export default function(RED) {
10511053
return res.status(500).send({ status: 500, message: (err.message || err) }).end();
10521054
}
10531055
}
1054-
}, RED);
1056+
}, RED, true);
10551057
});
10561058
}

0 commit comments

Comments
 (0)