-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-discoverer.android.ts
116 lines (104 loc) · 4.74 KB
/
node-discoverer.android.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {NodeDiscovered, NodeDiscoverer} from "./node-discoverer";
import { Application } from "@nativescript/core";
import { WearOsNode, wearOS } from "../../utils/android/wear-os-types.android";
import OnCompleteListener = com.google.android.gms.tasks.OnCompleteListener;
import { Node } from "../index";
import { CapabilityClient } from "../../communication/capabilities/android/capability-client.android";
import { getCapabilityAdvertiserResultService } from "../../communication/capabilities/android/capability-advertiser-result-service.android";
import { capabilityProtocol } from "../../communication/capabilities";
import { EMPTY, firstValueFrom, from, Observable, toArray } from "rxjs";
import { catchError, switchMap } from "rxjs/operators";
export class AndroidNodeDiscoverer implements NodeDiscoverer {
constructor(
private capabilityClient: CapabilityClient = new CapabilityClient(
capabilityProtocol, getCapabilityAdvertiserResultService()
),
private nodeClient: wearOS.NodeClient = wearOS.Wearable.getNodeClient(Application.android.context)
) {
}
public getLocalNode(): Promise<Node> {
const localNode = this.nodeClient.getLocalNode();
return new Promise<Node>((resolve, reject) => {
localNode.addOnCompleteListener(new OnCompleteListener({
onComplete: task => {
if (task.isSuccessful()) {
const nativeNode = task.getResult();
const node = new Node(nativeNode.getId(), nativeNode.getDisplayName());
resolve(node);
} else {
reject(task.getException().getMessage());
}
}
}));
});
}
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)
.pipe(toArray())
);
}
private scanConnectedNodes(timeout: number): Observable<NodeDiscovered> {
const connectedNodes = this.nodeClient.getConnectedNodes();
const connectedNodesPromise = new Promise<java.util.List<WearOsNode>>((resolve, reject) => {
connectedNodes.addOnCompleteListener(new OnCompleteListener({
onComplete: task => {
if (task.isSuccessful()) {
resolve(task.getResult());
} else {
reject(task.getException().getMessage());
}
}
}));
});
return from(connectedNodesPromise).pipe(
switchMap((wearosNodes: java.util.List<WearOsNode>) => {
const totalNodes = wearosNodes.size();
if (totalNodes === 0)
return EMPTY;
return new Observable<NodeDiscovered>(subscriber => {
const iterator = wearosNodes.iterator();
let responseCount = 0;
while (iterator.hasNext()) {
const nativeNode = iterator.next();
const node = new Node(
nativeNode.getId(),
nativeNode.getDisplayName(),
);
this.capabilityClient.sendCapabilityAdvertisementRequest(node, timeout)
.then((capabilityResult) => {
node.capabilities = capabilityResult.capabilities;
subscriber.next({
node: node,
});
}).catch((error) => {
console.log(error);
subscriber.next({
node: node,
error: error,
});
}).finally(() => {
responseCount++;
if (responseCount === totalNodes)
subscriber.complete();
});
}
});
}), catchError((() => EMPTY))
);
}
}