@cldmv/node-android-tv-remote 1.5.5 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +297 -17
- package/index.cjs +30 -0
- package/index.mjs +24 -0
- package/package.json +26 -5
- package/scripts/{postinstall.js → postinstall.mjs} +15 -2
- package/scripts/{setup-device.js → setup-device.mjs} +15 -5
- package/scripts/wake-tv.mjs +51 -0
- package/src/data/keyboard-keys.json +73 -0
- package/src/lib/android-tv-remote.mjs +1804 -0
- package/.gitattributes +0 -8
- package/.prettierrc +0 -15
- package/docs/KEYBOARD_KEYS.md +0 -80
- package/docs/KEYCODES.md +0 -55
- package/eslint.config.mjs +0 -45
- package/scripts/test-keycodes.js +0 -0
- package/src/lib/adb/setup.js +0 -144
- package/src/lib/android-tv-remote.js +0 -859
- package/test/android-tv-remote.test.js +0 -74
- package/test/api-surface.test.js +0 -50
- package/test/connect-readall.js +0 -35
- package/test/interactive-remote.js +0 -69
|
@@ -1,859 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
/**
|
|
3
|
-
* Android TV Remote module.
|
|
4
|
-
* @module android-tv-remote
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* @typedef {Object} RemoteConfig
|
|
8
|
-
* @property {string} ip - The IP address of the device.
|
|
9
|
-
* @property {number} [port=5555] - The port for ADB connection.
|
|
10
|
-
* @property {string} [inputDevice="/dev/input/event0"] - The input device path.
|
|
11
|
-
* @property {boolean} [autoConnect=true] - Whether to auto-connect on command.
|
|
12
|
-
* @property {boolean} [autoDisconnect=false] - Whether to auto-disconnect after inactivity.
|
|
13
|
-
* @property {number} [disconnectTimeout=10] - Inactivity timeout in seconds before disconnecting.
|
|
14
|
-
* @property {boolean} [maintainConnection=true] - Whether to maintain the ADB connection with a heartbeat.
|
|
15
|
-
* @property {number} [heartbeatInterval=30000] - Heartbeat interval in ms (default 30s).
|
|
16
|
-
* @property {number} [connectionCheckInterval=30000] - Interval in ms for periodic connection checks (default 30s).
|
|
17
|
-
* @property {boolean} [quiet=true] - Suppress connection log output if true.
|
|
18
|
-
*/
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {Object} Remote
|
|
21
|
-
* @property {function(string, boolean=): Promise<void>} handleSettings - Get or set Android settings via ADB.
|
|
22
|
-
* @property {function(function(Error=, any=)=): Promise|undefined} connect - Connect to the device. Supports both promise and callback styles.
|
|
23
|
-
* @property {function(function(Error=, any=)=): Promise|undefined} disconnect - Disconnect from the device. Supports both promise and callback styles.
|
|
24
|
-
* @property {function(number, function(Error=, any=)=): Promise|undefined} inputKeycode - Send a keycode to the device. Supports both promise and callback styles.
|
|
25
|
-
* @property {function(boolean=): Promise<"connected"|"disconnected"|"unknown">} getConnectionStatus - Returns the current connection status; optionally performs a live check.
|
|
26
|
-
* @property {boolean} isConnected - True if the module believes it is connected (internal state, not a live check).
|
|
27
|
-
* @property {Object} press - Remote control key functions for Android TV remotes.
|
|
28
|
-
* @property {Object} keyboard - Keyboard interface for all keys, with text and keycode fallback.
|
|
29
|
-
* @property {Object} keyboard.key - Contains all key functions.
|
|
30
|
-
* @property {Object} keyboard.key.shift - Contains all shifted key functions.
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
const adb = require("adbkit");
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Logs a message with a datetime prefix.
|
|
37
|
-
* @private
|
|
38
|
-
* @param {...any} args - Arguments to log.
|
|
39
|
-
*/
|
|
40
|
-
function logWithTime(...args) {
|
|
41
|
-
const now = new Date().toISOString();
|
|
42
|
-
console.log(`[${now}]`, ...args);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Logs a warning with a datetime prefix.
|
|
47
|
-
* @private
|
|
48
|
-
* @param {...any} args - Arguments to warn.
|
|
49
|
-
*/
|
|
50
|
-
function warnWithTime(...args) {
|
|
51
|
-
const now = new Date().toISOString();
|
|
52
|
-
console.warn(`[${now}]`, ...args);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Logs an error with a datetime prefix.
|
|
57
|
-
* @private
|
|
58
|
-
* @param {...any} args - Arguments to error.
|
|
59
|
-
*/
|
|
60
|
-
function errorWithTime(...args) {
|
|
61
|
-
const now = new Date().toISOString();
|
|
62
|
-
console.error(`[${now}]`, ...args);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Wraps an async function to support both promise and callback styles.
|
|
67
|
-
* @internal
|
|
68
|
-
* @param {Function} fn - The async function to wrap.
|
|
69
|
-
* @returns {Function} A function supporting both promise and callback usage.
|
|
70
|
-
*/
|
|
71
|
-
function wrapAsync(fn) {
|
|
72
|
-
return function (...args) {
|
|
73
|
-
const cb = typeof args[args.length - 1] === "function" ? args.pop() : null;
|
|
74
|
-
const p = fn.apply(this, args);
|
|
75
|
-
if (cb) {
|
|
76
|
-
p.then((result) => cb(null, result)).catch((err) => cb(err));
|
|
77
|
-
} else {
|
|
78
|
-
return p;
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Handles disconnect and connection errors, outputs helpful messages, and exits the process.
|
|
85
|
-
* Also provides onboarding steps for common authentication and connection issues.
|
|
86
|
-
* @private
|
|
87
|
-
* @param {Error} err - The error object.
|
|
88
|
-
* @example
|
|
89
|
-
* try {
|
|
90
|
-
* // ...code that may throw
|
|
91
|
-
* } catch (err) {
|
|
92
|
-
* handleDisconnectError(err);
|
|
93
|
-
* }
|
|
94
|
-
*/
|
|
95
|
-
function handleDisconnectError(err) {
|
|
96
|
-
errorWithTime("Error:", err.message || err);
|
|
97
|
-
if (err.message && (err.message.includes("device unauthorized") || err.message.includes("failed to authenticate"))) {
|
|
98
|
-
errorWithTime(
|
|
99
|
-
"Your device is unauthorized or failed to authenticate. Please check your TV and accept the authorization dialog to allow this system to connect via ADB."
|
|
100
|
-
);
|
|
101
|
-
errorWithTime("If you do not see a prompt, try disconnecting and reconnecting the device, or reboot your TV.");
|
|
102
|
-
errorWithTime("If the problem persists, remove the device from the list of authorized ADB devices in Developer Options and try again.");
|
|
103
|
-
errorWithTime(
|
|
104
|
-
"Tip: In Developer Options on your TV, try toggling 'ADB Debugging' off and then back on. This often resolves authentication issues."
|
|
105
|
-
);
|
|
106
|
-
process.exit(1);
|
|
107
|
-
}
|
|
108
|
-
if (err.message && (err.message.includes("actively refused") || err.message.includes("No connection could be made"))) {
|
|
109
|
-
errorWithTime("\nThe device refused the connection. To enable ADB, follow these steps on your Android TV or Fire TV:");
|
|
110
|
-
errorWithTime("1. Open Settings > Device Preferences > About (or My Fire TV > About)");
|
|
111
|
-
errorWithTime("2. Scroll to 'Build' and press OK 7 times to enable Developer Options");
|
|
112
|
-
errorWithTime("3. Go back to Settings > Device Preferences > Developer Options");
|
|
113
|
-
errorWithTime("4. Enable 'Developer Options' if needed, then enable 'ADB Debugging' and 'Apps from Unknown Sources'");
|
|
114
|
-
errorWithTime("5. Ensure your TV and computer are on the same network");
|
|
115
|
-
errorWithTime("6. On your computer, run: adb connect <device-ip>:5555");
|
|
116
|
-
errorWithTime("7. Accept the authorization prompt on your TV");
|
|
117
|
-
errorWithTime("\nIf you do not see 'Developer Options', repeat step 2 until it appears.");
|
|
118
|
-
process.exit(1);
|
|
119
|
-
}
|
|
120
|
-
return err;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Android keycodes mapping loaded from JSON file.
|
|
125
|
-
* @internal
|
|
126
|
-
* @type {Object.<string, number>}
|
|
127
|
-
* @see https://developer.android.com/reference/android/view/KeyEvent
|
|
128
|
-
*/
|
|
129
|
-
const keycodes = require("../data/keycodes.json");
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Remote keys mapping loaded from JSON file.
|
|
133
|
-
* @internal
|
|
134
|
-
* @type {string[]}
|
|
135
|
-
*/
|
|
136
|
-
const remoteKeys = require("../data/remote-keys.json");
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Factory function to create a Remote instance.
|
|
140
|
-
* @function
|
|
141
|
-
* @public
|
|
142
|
-
* @param {RemoteConfig} config - Configuration for the remote.
|
|
143
|
-
* @returns {Remote}
|
|
144
|
-
*/
|
|
145
|
-
module.exports = function (config) {
|
|
146
|
-
// Ensure config is an object and has the required 'ip' property
|
|
147
|
-
if (!config || typeof config !== "object" || !config.ip) {
|
|
148
|
-
throw new Error("Missing required 'ip' property in RemoteConfig.");
|
|
149
|
-
}
|
|
150
|
-
const ip = config.ip;
|
|
151
|
-
const port = config.port || 5555;
|
|
152
|
-
const inputDevice = config.inputDevice || "/dev/input/event0";
|
|
153
|
-
const host = ip + ":" + port;
|
|
154
|
-
const client = adb.createClient();
|
|
155
|
-
let connected = false;
|
|
156
|
-
const autoConnect = config.autoConnect !== false; // default true
|
|
157
|
-
const autoDisconnect = config.autoDisconnect === true; // default false
|
|
158
|
-
const disconnectTimeout = typeof config.disconnectTimeout === "number" ? config.disconnectTimeout : 10;
|
|
159
|
-
const connectionCheckInterval = typeof config.connectionCheckInterval === "number" ? config.connectionCheckInterval : 30000;
|
|
160
|
-
let connectionCheckTimer = null;
|
|
161
|
-
let disconnectTimer = null;
|
|
162
|
-
const quiet = config.quiet !== false; // default true
|
|
163
|
-
const maintainConnection = config.maintainConnection !== false; // default true
|
|
164
|
-
const heartbeatInterval = typeof config.heartbeatInterval === "number" ? config.heartbeatInterval : 30000;
|
|
165
|
-
let heartbeatTimer = null;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* On initialization, check if already connected to the device and set internal state.
|
|
169
|
-
* This ensures the internal state is correct if the device is already connected.
|
|
170
|
-
* Expose a promise (initPromise) that resolves or rejects based on the result.
|
|
171
|
-
*/
|
|
172
|
-
/**
|
|
173
|
-
* Internal resolve/reject for initPromise, always defined as functions.
|
|
174
|
-
*/
|
|
175
|
-
let initPromiseResolve = (_v) => {};
|
|
176
|
-
let initPromiseReject = (_e) => {};
|
|
177
|
-
/**
|
|
178
|
-
* Promise that resolves if initial auto-connect succeeds, or rejects if it fails.
|
|
179
|
-
* @type {Promise<void>}
|
|
180
|
-
* @example
|
|
181
|
-
* remote.initPromise.then(() => { ... }).catch((err) => { ... });
|
|
182
|
-
*/
|
|
183
|
-
const initPromise = new Promise((resolve, reject) => {
|
|
184
|
-
initPromiseResolve = resolve;
|
|
185
|
-
initPromiseReject = reject;
|
|
186
|
-
});
|
|
187
|
-
(async () => {
|
|
188
|
-
try {
|
|
189
|
-
const status = await getConnectionStatus(true);
|
|
190
|
-
if (status === "connected") {
|
|
191
|
-
connected = true;
|
|
192
|
-
if (!quiet) logWithTime("Already connected to " + host + " (on init)");
|
|
193
|
-
startHeartbeat();
|
|
194
|
-
initPromiseResolve();
|
|
195
|
-
} else {
|
|
196
|
-
// Try to connect if not already connected
|
|
197
|
-
await connect();
|
|
198
|
-
if (connected) {
|
|
199
|
-
initPromiseResolve();
|
|
200
|
-
} else {
|
|
201
|
-
initPromiseReject(new Error("Failed to connect to device on initialization."));
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
} catch (e) {
|
|
205
|
-
initPromiseReject(e);
|
|
206
|
-
}
|
|
207
|
-
})();
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Resets the disconnect timer if autoDisconnect is enabled.
|
|
211
|
-
* Disconnects after inactivity if enabled.
|
|
212
|
-
* @private
|
|
213
|
-
*/
|
|
214
|
-
function resetDisconnectTimer() {
|
|
215
|
-
if (!autoDisconnect) return;
|
|
216
|
-
if (disconnectTimer) clearTimeout(disconnectTimer);
|
|
217
|
-
disconnectTimer = setTimeout(function () {
|
|
218
|
-
if (connected) disconnect();
|
|
219
|
-
}, disconnectTimeout * 1000);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Starts the heartbeat timer to maintain the ADB connection.
|
|
224
|
-
* Periodically sends a shell command to keep the connection alive.
|
|
225
|
-
* Also starts the periodic connection check for auto-reconnect.
|
|
226
|
-
* @private
|
|
227
|
-
*/
|
|
228
|
-
function startHeartbeat() {
|
|
229
|
-
if (!maintainConnection) return;
|
|
230
|
-
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
|
231
|
-
heartbeatTimer = setInterval(() => {
|
|
232
|
-
if (!connected) return;
|
|
233
|
-
// Send a no-op shell command to keep the connection alive
|
|
234
|
-
client.shell(host, "echo heartbeat").catch(() => {});
|
|
235
|
-
}, heartbeatInterval);
|
|
236
|
-
startConnectionCheck();
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Starts the periodic connection check. If disconnected, attempts to reconnect.
|
|
241
|
-
* Ensures persistent, self-healing ADB connection.
|
|
242
|
-
* @private
|
|
243
|
-
*/
|
|
244
|
-
function startConnectionCheck() {
|
|
245
|
-
if (connectionCheckTimer) clearInterval(connectionCheckTimer);
|
|
246
|
-
connectionCheckTimer = setInterval(async () => {
|
|
247
|
-
if (!connected) return;
|
|
248
|
-
try {
|
|
249
|
-
const devices = await client.listDevices();
|
|
250
|
-
const deviceId = host;
|
|
251
|
-
const found = devices.some((d) => d.id === deviceId);
|
|
252
|
-
if (!found) {
|
|
253
|
-
warnWithTime(`Device ${deviceId} not found in adb devices list. Attempting reconnect...`);
|
|
254
|
-
connected = false;
|
|
255
|
-
await connect();
|
|
256
|
-
}
|
|
257
|
-
} catch (err) {
|
|
258
|
-
warnWithTime("Error checking device connection:", err.message || err);
|
|
259
|
-
}
|
|
260
|
-
}, connectionCheckInterval);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Stops the heartbeat and connection check timers.
|
|
265
|
-
* @private
|
|
266
|
-
*/
|
|
267
|
-
function stopHeartbeat() {
|
|
268
|
-
if (heartbeatTimer) {
|
|
269
|
-
clearInterval(heartbeatTimer);
|
|
270
|
-
heartbeatTimer = null;
|
|
271
|
-
}
|
|
272
|
-
if (connectionCheckTimer) {
|
|
273
|
-
clearInterval(connectionCheckTimer);
|
|
274
|
-
connectionCheckTimer = null;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Ensures connection if autoConnect is enabled.
|
|
280
|
-
* Used internally before sending commands.
|
|
281
|
-
* @internal
|
|
282
|
-
* @returns {Promise<void>}
|
|
283
|
-
*/
|
|
284
|
-
function ensureConnected() {
|
|
285
|
-
if (!autoConnect) return Promise.resolve();
|
|
286
|
-
if (connected) return Promise.resolve();
|
|
287
|
-
return connect();
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Connects to the ADB device if not already connected.
|
|
292
|
-
* Checks the internal connection state before connecting.
|
|
293
|
-
* @returns {Promise<void>}
|
|
294
|
-
* @example
|
|
295
|
-
* await remote.connect(); // Only connects if not already connected
|
|
296
|
-
*/
|
|
297
|
-
function connect() {
|
|
298
|
-
if (connected) {
|
|
299
|
-
if (!quiet) logWithTime("Already connected to " + host);
|
|
300
|
-
return Promise.resolve();
|
|
301
|
-
}
|
|
302
|
-
return client
|
|
303
|
-
.connect(ip, port)
|
|
304
|
-
.then(function () {
|
|
305
|
-
connected = true;
|
|
306
|
-
if (!quiet) logWithTime("Connected to " + host);
|
|
307
|
-
startHeartbeat();
|
|
308
|
-
})
|
|
309
|
-
.catch(function (err) {
|
|
310
|
-
if (err.message && err.message.includes("already connected")) {
|
|
311
|
-
if (!quiet) warnWithTime("Warning: Device already connected.");
|
|
312
|
-
connected = true;
|
|
313
|
-
startHeartbeat();
|
|
314
|
-
return true;
|
|
315
|
-
}
|
|
316
|
-
return handleDisconnectError(err);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
const connectWrapped = wrapAsync(connect);
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Disconnects from the ADB device and stops heartbeat/connection check.
|
|
323
|
-
* Handles already-disconnected state gracefully.
|
|
324
|
-
* @internal
|
|
325
|
-
* @returns {Promise<void>}
|
|
326
|
-
*/
|
|
327
|
-
function disconnect() {
|
|
328
|
-
if (disconnectTimer) {
|
|
329
|
-
clearTimeout(disconnectTimer);
|
|
330
|
-
disconnectTimer = null;
|
|
331
|
-
}
|
|
332
|
-
stopHeartbeat();
|
|
333
|
-
return client
|
|
334
|
-
.disconnect(ip, port)
|
|
335
|
-
.then(function () {
|
|
336
|
-
connected = false;
|
|
337
|
-
if (!quiet) logWithTime("Disconnected from " + host);
|
|
338
|
-
return true;
|
|
339
|
-
})
|
|
340
|
-
.catch(function (err) {
|
|
341
|
-
if (err.message && err.message.includes("disconnected")) {
|
|
342
|
-
if (!quiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
343
|
-
connected = false;
|
|
344
|
-
return true;
|
|
345
|
-
}
|
|
346
|
-
return handleDisconnectError(err);
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
const disconnectWrapped = wrapAsync(disconnect);
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Sends a keycode to the device, auto-connects/disconnects as needed.
|
|
353
|
-
* @internal
|
|
354
|
-
* @param {number} code - The Android keycode to send.
|
|
355
|
-
* @returns {Promise}
|
|
356
|
-
*/
|
|
357
|
-
function inputKeycode(code) {
|
|
358
|
-
return ensureConnected().then(function () {
|
|
359
|
-
resetDisconnectTimer();
|
|
360
|
-
return client.shell(host, "input keyevent " + code);
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
const inputKeycodeWrapped = wrapAsync(inputKeycode);
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Sends a long press keycode to the device, auto-connects/disconnects as needed.
|
|
367
|
-
* @internal
|
|
368
|
-
* @param {number} code - The Android keycode to send as a long press.
|
|
369
|
-
* @returns {Promise}
|
|
370
|
-
*/
|
|
371
|
-
function inputKeycodeLongPress(code) {
|
|
372
|
-
const cmd = "sendevent " + inputDevice + " 1 " + code + " 1 && " + "sleep 1 && " + "sendevent " + inputDevice + " 1 " + code + " 0";
|
|
373
|
-
return ensureConnected().then(function () {
|
|
374
|
-
resetDisconnectTimer();
|
|
375
|
-
return client.shell(host, cmd);
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
const inputKeycodeLongPressWrapped = wrapAsync(inputKeycodeLongPress);
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* Sends text input to the device, auto-connects/disconnects as needed.
|
|
382
|
-
* @internal
|
|
383
|
-
* @param {string} text - The text to input.
|
|
384
|
-
* @returns {Promise}
|
|
385
|
-
*/
|
|
386
|
-
function inputText(text) {
|
|
387
|
-
const escaped = text.replace(/ /g, "%s");
|
|
388
|
-
return ensureConnected().then(function () {
|
|
389
|
-
resetDisconnectTimer();
|
|
390
|
-
return client.shell(host, 'input text "' + escaped + '"');
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
const inputTextWrapped = wrapAsync(inputText);
|
|
394
|
-
|
|
395
|
-
const keyboardKeys = {};
|
|
396
|
-
const keyboardShiftKeys = {};
|
|
397
|
-
|
|
398
|
-
for (let i = 0; i < 26; i++) {
|
|
399
|
-
const letter = String.fromCharCode(97 + i);
|
|
400
|
-
const code = 29 + i;
|
|
401
|
-
keyboardKeys[letter] = (function (code) {
|
|
402
|
-
return function () {
|
|
403
|
-
return inputKeycode(code);
|
|
404
|
-
};
|
|
405
|
-
})(code);
|
|
406
|
-
keyboardShiftKeys[letter] = (function (code) {
|
|
407
|
-
return function () {
|
|
408
|
-
return inputKeycode(keycodes.shiftLeft).then(function () {
|
|
409
|
-
return inputKeycode(code);
|
|
410
|
-
});
|
|
411
|
-
};
|
|
412
|
-
})(code);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
keyboardKeys.enter = function () {
|
|
416
|
-
return inputKeycode(keycodes.enter);
|
|
417
|
-
};
|
|
418
|
-
keyboardKeys.space = function () {
|
|
419
|
-
return inputKeycode(keycodes.space);
|
|
420
|
-
};
|
|
421
|
-
keyboardKeys.del = function () {
|
|
422
|
-
return inputKeycode(keycodes.del);
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
keyboardShiftKeys.enter = function () {
|
|
426
|
-
return inputKeycode(keycodes.shiftLeft).then(function () {
|
|
427
|
-
return inputKeycode(keycodes.enter);
|
|
428
|
-
});
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Returns the current connection status as tracked by the module.
|
|
433
|
-
* Optionally performs a live check against adb devices if requested.
|
|
434
|
-
* Also updates the internal connected state if a live check is performed.
|
|
435
|
-
* @public
|
|
436
|
-
* @param {boolean} [liveCheck=false] - If true, checks adb devices for actual connection.
|
|
437
|
-
* @returns {Promise<"connected"|"disconnected"|"unknown">}
|
|
438
|
-
* @example
|
|
439
|
-
* await remote.getConnectionStatus();
|
|
440
|
-
* await remote.getConnectionStatus(true); // live check
|
|
441
|
-
*/
|
|
442
|
-
async function getConnectionStatus(liveCheck = false) {
|
|
443
|
-
if (!liveCheck) {
|
|
444
|
-
return connected ? "connected" : "disconnected";
|
|
445
|
-
}
|
|
446
|
-
try {
|
|
447
|
-
const devices = await client.listDevices();
|
|
448
|
-
const deviceId = host;
|
|
449
|
-
const found = devices.some((d) => d.id === deviceId);
|
|
450
|
-
connected = found; // Keep internal state in sync with live check
|
|
451
|
-
return found ? "connected" : "disconnected";
|
|
452
|
-
} catch {
|
|
453
|
-
connected = false;
|
|
454
|
-
return "unknown";
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Returns true if the module believes it is connected to the device.
|
|
460
|
-
* This is a fast check of internal state, not a live ADB query.
|
|
461
|
-
* @public
|
|
462
|
-
* @returns {boolean}
|
|
463
|
-
* @example
|
|
464
|
-
* if (remote.isConnected) { ... }
|
|
465
|
-
*/
|
|
466
|
-
function isConnected() {
|
|
467
|
-
return !!connected;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Returns a list of all available keyboard key function names on the live API (keyboard.key).
|
|
472
|
-
* This inspects the actual keyboard.key object, so it always matches the real API surface, including all aliases and dynamic keys.
|
|
473
|
-
* @returns {string[]}
|
|
474
|
-
* @example
|
|
475
|
-
* remote.getKeyboardKeys();
|
|
476
|
-
*/
|
|
477
|
-
function getKeyboardKeys() {
|
|
478
|
-
// 'this' is not bound, so we must access the constructed keyboard.key object
|
|
479
|
-
// Only return the top-level key names (not .keycode or .shift)
|
|
480
|
-
if (!this || !this.keyboard || !this.keyboard.key) return [];
|
|
481
|
-
return Object.keys(this.keyboard.key).filter((k) => typeof this.keyboard.key[k] === "function");
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
|
|
486
|
-
* This inspects the actual press object, so it always matches the real API surface.
|
|
487
|
-
* @returns {string[]}
|
|
488
|
-
* @example
|
|
489
|
-
* remote.getPressCommands();
|
|
490
|
-
*/
|
|
491
|
-
function getPressCommands() {
|
|
492
|
-
if (!this || !this.press) return [];
|
|
493
|
-
return Object.keys(this.press).filter((k) => k !== "long" && typeof this.press[k] === "function");
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
const remoteApi = {
|
|
497
|
-
/**
|
|
498
|
-
* Promise that resolves if initial auto-connect succeeds, or rejects if it fails.
|
|
499
|
-
* @type {Promise<void>}
|
|
500
|
-
* @example
|
|
501
|
-
* remote.initPromise.then(() => { ... }).catch((err) => { ... });
|
|
502
|
-
*/
|
|
503
|
-
initPromise,
|
|
504
|
-
/**
|
|
505
|
-
* Returns the current connection status as tracked by the module, or does a live check if requested.
|
|
506
|
-
* @function
|
|
507
|
-
* @param {boolean} [liveCheck=false] - If true, checks adb devices for actual connection.
|
|
508
|
-
* @returns {Promise<"connected"|"disconnected"|"unknown">}
|
|
509
|
-
* @example
|
|
510
|
-
* await remote.getConnectionStatus();
|
|
511
|
-
* await remote.getConnectionStatus(true); // live check
|
|
512
|
-
*/
|
|
513
|
-
getConnectionStatus,
|
|
514
|
-
/**
|
|
515
|
-
* Returns a list of all available keyboard key function names on the live API (keyboard.key).
|
|
516
|
-
* @returns {string[]}
|
|
517
|
-
* @example
|
|
518
|
-
* remote.getKeyboardKeys();
|
|
519
|
-
*/
|
|
520
|
-
getKeyboardKeys: function () {
|
|
521
|
-
return getKeyboardKeys.call(remoteApi);
|
|
522
|
-
},
|
|
523
|
-
/**
|
|
524
|
-
* Returns a list of all available press command names on the live API (press object), including aliases and dynamic keys.
|
|
525
|
-
* @returns {string[]}
|
|
526
|
-
* @example
|
|
527
|
-
* remote.getPressCommands();
|
|
528
|
-
*/
|
|
529
|
-
getPressCommands: function () {
|
|
530
|
-
return getPressCommands.call(remoteApi);
|
|
531
|
-
},
|
|
532
|
-
/**
|
|
533
|
-
* Returns true if the module believes it is connected to the device.
|
|
534
|
-
* @readonly
|
|
535
|
-
* @type {boolean}
|
|
536
|
-
* @example
|
|
537
|
-
* if (remote.isConnected) { ... }
|
|
538
|
-
*/
|
|
539
|
-
get isConnected() {
|
|
540
|
-
return isConnected();
|
|
541
|
-
},
|
|
542
|
-
/**
|
|
543
|
-
* Get or set Android settings via ADB.
|
|
544
|
-
* @public
|
|
545
|
-
* @param {string} mode - 'get' or 'set'.
|
|
546
|
-
* @param {boolean} [overrideQuiet] - Optionally override the quiet flag for this call.
|
|
547
|
-
* @returns {Promise<void>}
|
|
548
|
-
* @example
|
|
549
|
-
* remote.handleSettings('get', false);
|
|
550
|
-
*/
|
|
551
|
-
handleSettings: function (mode, overrideQuiet) {
|
|
552
|
-
const keys = [
|
|
553
|
-
{ ns: "system", key: "screen_off_timeout", value: 2147483647 },
|
|
554
|
-
{ ns: "secure", key: "sleep_timeout", value: 0 },
|
|
555
|
-
{ ns: "global", key: "stay_on_while_plugged_in", value: 3 }
|
|
556
|
-
];
|
|
557
|
-
const useQuiet = typeof overrideQuiet === "boolean" ? overrideQuiet : quiet;
|
|
558
|
-
return client
|
|
559
|
-
.connect(ip, port)
|
|
560
|
-
.then(() => {
|
|
561
|
-
if (!useQuiet) logWithTime(`Connected to ${host}`);
|
|
562
|
-
let chain = Promise.resolve();
|
|
563
|
-
keys.forEach((item) => {
|
|
564
|
-
chain = chain.then(() => {
|
|
565
|
-
let cmd;
|
|
566
|
-
if (mode === "set") {
|
|
567
|
-
cmd = `settings put ${item.ns} ${item.key} ${item.value}`;
|
|
568
|
-
} else {
|
|
569
|
-
cmd = `settings get ${item.ns} ${item.key}`;
|
|
570
|
-
}
|
|
571
|
-
return client
|
|
572
|
-
.shell(host, cmd)
|
|
573
|
-
.then(adb.util.readAll)
|
|
574
|
-
.then((result) => {
|
|
575
|
-
if (!useQuiet) {
|
|
576
|
-
if (mode === "set") {
|
|
577
|
-
logWithTime(`Set ${item.ns} ${item.key} to ${item.value}`);
|
|
578
|
-
} else {
|
|
579
|
-
logWithTime(`${item.ns} ${item.key}: ${result.toString().trim()}`);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
});
|
|
583
|
-
});
|
|
584
|
-
});
|
|
585
|
-
return chain;
|
|
586
|
-
})
|
|
587
|
-
.then(() => client.disconnect(ip, port))
|
|
588
|
-
.then(() => {
|
|
589
|
-
if (!useQuiet) logWithTime("Disconnected cleanly");
|
|
590
|
-
})
|
|
591
|
-
.catch((err) => {
|
|
592
|
-
if (err.message && err.message.includes("disconnected")) {
|
|
593
|
-
if (!useQuiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
594
|
-
} else {
|
|
595
|
-
return handleDisconnectError(err);
|
|
596
|
-
}
|
|
597
|
-
});
|
|
598
|
-
},
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Connect to the device. Supports both promise and callback styles.
|
|
602
|
-
* @public
|
|
603
|
-
* @param {function(Error=, any=)=} [cb]
|
|
604
|
-
* @returns {Promise|undefined}
|
|
605
|
-
* @example
|
|
606
|
-
* await remote.connect();
|
|
607
|
-
* remote.connect((err) => { ... });
|
|
608
|
-
*/
|
|
609
|
-
connect: /**
|
|
610
|
-
* @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
611
|
-
*/ (connectWrapped),
|
|
612
|
-
|
|
613
|
-
/**
|
|
614
|
-
* Disconnect from the device. Supports both promise and callback styles.
|
|
615
|
-
* @public
|
|
616
|
-
* @param {function(Error=, any=)=} [cb]
|
|
617
|
-
* @returns {Promise|undefined}
|
|
618
|
-
* @example
|
|
619
|
-
* await remote.disconnect();
|
|
620
|
-
* remote.disconnect((err) => { ... });
|
|
621
|
-
*/
|
|
622
|
-
disconnect: /**
|
|
623
|
-
* @type {(cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
624
|
-
*/ (disconnectWrapped),
|
|
625
|
-
|
|
626
|
-
/**
|
|
627
|
-
* Send a keycode. Supports both promise and callback styles.
|
|
628
|
-
* @public
|
|
629
|
-
* @param {number} code
|
|
630
|
-
* @param {function(Error=, any=)=} [cb]
|
|
631
|
-
* @returns {Promise|undefined}
|
|
632
|
-
* @example
|
|
633
|
-
* await remote.inputKeycode(23);
|
|
634
|
-
* remote.inputKeycode(23, (err) => { ... });
|
|
635
|
-
*/
|
|
636
|
-
inputKeycode: /**
|
|
637
|
-
* @type {(code: number, cb?: (err?: Error, result?: any) => any) => Promise<any> | undefined}
|
|
638
|
-
*/ (inputKeycodeWrapped),
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* All remote key functions support both promise and callback styles.
|
|
642
|
-
* @public
|
|
643
|
-
*/
|
|
644
|
-
press: (() => {
|
|
645
|
-
/**
|
|
646
|
-
* Dynamically generated remote control key functions for Android TV remotes.
|
|
647
|
-
* Keys are loaded from remote-keys.json. Color buttons are excluded.
|
|
648
|
-
* @namespace press
|
|
649
|
-
*/
|
|
650
|
-
const obj = {};
|
|
651
|
-
const longObj = {};
|
|
652
|
-
remoteKeys.forEach(function (key) {
|
|
653
|
-
/**
|
|
654
|
-
* Sends the corresponding keycode for this remote key.
|
|
655
|
-
* @returns {Promise}
|
|
656
|
-
* @example
|
|
657
|
-
* press.home();
|
|
658
|
-
*/
|
|
659
|
-
obj[key] = wrapAsync(function () {
|
|
660
|
-
// Special handling for ok/select
|
|
661
|
-
if (key === "ok" || key === "select") {
|
|
662
|
-
return inputKeycode(keycodes.dpadCenter || keycodes.ok);
|
|
663
|
-
}
|
|
664
|
-
// Map up/down/left/right to dpad keycodes
|
|
665
|
-
if (key === "up") {
|
|
666
|
-
return inputKeycode(keycodes.dpadUp);
|
|
667
|
-
}
|
|
668
|
-
if (key === "down") {
|
|
669
|
-
return inputKeycode(keycodes.dpadDown);
|
|
670
|
-
}
|
|
671
|
-
if (key === "left") {
|
|
672
|
-
return inputKeycode(keycodes.dpadLeft);
|
|
673
|
-
}
|
|
674
|
-
if (key === "right") {
|
|
675
|
-
return inputKeycode(keycodes.dpadRight);
|
|
676
|
-
}
|
|
677
|
-
// Special handling for play/pause
|
|
678
|
-
if (key === "play") {
|
|
679
|
-
return inputKeycode(keycodes.mediaPlay || keycodes.playPause);
|
|
680
|
-
}
|
|
681
|
-
if (key === "pause") {
|
|
682
|
-
return inputKeycode(keycodes.mediaPause || keycodes.playPause);
|
|
683
|
-
}
|
|
684
|
-
if (key === "volumeMute" || key === "mute") {
|
|
685
|
-
return inputKeycode(keycodes.volumeMute || keycodes.mute);
|
|
686
|
-
}
|
|
687
|
-
if (key === "input") {
|
|
688
|
-
return inputKeycode(keycodes.tvInput);
|
|
689
|
-
}
|
|
690
|
-
if (key.startsWith("number")) {
|
|
691
|
-
// e.g. number0
|
|
692
|
-
const num = key.replace("number", "");
|
|
693
|
-
return inputKeycode(keycodes[num]);
|
|
694
|
-
}
|
|
695
|
-
return inputKeycode(keycodes[key]);
|
|
696
|
-
});
|
|
697
|
-
|
|
698
|
-
/**
|
|
699
|
-
* Sends the corresponding keycode for this remote key as a long press.
|
|
700
|
-
* @returns {Promise}
|
|
701
|
-
* @example
|
|
702
|
-
* press.long.home();
|
|
703
|
-
*/
|
|
704
|
-
longObj[key] = wrapAsync(function () {
|
|
705
|
-
if (key === "ok" || key === "select") {
|
|
706
|
-
return inputKeycodeLongPressWrapped(keycodes.dpadCenter || keycodes.ok);
|
|
707
|
-
}
|
|
708
|
-
if (key === "up") {
|
|
709
|
-
return inputKeycodeLongPressWrapped(keycodes.dpadUp);
|
|
710
|
-
}
|
|
711
|
-
if (key === "down") {
|
|
712
|
-
return inputKeycodeLongPressWrapped(keycodes.dpadDown);
|
|
713
|
-
}
|
|
714
|
-
if (key === "left") {
|
|
715
|
-
return inputKeycodeLongPressWrapped(keycodes.dpadLeft);
|
|
716
|
-
}
|
|
717
|
-
if (key === "right") {
|
|
718
|
-
return inputKeycodeLongPressWrapped(keycodes.dpadRight);
|
|
719
|
-
}
|
|
720
|
-
if (key === "play") {
|
|
721
|
-
return inputKeycodeLongPressWrapped(keycodes.mediaPlay || keycodes.playPause);
|
|
722
|
-
}
|
|
723
|
-
if (key === "pause") {
|
|
724
|
-
return inputKeycodeLongPressWrapped(keycodes.mediaPause || keycodes.playPause);
|
|
725
|
-
}
|
|
726
|
-
if (key === "volumeMute" || key === "mute") {
|
|
727
|
-
return inputKeycodeLongPressWrapped(keycodes.volumeMute || keycodes.mute);
|
|
728
|
-
}
|
|
729
|
-
if (key === "input") {
|
|
730
|
-
return inputKeycodeLongPressWrapped(keycodes.tvInput);
|
|
731
|
-
}
|
|
732
|
-
if (key.startsWith("number")) {
|
|
733
|
-
const num = key.replace("number", "");
|
|
734
|
-
return inputKeycodeLongPressWrapped(keycodes[num]);
|
|
735
|
-
}
|
|
736
|
-
return inputKeycodeLongPressWrapped(keycodes[key]);
|
|
737
|
-
});
|
|
738
|
-
});
|
|
739
|
-
// Robust aliasing: always add if dpad keys exist in keycodes
|
|
740
|
-
// if (typeof obj.dpadUp === "function" || keycodes.dpadUp) obj.up = obj.dpadUp;
|
|
741
|
-
// if (typeof obj.dpadDown === "function" || keycodes.dpadDown) obj.down = obj.dpadDown;
|
|
742
|
-
// if (typeof obj.dpadLeft === "function" || keycodes.dpadLeft) obj.left = obj.dpadLeft;
|
|
743
|
-
// if (typeof obj.dpadRight === "function" || keycodes.dpadRight) obj.right = obj.dpadRight;
|
|
744
|
-
// if (typeof obj.dpadCenter === "function" || keycodes.dpadCenter) obj.ok = obj.dpadCenter;
|
|
745
|
-
// if (typeof longObj.dpadUp === "function") longObj.up = longObj.dpadUp;
|
|
746
|
-
// if (typeof longObj.dpadDown === "function") longObj.down = longObj.dpadDown;
|
|
747
|
-
// if (typeof longObj.dpadLeft === "function") longObj.left = longObj.dpadLeft;
|
|
748
|
-
// if (typeof longObj.dpadRight === "function") longObj.right = longObj.dpadRight;
|
|
749
|
-
// if (typeof longObj.dpadCenter === "function") longObj.ok = longObj.dpadCenter;
|
|
750
|
-
obj.long = longObj;
|
|
751
|
-
return obj;
|
|
752
|
-
})(),
|
|
753
|
-
|
|
754
|
-
/**
|
|
755
|
-
* Keyboard interface for all keys, with text and keycode fallback.
|
|
756
|
-
* @public
|
|
757
|
-
*/
|
|
758
|
-
keyboard: {
|
|
759
|
-
/**
|
|
760
|
-
* Sends text input to the device.
|
|
761
|
-
* @public
|
|
762
|
-
* @param {string} text - The text to input.
|
|
763
|
-
* @returns {Promise}
|
|
764
|
-
* @example
|
|
765
|
-
* keyboard.text('hello');
|
|
766
|
-
*/
|
|
767
|
-
text: inputTextWrapped,
|
|
768
|
-
|
|
769
|
-
/**
|
|
770
|
-
* Key subobject: callable and contains all key functions.
|
|
771
|
-
*/
|
|
772
|
-
key: Object.assign(
|
|
773
|
-
wrapAsync(function (keyName, options) {
|
|
774
|
-
options = options || {};
|
|
775
|
-
if (!keycodes[keyName]) {
|
|
776
|
-
return Promise.reject(new Error("Unknown key: " + keyName));
|
|
777
|
-
}
|
|
778
|
-
if (!options.forceKeycode) {
|
|
779
|
-
// Try inputText for single character keys
|
|
780
|
-
if (keyName.length === 1) {
|
|
781
|
-
return inputText(keyName);
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
// Fallback to keycode
|
|
785
|
-
return inputKeycode(keycodes[keyName]);
|
|
786
|
-
}),
|
|
787
|
-
{
|
|
788
|
-
// Dynamically add all key functions and .keycode subobject
|
|
789
|
-
...(() => {
|
|
790
|
-
const keyFns = {};
|
|
791
|
-
Object.keys(keycodes).forEach(function (keyName) {
|
|
792
|
-
/**
|
|
793
|
-
* Sends this key using inputText (if possible) or keycode fallback.
|
|
794
|
-
* @returns {Promise}
|
|
795
|
-
* @example
|
|
796
|
-
* keyboard.key.a();
|
|
797
|
-
*/
|
|
798
|
-
keyFns[keyName] = wrapAsync(function (options) {
|
|
799
|
-
options = options || {};
|
|
800
|
-
if (!options.forceKeycode && keyName.length === 1) {
|
|
801
|
-
return inputText(keyName);
|
|
802
|
-
}
|
|
803
|
-
return inputKeycode(keycodes[keyName]);
|
|
804
|
-
});
|
|
805
|
-
/**
|
|
806
|
-
* Sends this key using keycode only.
|
|
807
|
-
* @returns {Promise}
|
|
808
|
-
* @example
|
|
809
|
-
* keyboard.key.a.keycode();
|
|
810
|
-
*/
|
|
811
|
-
keyFns[keyName].keycode = wrapAsync(function () {
|
|
812
|
-
return inputKeycode(keycodes[keyName]);
|
|
813
|
-
});
|
|
814
|
-
});
|
|
815
|
-
return keyFns;
|
|
816
|
-
})(),
|
|
817
|
-
|
|
818
|
-
/**
|
|
819
|
-
* Shift subobject for shifted keys.
|
|
820
|
-
*/
|
|
821
|
-
shift: (() => {
|
|
822
|
-
const shiftFns = {};
|
|
823
|
-
Object.keys(keycodes).forEach(function (keyName) {
|
|
824
|
-
/**
|
|
825
|
-
* Sends this key with shift using inputText (if possible) or keycode fallback.
|
|
826
|
-
* @returns {Promise}
|
|
827
|
-
* @example
|
|
828
|
-
* keyboard.key.shift.a();
|
|
829
|
-
*/
|
|
830
|
-
shiftFns[keyName] = wrapAsync(function (options) {
|
|
831
|
-
options = options || {};
|
|
832
|
-
if (!options.forceKeycode && keyName.length === 1) {
|
|
833
|
-
return inputText(keyName.toUpperCase());
|
|
834
|
-
}
|
|
835
|
-
return inputKeycodeWrapped(keycodes.shiftLeft).then(function () {
|
|
836
|
-
return inputKeycodeLongPressWrapped(keycodes[keyName]);
|
|
837
|
-
});
|
|
838
|
-
});
|
|
839
|
-
|
|
840
|
-
/**
|
|
841
|
-
* Sends this key with shift using keycode only.
|
|
842
|
-
* @returns {Promise}
|
|
843
|
-
* @example
|
|
844
|
-
* keyboard.key.shift.a.keycode();
|
|
845
|
-
*/
|
|
846
|
-
shiftFns[keyName].keycode = wrapAsync(function () {
|
|
847
|
-
return inputKeycodeWrapped(keycodes.shiftLeft).then(function () {
|
|
848
|
-
return inputKeycodeLongPressWrapped(keycodes[keyName]);
|
|
849
|
-
});
|
|
850
|
-
});
|
|
851
|
-
});
|
|
852
|
-
return shiftFns;
|
|
853
|
-
})()
|
|
854
|
-
}
|
|
855
|
-
)
|
|
856
|
-
}
|
|
857
|
-
};
|
|
858
|
-
return remoteApi;
|
|
859
|
-
};
|