Skip to content

Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.

fix: better support for multi displays devices

Gil Megidish8a80f9199c8a
diff --git a/src/android.ts b/src/android.ts
index a8874bf..ed8e1bc 100644
--- a/src/android.ts
+++ b/src/android.ts
@@ -66,12 +66,6 @@ export class AndroidRobot implements Robot {
});
}
- public getFirstDisplayId(): string | null {
- const output = this.adb("shell", "dumpsys", "SurfaceFlinger", "--display-id").toString();
- const match = output.match(/Display (\d+) \(/);
- return match ? match[1] : null;
- }
-
public getSystemFeatures(): string[] {
return this.adb("shell", "pm", "list", "features")
.toString()
@@ -206,16 +200,48 @@ export class AndroidRobot implements Robot {
this.adb("shell", "input", "swipe", `${x0}`, `${y0}`, `${x1}`, `${y1}`, "1000");
}
- public async getScreenshot(): Promise<Buffer> {
- const displayId = this.getFirstDisplayId();
+ private getDisplayCount(): number {
+ return this.adb("shell", "dumpsys", "SurfaceFlinger", "--display-id")
+ .toString()
+ .split("\n")
+ .filter(s => s.startsWith("Display "))
+ .length;
+ }
- if (displayId !== null) {
- // always good to provide displayId. required for multi-display devices such as fold
- return this.adb("exec-out", "screencap", "-p", "-d", displayId);
- } else {
- // backward compatibility for android 10 and below
+ private getFirstDisplayId(): string | null {
+ const displays = this.adb("shell", "cmd", "display", "get-displays")
+ .toString()
+ .split("\n")
+ .filter(s => s.startsWith("Display id "))
+ // filter for state ON even though get-displays only returns turned on displays
+ .filter(s => s.indexOf(", state ON,") >= 0)
+ // another paranoia check
+ .filter(s => s.indexOf(", uniqueId ") >= 0);
+
+ if (displays.length > 0) {
+ const m = displays[0].match(/uniqueId \"([^\"]+)\"/);
+ if (m !== null) {
+ const displayId = m[1];
+ if (displayId.indexOf("local:") === 0) {
+ return displayId.split(":")[1];
+ }
+
+ return displayId;
+ }
+ }
+
+ return null;
+ }
+
+ public async getScreenshot(): Promise<Buffer> {
+ if (this.getDisplayCount() <= 1) {
+ // backward compatibility for android 10 and below, and for single display devices
return this.adb("exec-out", "screencap", "-p");
}
+
+ // find the first display that is turned on, and capture that one
+ const displayId = this.getFirstDisplayId();
+ return this.adb("exec-out", "screencap", "-p", "-d", `${displayId}`);
}
private collectElements(node: UiAutomatorXmlNode): ScreenElement[] {