From ba0fc47c140e080cb82213b3ad975c5a9b01c934 Mon Sep 17 00:00:00 2001 From: Miguel Matey Sanz Date: Tue, 2 Aug 2022 15:26:40 +0200 Subject: [PATCH] feat(plugin): add a way to know if there are connected nodes without asking their capabilities. --- README.md | 9 +++++---- .../node/discoverer/node-discoverer.android.ts | 16 ++++++++++++++++ src/internal/node/discoverer/node-discoverer.ts | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3491e8f..edccd98 100644 --- a/README.md +++ b/README.md @@ -255,10 +255,11 @@ export const defaultConfig = { ### [`NodeDiscoverer`](src/internal/node/discoverer/node-discoverer.android.ts) -| Function | Return type | Description | -|---------------------------------------------|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------| -| `getLocalNode()` | `Promise` | Get a reference to the local node (smartphone). | -| `getConnectedNodes(timeout: number = 5000)` | `Promise` | Get a reference to the local node (smartphone). | +| `areConnectedNodes()` | `Promise` | Returns true if there are connected nodes. | +| `getConnectedNodes(timeout: number = 5000)` | `Promise { + const connectedNodes = this.nodeClient.getConnectedNodes(); + return new Promise((resolve) => { + connectedNodes.addOnCompleteListener(new OnCompleteListener({ + onComplete: task => { + if (!task.isSuccessful()) { + return resolve(false); + } + + const nodes = task.getResult(); + resolve(nodes.size() !== 0); + } + })); + }); + } + public getConnectedNodes(timeout: number = 5000): Promise { return firstValueFrom( this.scanConnectedNodes(timeout) diff --git a/src/internal/node/discoverer/node-discoverer.ts b/src/internal/node/discoverer/node-discoverer.ts index cdfdcee..ff94da0 100644 --- a/src/internal/node/discoverer/node-discoverer.ts +++ b/src/internal/node/discoverer/node-discoverer.ts @@ -2,6 +2,7 @@ import { Node } from "../index"; export interface NodeDiscoverer { getLocalNode(): Promise; + areConnectedNodes(): Promise; getConnectedNodes(timeout?: number): Promise; }