@cldmv/node-android-tv-remote 1.0.0 → 1.2.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/package.json +3 -2
- package/scripts/setup-device.js +41 -0
- package/src/lib/adb/setup.js +144 -0
- package/src/lib/android-tv-remote.js +200 -6
- package/test/connect-readall.js +33 -100
- package/test/interactive-remote.js +68 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cldmv/node-android-tv-remote",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A Node.js library for controlling Android TV devices via ADB keycodes and text input.",
|
|
5
5
|
"main": "src/lib/android-tv-remote.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest",
|
|
8
|
-
"postinstall": "node scripts/postinstall.js"
|
|
8
|
+
"postinstall": "node scripts/postinstall.js",
|
|
9
|
+
"setup-device": "node scripts/setup-device.js"
|
|
9
10
|
},
|
|
10
11
|
"keywords": [
|
|
11
12
|
"android",
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const readline = require("readline");
|
|
4
|
+
const AndroidTVSetup = require("../src/lib/adb/setup");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interactive CLI for setting up a new Android TV device.
|
|
8
|
+
* Prompts for IP and port, then runs setup.
|
|
9
|
+
*/
|
|
10
|
+
async function prompt(question, defaultValue) {
|
|
11
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
rl.question(`${question}${defaultValue ? ` [${defaultValue}]` : ""}: `, (answer) => {
|
|
14
|
+
rl.close();
|
|
15
|
+
resolve(answer || defaultValue);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
const ip = await prompt("Enter device IP address");
|
|
22
|
+
if (!ip) {
|
|
23
|
+
console.error("IP address is required.");
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
const port = parseInt(await prompt("Enter device port", "5555"), 10) || 5555;
|
|
27
|
+
const quiet = false;
|
|
28
|
+
const setup = new AndroidTVSetup({ ip, port, quiet });
|
|
29
|
+
try {
|
|
30
|
+
await setup.connect();
|
|
31
|
+
await setup.setSettings();
|
|
32
|
+
await setup.ensureAwake();
|
|
33
|
+
await setup.disconnect();
|
|
34
|
+
console.log("\nSetup complete!");
|
|
35
|
+
} catch (err) {
|
|
36
|
+
setup.errorWithTime("Error:", err.message || err);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
main();
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AndroidTVSetup class for configuring and managing Android TV devices via ADB.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* const setup = new AndroidTVSetup({ ip, port, quiet });
|
|
6
|
+
* await setup.connect();
|
|
7
|
+
* await setup.setSettings();
|
|
8
|
+
* await setup.ensureAwake();
|
|
9
|
+
* await setup.disconnect();
|
|
10
|
+
*/
|
|
11
|
+
const createRemote = require("../android-tv-remote");
|
|
12
|
+
|
|
13
|
+
class AndroidTVSetup {
|
|
14
|
+
/**
|
|
15
|
+
* @param {Object} options
|
|
16
|
+
* @param {string} options.ip - The IP address of the ADB device.
|
|
17
|
+
* @param {number} [options.port=5555] - The port number for the ADB connection.
|
|
18
|
+
* @param {boolean} [options.quiet=true] - Suppress log output except for results.
|
|
19
|
+
*/
|
|
20
|
+
constructor({ ip, port = 5555, quiet = true }) {
|
|
21
|
+
this.ip = ip;
|
|
22
|
+
this.port = port;
|
|
23
|
+
this.quiet = quiet;
|
|
24
|
+
this.remote = createRemote({ ip, port, quiet });
|
|
25
|
+
this.host = `${ip}:${port}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
logWithTime(...args) {
|
|
29
|
+
if (!this.quiet) {
|
|
30
|
+
const now = new Date().toISOString();
|
|
31
|
+
console.log(`[${now}]`, ...args);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
warnWithTime(...args) {
|
|
35
|
+
if (!this.quiet) {
|
|
36
|
+
const now = new Date().toISOString();
|
|
37
|
+
console.warn(`[${now}]`, ...args);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
errorWithTime(...args) {
|
|
41
|
+
const now = new Date().toISOString();
|
|
42
|
+
console.error(`[${now}]`, ...args);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async connect() {
|
|
46
|
+
await this.remote.connect();
|
|
47
|
+
this.logWithTime(`Connected to ${this.host}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async disconnect() {
|
|
51
|
+
try {
|
|
52
|
+
await this.remote.disconnect();
|
|
53
|
+
this.logWithTime("Disconnected cleanly");
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (err.message && err.message.includes("disconnected")) {
|
|
56
|
+
this.warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
57
|
+
} else {
|
|
58
|
+
this.errorWithTime("Error:", err.message || err);
|
|
59
|
+
if (err.message && err.message.includes("device unauthorized")) {
|
|
60
|
+
this.errorWithTime(
|
|
61
|
+
"Your device is unauthorized. Please check your TV and accept the authorization dialog to allow this computer to connect via ADB."
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Set all recommended settings for always-on display and max brightness.
|
|
71
|
+
*/
|
|
72
|
+
async setSettings() {
|
|
73
|
+
// Use the remote's handleSettings for all settings
|
|
74
|
+
await this.remote.handleSettings("set", this.quiet);
|
|
75
|
+
// svc power stayon true and brightness are not in handleSettings, so do them here
|
|
76
|
+
await this.remote.connect();
|
|
77
|
+
const cmds = ["settings put system screen_brightness_mode 0", "settings put system screen_brightness 255", "svc power stayon true"];
|
|
78
|
+
for (const cmd of cmds) {
|
|
79
|
+
(await this.remote.inputKeycode) ? Promise.resolve() : Promise.resolve(); // placeholder for future
|
|
80
|
+
await this.remotePressShell(cmd);
|
|
81
|
+
this.logWithTime(`Ran: ${cmd}`);
|
|
82
|
+
}
|
|
83
|
+
await this.remote.disconnect();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Helper to run a shell command using the remote's ADB client.
|
|
88
|
+
* @param {string} cmd
|
|
89
|
+
* @returns {Promise<void>}
|
|
90
|
+
*/
|
|
91
|
+
async remotePressShell(cmd) {
|
|
92
|
+
// Use the internal client for shell commands
|
|
93
|
+
if (this.remote && this.remote.inputKeycode) {
|
|
94
|
+
// fallback: use adbkit directly if needed
|
|
95
|
+
const adb = require("adbkit");
|
|
96
|
+
const client = adb.createClient();
|
|
97
|
+
await client.shell(this.host, cmd).then(adb.util.readAll);
|
|
98
|
+
} else if (this.remote && this.remote.client) {
|
|
99
|
+
await this.remote.client.shell(this.host, cmd).then(require("adbkit").util.readAll);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Ensures the device is awake and on the home screen.
|
|
105
|
+
*/
|
|
106
|
+
async ensureAwake() {
|
|
107
|
+
// Use the remote's ADB client for dumpsys power
|
|
108
|
+
const adb = require("adbkit");
|
|
109
|
+
const client = adb.createClient();
|
|
110
|
+
const output = await client
|
|
111
|
+
.shell(this.host, "dumpsys power")
|
|
112
|
+
.then(adb.util.readAll)
|
|
113
|
+
.then((b) => b.toString());
|
|
114
|
+
const parsed = this.parsePowerState(output);
|
|
115
|
+
this.logWithTime("\n--- Power State ---");
|
|
116
|
+
this.logWithTime(`mIsPowered: ${parsed.mIsPowered}`);
|
|
117
|
+
this.logWithTime(`mWakefulness: ${parsed.mWakefulness}`);
|
|
118
|
+
this.logWithTime(`mDisplayReady: ${parsed.mDisplayReady}`);
|
|
119
|
+
this.logWithTime("-------------------\n");
|
|
120
|
+
// Use remote.inputKeycode for keyevents
|
|
121
|
+
if (parsed.mIsPowered !== "true") await this.remote.inputKeycode(26);
|
|
122
|
+
if (parsed.mWakefulness !== "Awake") await this.remote.inputKeycode(224);
|
|
123
|
+
if (parsed.mDisplayReady !== "true") await this.remote.inputKeycode(26);
|
|
124
|
+
await this.remote.inputKeycode(3); // HOME
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Parses the output of 'dumpsys power' to extract power, wake, and display state.
|
|
129
|
+
* @param {string} output - The full output string from 'dumpsys power'.
|
|
130
|
+
* @returns {{ mIsPowered: string, mWakefulness: string, mDisplayReady: string }}
|
|
131
|
+
*/
|
|
132
|
+
parsePowerState(output) {
|
|
133
|
+
const mIsPoweredMatch = output.match(/^\s*mIsPowered=([a-zA-Z0-9]+)/m);
|
|
134
|
+
const mWakefulnessMatch = output.match(/^\s*mWakefulness=([a-zA-Z0-9]+)/m);
|
|
135
|
+
const mDisplayReadyMatch = output.match(/^\s*mDisplayReady=([a-zA-Z0-9]+)/m);
|
|
136
|
+
return {
|
|
137
|
+
mIsPowered: mIsPoweredMatch ? mIsPoweredMatch[1] : "unknown",
|
|
138
|
+
mWakefulness: mWakefulnessMatch ? mWakefulnessMatch[1] : "unknown",
|
|
139
|
+
mDisplayReady: mDisplayReadyMatch ? mDisplayReadyMatch[1] : "unknown"
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = AndroidTVSetup;
|
|
@@ -8,8 +8,12 @@
|
|
|
8
8
|
* @property {number} [port=5555] - The port for ADB connection.
|
|
9
9
|
* @property {string} [inputDevice="/dev/input/event0"] - The input device path.
|
|
10
10
|
* @property {boolean} [autoConnect=true] - Whether to auto-connect on command.
|
|
11
|
-
* @property {boolean} [autoDisconnect=
|
|
11
|
+
* @property {boolean} [autoDisconnect=false] - Whether to auto-disconnect after inactivity.
|
|
12
12
|
* @property {number} [disconnectTimeout=10] - Inactivity timeout in seconds before disconnecting.
|
|
13
|
+
* @property {boolean} [maintainConnection=true] - Whether to maintain the ADB connection with a heartbeat.
|
|
14
|
+
* @property {number} [heartbeatInterval=30000] - Heartbeat interval in ms (default 30s).
|
|
15
|
+
* @property {number} [connectionCheckInterval=30000] - Interval in ms for periodic connection checks (default 30s).
|
|
16
|
+
* @property {boolean} [quiet=true] - Suppress connection log output if true.
|
|
13
17
|
*/
|
|
14
18
|
/**
|
|
15
19
|
* @typedef {Object} Remote
|
|
@@ -17,6 +21,8 @@
|
|
|
17
21
|
* @property {function(function(Error=, any=)=): Promise|undefined} connect - Connect to the device. Supports both promise and callback styles.
|
|
18
22
|
* @property {function(function(Error=, any=)=): Promise|undefined} disconnect - Disconnect from the device. Supports both promise and callback styles.
|
|
19
23
|
* @property {function(number, function(Error=, any=)=): Promise|undefined} inputKeycode - Send a keycode to the device. Supports both promise and callback styles.
|
|
24
|
+
* @property {function(boolean=): Promise<"connected"|"disconnected"|"unknown">} getConnectionStatus - Returns the current connection status; optionally performs a live check.
|
|
25
|
+
* @property {boolean} isConnected - True if the module believes it is connected (internal state, not a live check).
|
|
20
26
|
* @property {Object} press - Remote control key functions for Android TV remotes.
|
|
21
27
|
* @property {Object} keyboard - Keyboard interface for all keys, with text and keycode fallback.
|
|
22
28
|
* @property {Object} keyboard.key - Contains all key functions.
|
|
@@ -75,15 +81,38 @@ function wrapAsync(fn) {
|
|
|
75
81
|
|
|
76
82
|
/**
|
|
77
83
|
* Handles disconnect and connection errors, outputs helpful messages, and exits the process.
|
|
84
|
+
* Also provides onboarding steps for common authentication and connection issues.
|
|
78
85
|
* @private
|
|
79
86
|
* @param {Error} err - The error object.
|
|
87
|
+
* @example
|
|
88
|
+
* try {
|
|
89
|
+
* // ...code that may throw
|
|
90
|
+
* } catch (err) {
|
|
91
|
+
* handleDisconnectError(err);
|
|
92
|
+
* }
|
|
80
93
|
*/
|
|
81
94
|
function handleDisconnectError(err) {
|
|
82
95
|
errorWithTime("Error:", err.message || err);
|
|
83
|
-
if (err.message && err.message.includes("device unauthorized")) {
|
|
96
|
+
if (err.message && (err.message.includes("device unauthorized") || err.message.includes("failed to authenticate"))) {
|
|
84
97
|
errorWithTime(
|
|
85
|
-
"Your device is unauthorized. Please check your TV and accept the authorization dialog to allow this system to connect via ADB."
|
|
98
|
+
"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."
|
|
86
99
|
);
|
|
100
|
+
errorWithTime("If you do not see a prompt, try disconnecting and reconnecting the device, or reboot your TV.");
|
|
101
|
+
errorWithTime("If the problem persists, remove the device from the list of authorized ADB devices in Developer Options and try again.");
|
|
102
|
+
errorWithTime(
|
|
103
|
+
"Tip: In Developer Options on your TV, try toggling 'ADB Debugging' off and then back on. This often resolves authentication issues."
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (err.message && (err.message.includes("actively refused") || err.message.includes("No connection could be made"))) {
|
|
107
|
+
errorWithTime("\nThe device refused the connection. To enable ADB, follow these steps on your Android TV or Fire TV:");
|
|
108
|
+
errorWithTime("1. Open Settings > Device Preferences > About (or My Fire TV > About)");
|
|
109
|
+
errorWithTime("2. Scroll to 'Build' and press OK 7 times to enable Developer Options");
|
|
110
|
+
errorWithTime("3. Go back to Settings > Device Preferences > Developer Options");
|
|
111
|
+
errorWithTime("4. Enable 'Developer Options' if needed, then enable 'ADB Debugging' and 'Apps from Unknown Sources'");
|
|
112
|
+
errorWithTime("5. Ensure your TV and computer are on the same network");
|
|
113
|
+
errorWithTime("6. On your computer, run: adb connect <device-ip>:5555");
|
|
114
|
+
errorWithTime("7. Accept the authorization prompt on your TV");
|
|
115
|
+
errorWithTime("\nIf you do not see 'Developer Options', repeat step 2 until it appears.");
|
|
87
116
|
}
|
|
88
117
|
process.exit(1);
|
|
89
118
|
}
|
|
@@ -108,12 +137,19 @@ const remoteKeys = require("../data/remote-keys.json");
|
|
|
108
137
|
* @function
|
|
109
138
|
* @public
|
|
110
139
|
* @param {RemoteConfig} config - Configuration for the remote.
|
|
140
|
+
* @param {boolean} [config.maintainConnection=true] - Whether to maintain the ADB connection with a heartbeat.
|
|
141
|
+
* @param {number} [config.heartbeatInterval=30000] - Heartbeat interval in ms (default 30s).
|
|
111
142
|
* @returns {Remote}
|
|
112
143
|
*/
|
|
113
144
|
module.exports = function (config) {
|
|
114
145
|
/**
|
|
115
146
|
* @type {RemoteConfig}
|
|
116
147
|
*/
|
|
148
|
+
/**
|
|
149
|
+
* Initializes and configures the Android TV Remote instance.
|
|
150
|
+
* Handles connection, persistent heartbeat, and auto-reconnect logic.
|
|
151
|
+
* @returns {Remote}
|
|
152
|
+
*/
|
|
117
153
|
config = config || {};
|
|
118
154
|
const ip = config.ip;
|
|
119
155
|
const port = config.port || 5555;
|
|
@@ -122,13 +158,19 @@ module.exports = function (config) {
|
|
|
122
158
|
const client = adb.createClient();
|
|
123
159
|
let connected = false;
|
|
124
160
|
const autoConnect = config.autoConnect !== false; // default true
|
|
125
|
-
const autoDisconnect = config.autoDisconnect
|
|
161
|
+
const autoDisconnect = config.autoDisconnect === true; // default false
|
|
126
162
|
const disconnectTimeout = typeof config.disconnectTimeout === "number" ? config.disconnectTimeout : 10;
|
|
163
|
+
const connectionCheckInterval = typeof config.connectionCheckInterval === "number" ? config.connectionCheckInterval : 30000;
|
|
164
|
+
let connectionCheckTimer = null;
|
|
127
165
|
let disconnectTimer = null;
|
|
128
166
|
const quiet = config.quiet !== false; // default true
|
|
167
|
+
const maintainConnection = config.maintainConnection !== false; // default true
|
|
168
|
+
const heartbeatInterval = typeof config.heartbeatInterval === "number" ? config.heartbeatInterval : 30000;
|
|
169
|
+
let heartbeatTimer = null;
|
|
129
170
|
|
|
130
171
|
/**
|
|
131
172
|
* Resets the disconnect timer if autoDisconnect is enabled.
|
|
173
|
+
* Disconnects after inactivity if enabled.
|
|
132
174
|
* @private
|
|
133
175
|
*/
|
|
134
176
|
function resetDisconnectTimer() {
|
|
@@ -139,8 +181,65 @@ module.exports = function (config) {
|
|
|
139
181
|
}, disconnectTimeout * 1000);
|
|
140
182
|
}
|
|
141
183
|
|
|
184
|
+
/**
|
|
185
|
+
* Starts the heartbeat timer to maintain the ADB connection.
|
|
186
|
+
* Periodically sends a shell command to keep the connection alive.
|
|
187
|
+
* Also starts the periodic connection check for auto-reconnect.
|
|
188
|
+
* @private
|
|
189
|
+
*/
|
|
190
|
+
function startHeartbeat() {
|
|
191
|
+
if (!maintainConnection) return;
|
|
192
|
+
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
|
193
|
+
heartbeatTimer = setInterval(() => {
|
|
194
|
+
if (!connected) return;
|
|
195
|
+
// Send a no-op shell command to keep the connection alive
|
|
196
|
+
client.shell(host, "echo heartbeat").catch(() => {});
|
|
197
|
+
}, heartbeatInterval);
|
|
198
|
+
startConnectionCheck();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Starts the periodic connection check. If disconnected, attempts to reconnect.
|
|
203
|
+
* Ensures persistent, self-healing ADB connection.
|
|
204
|
+
* @private
|
|
205
|
+
*/
|
|
206
|
+
function startConnectionCheck() {
|
|
207
|
+
if (connectionCheckTimer) clearInterval(connectionCheckTimer);
|
|
208
|
+
connectionCheckTimer = setInterval(async () => {
|
|
209
|
+
if (!connected) return;
|
|
210
|
+
try {
|
|
211
|
+
const devices = await client.listDevices();
|
|
212
|
+
const deviceId = host;
|
|
213
|
+
const found = devices.some((d) => d.id === deviceId);
|
|
214
|
+
if (!found) {
|
|
215
|
+
warnWithTime(`Device ${deviceId} not found in adb devices list. Attempting reconnect...`);
|
|
216
|
+
connected = false;
|
|
217
|
+
await connect();
|
|
218
|
+
}
|
|
219
|
+
} catch (err) {
|
|
220
|
+
warnWithTime("Error checking device connection:", err.message || err);
|
|
221
|
+
}
|
|
222
|
+
}, connectionCheckInterval);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Stops the heartbeat and connection check timers.
|
|
227
|
+
* @private
|
|
228
|
+
*/
|
|
229
|
+
function stopHeartbeat() {
|
|
230
|
+
if (heartbeatTimer) {
|
|
231
|
+
clearInterval(heartbeatTimer);
|
|
232
|
+
heartbeatTimer = null;
|
|
233
|
+
}
|
|
234
|
+
if (connectionCheckTimer) {
|
|
235
|
+
clearInterval(connectionCheckTimer);
|
|
236
|
+
connectionCheckTimer = null;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
142
240
|
/**
|
|
143
241
|
* Ensures connection if autoConnect is enabled.
|
|
242
|
+
* Used internally before sending commands.
|
|
144
243
|
* @internal
|
|
145
244
|
* @returns {Promise<void>}
|
|
146
245
|
*/
|
|
@@ -151,7 +250,8 @@ module.exports = function (config) {
|
|
|
151
250
|
}
|
|
152
251
|
|
|
153
252
|
/**
|
|
154
|
-
* Connects to the ADB device.
|
|
253
|
+
* Connects to the ADB device and starts heartbeat/connection check.
|
|
254
|
+
* Handles already-connected state gracefully.
|
|
155
255
|
* @internal
|
|
156
256
|
* @returns {Promise<void>}
|
|
157
257
|
*/
|
|
@@ -161,11 +261,13 @@ module.exports = function (config) {
|
|
|
161
261
|
.then(function () {
|
|
162
262
|
connected = true;
|
|
163
263
|
if (!quiet) logWithTime("Connected to " + host);
|
|
264
|
+
startHeartbeat();
|
|
164
265
|
})
|
|
165
266
|
.catch(function (err) {
|
|
166
267
|
if (err.message && err.message.includes("already connected")) {
|
|
167
268
|
if (!quiet) warnWithTime("Warning: Device already connected.");
|
|
168
269
|
connected = true;
|
|
270
|
+
startHeartbeat();
|
|
169
271
|
return;
|
|
170
272
|
}
|
|
171
273
|
handleDisconnectError(err);
|
|
@@ -174,7 +276,8 @@ module.exports = function (config) {
|
|
|
174
276
|
const connectWrapped = wrapAsync(connect);
|
|
175
277
|
|
|
176
278
|
/**
|
|
177
|
-
* Disconnects from the ADB device.
|
|
279
|
+
* Disconnects from the ADB device and stops heartbeat/connection check.
|
|
280
|
+
* Handles already-disconnected state gracefully.
|
|
178
281
|
* @internal
|
|
179
282
|
* @returns {Promise<void>}
|
|
180
283
|
*/
|
|
@@ -183,6 +286,7 @@ module.exports = function (config) {
|
|
|
183
286
|
clearTimeout(disconnectTimer);
|
|
184
287
|
disconnectTimer = null;
|
|
185
288
|
}
|
|
289
|
+
stopHeartbeat();
|
|
186
290
|
return client
|
|
187
291
|
.disconnect(ip, port)
|
|
188
292
|
.then(function () {
|
|
@@ -280,7 +384,61 @@ module.exports = function (config) {
|
|
|
280
384
|
});
|
|
281
385
|
};
|
|
282
386
|
|
|
387
|
+
/**
|
|
388
|
+
* Returns the current connection status as tracked by the module.
|
|
389
|
+
* Optionally performs a live check against adb devices if requested.
|
|
390
|
+
* @public
|
|
391
|
+
* @param {boolean} [liveCheck=false] - If true, checks adb devices for actual connection.
|
|
392
|
+
* @returns {Promise<"connected"|"disconnected"|"unknown">}
|
|
393
|
+
* @example
|
|
394
|
+
* await remote.getConnectionStatus();
|
|
395
|
+
* await remote.getConnectionStatus(true); // live check
|
|
396
|
+
*/
|
|
397
|
+
async function getConnectionStatus(liveCheck = false) {
|
|
398
|
+
if (!liveCheck) {
|
|
399
|
+
return connected ? "connected" : "disconnected";
|
|
400
|
+
}
|
|
401
|
+
try {
|
|
402
|
+
const devices = await client.listDevices();
|
|
403
|
+
const deviceId = host;
|
|
404
|
+
const found = devices.some((d) => d.id === deviceId);
|
|
405
|
+
return found ? "connected" : "disconnected";
|
|
406
|
+
} catch {
|
|
407
|
+
return "unknown";
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Returns true if the module believes it is connected to the device.
|
|
413
|
+
* This is a fast check of internal state, not a live ADB query.
|
|
414
|
+
* @public
|
|
415
|
+
* @returns {boolean}
|
|
416
|
+
* @example
|
|
417
|
+
* if (remote.isConnected) { ... }
|
|
418
|
+
*/
|
|
419
|
+
function isConnected() {
|
|
420
|
+
return !!connected;
|
|
421
|
+
}
|
|
422
|
+
|
|
283
423
|
return {
|
|
424
|
+
/**
|
|
425
|
+
* Returns the current connection status as tracked by the module, or does a live check if requested.
|
|
426
|
+
* @function
|
|
427
|
+
* @param {boolean} [liveCheck=false] - If true, checks adb devices for actual connection.
|
|
428
|
+
* @returns {Promise<"connected"|"disconnected"|"unknown">}
|
|
429
|
+
* @example
|
|
430
|
+
* await remote.getConnectionStatus();
|
|
431
|
+
* await remote.getConnectionStatus(true); // live check
|
|
432
|
+
*/
|
|
433
|
+
getConnectionStatus,
|
|
434
|
+
/**
|
|
435
|
+
* Returns true if the module believes it is connected to the device.
|
|
436
|
+
* @readonly
|
|
437
|
+
* @type {boolean}
|
|
438
|
+
* @example
|
|
439
|
+
* if (remote.isConnected) { ... }
|
|
440
|
+
*/
|
|
441
|
+
isConnected,
|
|
284
442
|
/**
|
|
285
443
|
* Get or set Android settings via ADB.
|
|
286
444
|
* @public
|
|
@@ -388,6 +546,19 @@ module.exports = function (config) {
|
|
|
388
546
|
if (key === "ok" || key === "select") {
|
|
389
547
|
return inputKeycode(keycodes.dpadCenter || keycodes.ok);
|
|
390
548
|
}
|
|
549
|
+
// Map up/down/left/right to dpad keycodes
|
|
550
|
+
if (key === "up") {
|
|
551
|
+
return inputKeycode(keycodes.dpadUp);
|
|
552
|
+
}
|
|
553
|
+
if (key === "down") {
|
|
554
|
+
return inputKeycode(keycodes.dpadDown);
|
|
555
|
+
}
|
|
556
|
+
if (key === "left") {
|
|
557
|
+
return inputKeycode(keycodes.dpadLeft);
|
|
558
|
+
}
|
|
559
|
+
if (key === "right") {
|
|
560
|
+
return inputKeycode(keycodes.dpadRight);
|
|
561
|
+
}
|
|
391
562
|
// Special handling for play/pause
|
|
392
563
|
if (key === "play") {
|
|
393
564
|
return inputKeycode(keycodes.mediaPlay || keycodes.playPause);
|
|
@@ -419,6 +590,18 @@ module.exports = function (config) {
|
|
|
419
590
|
if (key === "ok" || key === "select") {
|
|
420
591
|
return inputKeycodeLongPressWrapped(keycodes.dpadCenter || keycodes.ok);
|
|
421
592
|
}
|
|
593
|
+
if (key === "up") {
|
|
594
|
+
return inputKeycodeLongPressWrapped(keycodes.dpadUp);
|
|
595
|
+
}
|
|
596
|
+
if (key === "down") {
|
|
597
|
+
return inputKeycodeLongPressWrapped(keycodes.dpadDown);
|
|
598
|
+
}
|
|
599
|
+
if (key === "left") {
|
|
600
|
+
return inputKeycodeLongPressWrapped(keycodes.dpadLeft);
|
|
601
|
+
}
|
|
602
|
+
if (key === "right") {
|
|
603
|
+
return inputKeycodeLongPressWrapped(keycodes.dpadRight);
|
|
604
|
+
}
|
|
422
605
|
if (key === "play") {
|
|
423
606
|
return inputKeycodeLongPressWrapped(keycodes.mediaPlay || keycodes.playPause);
|
|
424
607
|
}
|
|
@@ -438,6 +621,17 @@ module.exports = function (config) {
|
|
|
438
621
|
return inputKeycodeLongPressWrapped(keycodes[key]);
|
|
439
622
|
});
|
|
440
623
|
});
|
|
624
|
+
// Robust aliasing: always add if dpad keys exist in keycodes
|
|
625
|
+
// if (typeof obj.dpadUp === "function" || keycodes.dpadUp) obj.up = obj.dpadUp;
|
|
626
|
+
// if (typeof obj.dpadDown === "function" || keycodes.dpadDown) obj.down = obj.dpadDown;
|
|
627
|
+
// if (typeof obj.dpadLeft === "function" || keycodes.dpadLeft) obj.left = obj.dpadLeft;
|
|
628
|
+
// if (typeof obj.dpadRight === "function" || keycodes.dpadRight) obj.right = obj.dpadRight;
|
|
629
|
+
// if (typeof obj.dpadCenter === "function" || keycodes.dpadCenter) obj.ok = obj.dpadCenter;
|
|
630
|
+
// if (typeof longObj.dpadUp === "function") longObj.up = longObj.dpadUp;
|
|
631
|
+
// if (typeof longObj.dpadDown === "function") longObj.down = longObj.dpadDown;
|
|
632
|
+
// if (typeof longObj.dpadLeft === "function") longObj.left = longObj.dpadLeft;
|
|
633
|
+
// if (typeof longObj.dpadRight === "function") longObj.right = longObj.dpadRight;
|
|
634
|
+
// if (typeof longObj.dpadCenter === "function") longObj.ok = longObj.dpadCenter;
|
|
441
635
|
obj.long = longObj;
|
|
442
636
|
return obj;
|
|
443
637
|
})(),
|
package/test/connect-readall.js
CHANGED
|
@@ -1,102 +1,35 @@
|
|
|
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
|
-
{ ns: "system", key: "screen_off_timeout", value: 2147483647 },
|
|
34
|
-
{ ns: "secure", key: "sleep_timeout", value: 0 },
|
|
35
|
-
{ ns: "global", key: "stay_on_while_plugged_in", value: 3 }
|
|
36
|
-
];
|
|
37
|
-
|
|
38
|
-
return client
|
|
39
|
-
.connect(ip, port)
|
|
40
|
-
.then(() => {
|
|
41
|
-
if (!quiet) logWithTime(`Connected to ${host}`);
|
|
42
|
-
let chain = Promise.resolve();
|
|
43
|
-
keys.forEach((item) => {
|
|
44
|
-
chain = chain.then(() => {
|
|
45
|
-
let cmd;
|
|
46
|
-
if (mode === "set") {
|
|
47
|
-
cmd = `settings put ${item.ns} ${item.key} ${item.value}`;
|
|
48
|
-
} else {
|
|
49
|
-
cmd = `settings get ${item.ns} ${item.key}`;
|
|
50
|
-
}
|
|
51
|
-
return client
|
|
52
|
-
.shell(host, cmd)
|
|
53
|
-
.then(adb.util.readAll)
|
|
54
|
-
.then((result) => {
|
|
55
|
-
if (!quiet) {
|
|
56
|
-
if (mode === "set") {
|
|
57
|
-
logWithTime(`Set ${item.ns} ${item.key} to ${item.value}`);
|
|
58
|
-
} else {
|
|
59
|
-
logWithTime(`${item.ns} ${item.key}: ${result.toString().trim()}`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
return chain;
|
|
66
|
-
})
|
|
67
|
-
.then(() => client.disconnect(ip, port))
|
|
68
|
-
.then(() => {
|
|
69
|
-
if (!quiet) logWithTime("Disconnected cleanly");
|
|
70
|
-
})
|
|
71
|
-
.catch((err) => {
|
|
72
|
-
if (err.message && err.message.includes("disconnected")) {
|
|
73
|
-
if (!quiet) warnWithTime("Warning: Device already disconnected before explicit disconnect call.");
|
|
74
|
-
} else {
|
|
75
|
-
errorWithTime("Error:", err.message || err);
|
|
76
|
-
if (err.message && err.message.includes("device unauthorized")) {
|
|
77
|
-
errorWithTime(
|
|
78
|
-
"Your device is unauthorized. Please check your TV and accept the authorization dialog to allow this computer to connect via ADB."
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
process.exit(1);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
1
|
+
// Test script to connect to an ADB device and read settings.
|
|
2
|
+
// Usage: node test/connect-readall.js <ip> [port] [get|set] [--quiet]
|
|
3
|
+
const AndroidTVSetup = require("../src/lib/adb/setup");
|
|
4
|
+
const readline = require("readline");
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const ip = process.argv[2];
|
|
8
|
+
const port = process.argv[3] ? parseInt(process.argv[3], 10) : 5555;
|
|
9
|
+
const mode = process.argv[4] === "set" ? "set" : "get";
|
|
10
|
+
const quiet = process.argv.includes("--quiet");
|
|
11
|
+
|
|
12
|
+
if (!ip) {
|
|
13
|
+
console.error("Usage: node test/connect-readall.js <ip> [port] [get|set] [--quiet]");
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!quiet) console.log(`Running in ${mode} mode on ${ip}:${port}`);
|
|
18
|
+
|
|
19
|
+
const setup = new AndroidTVSetup({ ip, port, quiet });
|
|
20
|
+
try {
|
|
21
|
+
await setup.connect();
|
|
22
|
+
if (mode === "set") {
|
|
23
|
+
await setup.setSettings();
|
|
24
|
+
}
|
|
25
|
+
await setup.ensureAwake();
|
|
26
|
+
await setup.disconnect();
|
|
27
|
+
} catch (err) {
|
|
28
|
+
setup.errorWithTime("Error:", err.message || err);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!quiet) console.log(`Ran in ${mode} mode on ${ip}:${port}`);
|
|
84
33
|
}
|
|
85
34
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const ip = process.argv[2];
|
|
89
|
-
const port = process.argv[3] ? parseInt(process.argv[3], 10) : 5555;
|
|
90
|
-
const mode = process.argv[4] === "set" ? "set" : "get";
|
|
91
|
-
const quiet = process.argv.includes("--quiet");
|
|
92
|
-
|
|
93
|
-
if (!ip) {
|
|
94
|
-
errorWithTime("Usage: node test/connect-readall.js <ip> [port] [get|set] [--quiet]");
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (!quiet) logWithTime(`Running in ${mode} mode on ${ip}:${port}`);
|
|
99
|
-
|
|
100
|
-
handleSettings(ip, port, mode, quiet);
|
|
101
|
-
|
|
102
|
-
if (!quiet) logWithTime(`Ran in ${mode} mode on ${ip}:${port}`);
|
|
35
|
+
main();
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Interactive test script for Android TV Remote
|
|
2
|
+
// Prompts for IP and port, connects, and sends button presses at intervals.
|
|
3
|
+
// Usage: node test/interactive-remote.js
|
|
4
|
+
|
|
5
|
+
const readline = require("readline");
|
|
6
|
+
const remote = require("../src/lib/android-tv-remote");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Prompts the user for input in the terminal.
|
|
10
|
+
* @param {string} query - The prompt message.
|
|
11
|
+
* @returns {Promise<string>} The user's input.
|
|
12
|
+
* @example
|
|
13
|
+
* const ip = await prompt("Enter IP: ");
|
|
14
|
+
*/
|
|
15
|
+
function prompt(query) {
|
|
16
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
17
|
+
return new Promise((resolve) =>
|
|
18
|
+
rl.question(query, (ans) => {
|
|
19
|
+
rl.close();
|
|
20
|
+
resolve(ans);
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Runs the interactive remote test using the main remote module.
|
|
27
|
+
* Prompts for IP/port, connects, and sends button presses.
|
|
28
|
+
* @returns {Promise<void>}
|
|
29
|
+
*/
|
|
30
|
+
async function main() {
|
|
31
|
+
const ip = (await prompt("Enter device IP: ")).trim();
|
|
32
|
+
let portInput = await prompt("Enter port [5555]: ");
|
|
33
|
+
const port = portInput.trim() ? parseInt(portInput.trim(), 10) : 5555;
|
|
34
|
+
if (!ip) {
|
|
35
|
+
console.error("IP address is required.");
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
await remote.connect({ ip, port });
|
|
40
|
+
console.log(`Connected to ${ip}:${port}`);
|
|
41
|
+
// List of buttons to press (as method names on remote.press)
|
|
42
|
+
const buttons = ["home", "up", "down", "left", "right", "ok", "back"];
|
|
43
|
+
for (let i = 0; i < buttons.length; i++) {
|
|
44
|
+
const btn = buttons[i];
|
|
45
|
+
if (remote.press && typeof remote.press[btn] === "function") {
|
|
46
|
+
console.log(`Pressing: ${btn}`);
|
|
47
|
+
await remote.press[btn]();
|
|
48
|
+
} else {
|
|
49
|
+
console.log(`Button '${btn}' not available.`);
|
|
50
|
+
}
|
|
51
|
+
if (btn === "home") {
|
|
52
|
+
await new Promise((res) => setTimeout(res, 750)); // 750ms after home
|
|
53
|
+
} else if (btn === "back") {
|
|
54
|
+
await new Promise((res) => setTimeout(res, 2500)); // 2.5s for back
|
|
55
|
+
} else {
|
|
56
|
+
await new Promise((res) => setTimeout(res, 500));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
await remote.disconnect();
|
|
60
|
+
console.log("Test complete. Disconnected.");
|
|
61
|
+
} catch (err) {
|
|
62
|
+
if (remote.errorWithTime) remote.errorWithTime("Error:", err.message || err);
|
|
63
|
+
else console.error("Error:", err.message || err);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
main();
|