Skip to content

Commit d4c583c

Browse files
author
Daisuke Baba
committed
Add scanStart/scanStop/scanRestart commands
1 parent 6c0c7b9 commit d4c583c

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ The node should still work on macOS and Windows as nothing is modified for these
1111

1212
Supported operations are as follows:
1313

14+
- Start BLE Scanning
15+
- Stop BLE Scanning
16+
- Restart BLE Scanning (Stop then start BLE Scanning again)
17+
- Connect to a peripheral device
18+
- Disonnect from a peripheral device
1419
- Read
1520
- Write
1621
- Write without Response

src/generic-ble.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ function startBLEScanning(RED) {
184184
noble.addListener('error', handlers.onError);
185185

186186
if (noble.state === 'poweredOn') {
187+
RED.log.info(`[GenericBLE] Start BLE scanning`);
187188
noble.startScanning([], true);
188189
genericBleState.scanning = true;
189190
} else {
@@ -803,7 +804,19 @@ module.exports = function (RED) {
803804
// ignore
804805
}
805806
try {
806-
if (msg.topic === 'connect') {
807+
if (msg.topic === 'scanStart') {
808+
startBLEScanning(RED);
809+
return;
810+
} else if (msg.topic === 'scanStop') {
811+
stopBLEScanning(RED);
812+
return;
813+
} else if (msg.topic === 'scanRestart') {
814+
stopBLEScanning(RED);
815+
setTimeout(() => {
816+
startBLEScanning(RED);
817+
}, 1000);
818+
return;
819+
} else if (msg.topic === 'connect') {
807820
await this.genericBleNode.connectPeripheral();
808821
} else if (msg.topic === 'disconnect') {
809822
await this.genericBleNode.disconnectPeripheral();

src/locales/en-US/generic-ble.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,28 @@ <h3>Inputs</h3>
2222
<p>
2323
Expects a message used for peripheral connecting/disconnecting operation, triggering <code>Read</code> operation, or subscribing <code>Notify</code> events.
2424
<dl>
25-
<dt>For <code>Connect</code> operation:</dt>
25+
<dt>For <code>scanStart</code> operation:</dt>
26+
<dd>
27+
In order to start BLE scanning to discover peripheral devices, set <code>msg.topic</code> to <code>scanStart</code>.
28+
<code>msg.payload</code> is always ignored. This operation doesn't emit anything from the Output port.
29+
</dd>
30+
<dt>For <code>scanStop</code> operation:</dt>
31+
<dd>
32+
In order to stop BLE scanning, set <code>msg.topic</code> to <code>scanStop</code>.
33+
<code>msg.payload</code> is always ignored. This operation doesn't emit anything from the Output port.
34+
</dd>
35+
<dt>For <code>scanRestart</code> operation:</dt>
36+
<dd>
37+
In order to restart BLE scanning, set <code>msg.topic</code> to <code>scanRestart</code>.
38+
This operation performs <code>ScanStop</code> then <code>ScanStart</code>.
39+
<code>msg.payload</code> is always ignored. This operation doesn't emit anything from the Output port.
40+
</dd>
41+
<dt>For <code>connect</code> operation:</dt>
2642
<dd>
2743
In order to connect a peripheral device, set <code>msg.topic</code> to <code>connect</code>.
2844
<code>msg.payload</code> is always ignored. This operation doesn't emit anything from the Output port.
2945
</dd>
30-
<dt>For <code>Disonnect</code> operation:</dt>
46+
<dt>For <code>disconnect</code> operation:</dt>
3147
<dd>
3248
In order to disconnect a peripheral device, set <code>msg.topic</code> to <code>disconnect</code>.
3349
<code>msg.payload</code> is always ignored. This operation doesn't emit anything from the Output port.

0 commit comments

Comments
 (0)