Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Handle duplicate ADB connections in Android development script

Daniel638b948b6a88
diff --git a/scripts/android-dev-run.mjs b/scripts/android-dev-run.mjs
index a7bbaf00fe..cd0f13fe45 100644
--- a/scripts/android-dev-run.mjs
+++ b/scripts/android-dev-run.mjs
@@ -8,6 +8,7 @@ import {copyFileSync, existsSync, readdirSync, readFileSync, statSync, unlinkSyn
import os from "node:os";
import path from "node:path";
import {fileURLToPath} from "node:url";
+import {selectDuplicateDevice} from "./android-device.mjs";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const appDir = path.join(repoRoot, "app");
@@ -18,7 +19,7 @@ function usage() {
console.log("Options:");
console.log(" --flavor=<flavor> Flavor: cn / googleplay / huawei / official (default: official)");
console.log(" --android-dir=<path> Path to the siyuan-android project (default: next to the siyuan project)");
- console.log(" --device=<serial> Specify an adb device (required when multiple devices are connected)");
+ console.log(" --device=<serial> Specify an adb connection (required for different or unidentified devices)");
console.log(" --skip-ui Skip pnpm build:mobile and use the existing stage/build/mobile output");
console.log(" --skip-kernel Skip the gomobile build and use the existing kernel/kernel.aar");
console.log(" --skip-gradle Skip the Gradle build and reinstall the most recently built APK");
@@ -182,7 +183,12 @@ function deviceSerial(adb) {
if (devices.length === 0) {
fail("No connected devices detected. To connect using wireless debugging, enable Wireless debugging on the device, run adb pair <ip>:<port> <pairing-code>, and then run adb connect <ip>:<port>");
}
- fail(`Multiple devices detected: ${devices.join(", ")}. Specify one using --device=<serial>`);
+ const selected = selectDuplicateDevice(adb, devices);
+ if (selected) {
+ console.log(`Multiple connections to the same device detected; using ${selected}`);
+ return selected;
+ }
+ fail(`Multiple different or unidentified devices detected: ${devices.join(", ")}. Specify one using --device=<serial>`);
}
const sdkDir = findSdkDir();
diff --git a/scripts/android-device.mjs b/scripts/android-device.mjs
new file mode 100644
index 0000000000..317111d18e
--- /dev/null
+++ b/scripts/android-device.mjs
@@ -0,0 +1,23 @@
+import {spawnSync} from "node:child_process";
+
+export function selectDuplicateDevice(adb, devices, execute = spawnSync) {
+ let identity = null;
+ for (const serial of devices) {
+ // 连接名称可能随无线服务重新发布而变化,使用手机报告的序列号核对身份。
+ const result = execute(adb, ["-s", serial, "shell", "getprop", "ro.serialno"], {
+ encoding: "utf8",
+ timeout: 3000,
+ windowsHide: true,
+ });
+ const value = result.stdout?.trim();
+ // 无法确认身份时保留手动选择,避免把未知连接合并到另一台手机。
+ if (result.error || result.status !== 0 || !value || /^(unknown|null|0+)$/i.test(value)) {
+ return null;
+ }
+ if (identity !== null && identity !== value) {
+ return null;
+ }
+ identity = value;
+ }
+ return devices[0] || null;
+}
diff --git a/scripts/android-device.test.mjs b/scripts/android-device.test.mjs
new file mode 100644
index 0000000000..68ad120b2d
--- /dev/null
+++ b/scripts/android-device.test.mjs
@@ -0,0 +1,44 @@
+import assert from "node:assert/strict";
+import {test} from "node:test";
+import {selectDuplicateDevice} from "./android-device.mjs";
+
+const devices = ["adb-phone (2)._adb-tls-connect._tcp", "adb-phone._adb-tls-connect._tcp"];
+
+test("duplicate wireless connections use the physical serial without splitting names", () => {
+ const calls = [];
+ const selected = selectDuplicateDevice("adb", devices, (command, args, options) => {
+ calls.push(args);
+ assert.equal(command, "adb");
+ assert.equal(options.timeout, 3000);
+ return {status: 0, stdout: "physical-serial\r\n"};
+ });
+ assert.equal(selected, devices[0]);
+ assert.deepEqual(calls, devices.map((serial) => ["-s", serial, "shell", "getprop", "ro.serialno"]));
+});
+
+test("similar connection names do not merge different phones", () => {
+ assert.equal(selectDuplicateDevice("adb", devices, (command, args) => ({
+ status: 0,
+ stdout: args[1] === devices[0] ? "phone-a" : "phone-b",
+ })), null);
+});
+
+test("USB and wireless connections to one phone can be merged", () => {
+ assert.equal(selectDuplicateDevice("adb", ["physical-serial", devices[0]], () => ({
+ status: 0, stdout: "physical-serial",
+ })), "physical-serial");
+});
+
+test("unidentified or unresponsive connections require explicit selection", () => {
+ for (const result of [
+ {status: 0, stdout: ""},
+ {status: 0, stdout: "unknown"},
+ {status: 0, stdout: "00000000"},
+ {status: 1, stdout: ""},
+ {status: null, error: new Error("timeout")},
+ ]) {
+ let calls = 0;
+ assert.equal(selectDuplicateDevice("adb", devices, () => ++calls === 1
+ ? {status: 0, stdout: "phone-a"} : result), null);
+ }
+});