Skip to content

Commit 9af5b35

Browse files
committed
Add attach / detach commands (DeviceFarmer#534) from YishaiYosifov
1 parent 334298c commit 9af5b35

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/adb/DeviceClient.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import ProcStat from './proc/stat.js';
1414
import { HostTransportCommand } from './command/host/index.js';
1515
import * as hostCmd from './command/host-transport/index.js';
1616
import {
17+
AttachCommand,
18+
DetachCommand,
1719
ForwardCommand,
1820
GetDevicePathCommand,
1921
GetSerialNoCommand,
@@ -1046,6 +1048,26 @@ export default class DeviceClient {
10461048
return new WaitForDeviceCommand(conn).execute(this.serial);
10471049
}
10481050

1051+
/**
1052+
* Reattaches ADB to the device's ADB USB interface. This re-enables communication between ADB and device, reversing `client.detach()`.
1053+
*
1054+
* @returns true
1055+
*/
1056+
public async attach(): Promise<boolean> {
1057+
const conn = await this.connection();
1058+
return new AttachCommand(conn).execute(this.serial);
1059+
}
1060+
1061+
/**
1062+
* Detaches ADB's USB interface from ADB. This releases the device from ADB control, allowing other processes to use it.
1063+
*
1064+
* @returns true
1065+
*/
1066+
public async detach(): Promise<boolean> {
1067+
const conn = await this.connection();
1068+
return new DetachCommand(conn).execute(this.serial);
1069+
}
1070+
10491071
/**
10501072
* prepare a Scrcpy server
10511073
* this server must be started with the start() method
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Command from '../../command.js';
2+
3+
export default class AttachCommand extends Command<boolean> {
4+
async execute(serial: string): Promise<boolean> {
5+
await this._send(`host-serial:${serial}:attach`);
6+
await this.readOKAY();
7+
return true;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Command from '../../command.js';
2+
3+
export default class DetachCommand extends Command<boolean> {
4+
async execute(serial: string): Promise<boolean> {
5+
await this._send(`host-serial:${serial}:detach`);
6+
await this.readOKAY();
7+
return true;
8+
}
9+
}

src/adb/command/host-serial/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { default as AttachCommand } from './attach.js';
2+
export { default as DetachCommand } from './detach.js';
13
export { default as ForwardCommand } from './forward.js';
24
export { default as KillForwardCommand } from './killForward.js';
35
export { default as GetDevicePathCommand } from './getdevicepath.js';

src/models/Device.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import DeviceClient from "../adb/DeviceClient.js";
22

3-
const deviceTypes = ['emulator', 'device', 'offline', 'unauthorized', 'recovery'] as const;
3+
const deviceTypes = ['emulator', 'device', 'offline', 'unauthorized', 'recovery', 'unknown'] as const;
44
/**
55
* adb device starts
66
*/

0 commit comments

Comments
 (0)