Skip to content

Commit

Permalink
feat(plugin): add a way to know if there are connected nodes without …
Browse files Browse the repository at this point in the history
…asking their capabilities.
  • Loading branch information
matey97 committed Aug 2, 2022
1 parent 46d490e commit ba0fc47
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ export const defaultConfig = {

### [`NodeDiscoverer`](src/internal/node/discoverer/node-discoverer.android.ts)

| Function | Return type | Description |
|---------------------------------------------|----------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| `getLocalNode()` | `Promise<Node>` | Get a reference to the local node (smartphone). |
| `getConnectedNodes(timeout: number = 5000)` | `Promise<NodeDiscovered[]` | Get the currently connected nodes. Timeout indicates the maximum wait time for the connected nodes to communicate with the smartphone. |
| Function | Return type | Description |
|---------------------------------------------|----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `getLocalNode()` | `Promise<Node>` | Get a reference to the local node (smartphone). |
| `areConnectedNodes()` | `Promise<boolean>` | Returns true if there are connected nodes. |
| `getConnectedNodes(timeout: number = 5000)` | `Promise<NodeDiscovered[]` | Get the currently connected nodes and their available sensors. Timeout indicates the maximum wait time for the connected nodes to communicate with the smartphone. |

#### [`Node`](src/internal/node/index.ts)

Expand Down
16 changes: 16 additions & 0 deletions src/internal/node/discoverer/node-discoverer.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ export class AndroidNodeDiscoverer implements NodeDiscoverer {
});
}

public areConnectedNodes(): Promise<boolean> {
const connectedNodes = this.nodeClient.getConnectedNodes();
return new Promise<boolean>((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<NodeDiscovered[]> {
return firstValueFrom(
this.scanConnectedNodes(timeout)
Expand Down
1 change: 1 addition & 0 deletions src/internal/node/discoverer/node-discoverer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Node } from "../index";

export interface NodeDiscoverer {
getLocalNode(): Promise<Node>;
areConnectedNodes(): Promise<boolean>;
getConnectedNodes(timeout?: number): Promise<NodeDiscovered[]>;
}

Expand Down

0 comments on commit ba0fc47

Please sign in to comment.