Skip to content

Commit 06000af

Browse files
Add attach / detach commands (#534)
* Add attach / detach commands Signed-off-by: Yishai Yosifov <[email protected]> * Add "unknown" type to the device interface Signed-off-by: Yishai Yosifov <[email protected]> --------- Signed-off-by: Yishai Yosifov <[email protected]>
1 parent 008980d commit 06000af

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,22 @@ Waits until ADB can see the device. Note that you must know the serial in advanc
951951
- Returns: `Promise`
952952
- Resolves with: `id` (see callback)
953953

954+
#### device.attach():
955+
Reattaches ADB to the device's ADB USB interface. This re-enables communication between ADB and device, reversing `client.detach()`.
956+
957+
- **callback(err)** Optional. Use this or the returned `Promise`.
958+
- **err** `null` if the device device was reattached successfully, `Error` otherwise (can occur if the device is not detached).
959+
- Returns: `Promise`
960+
- Resolves with: `true`
961+
962+
#### device.detach():
963+
Detaches ADB's USB interface from ADB. This releases the device from ADB control, allowing other processes to use it.
964+
965+
- **callback(err)** Optional. Use this or the returned `Promise`.
966+
- **err** `null` if the device device was detached successfully, `Error` otherwise (can occur if the device is not attached).
967+
- Returns: `Promise`
968+
- Resolves with: `true`
969+
954970
### Sync
955971

956972
#### sync.end()

src/Device.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export default interface Device {
22
id: string;
3-
type: 'emulator' | 'device' | 'offline' | 'unauthorized';
3+
type: 'emulator' | 'device' | 'offline' | 'unauthorized' | 'unknown';
44
}

src/adb/DeviceClient.ts

+20
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import {
3636
WaitBootCompleteCommand,
3737
} from './command/host-transport';
3838
import {
39+
AttachCommand,
40+
DetachCommand,
3941
ForwardCommand,
4042
GetDevicePathCommand,
4143
GetSerialNoCommand,
@@ -603,4 +605,22 @@ export default class DeviceClient {
603605
public waitForDevice(): Bluebird<string> {
604606
return this.connection().then((conn) => new WaitForDeviceCommand(conn).execute(this.serial));
605607
}
608+
609+
/**
610+
* Reattaches ADB to the device's ADB USB interface. This re-enables communication between ADB and device, reversing `client.detach()`.
611+
*
612+
* @returns true
613+
*/
614+
public attach(): Bluebird<boolean> {
615+
return this.connection().then((conn) => new AttachCommand(conn).execute(this.serial));
616+
}
617+
618+
/**
619+
* Detaches ADB's USB interface from ADB. This releases the device from ADB control, allowing other processes to use it.
620+
*
621+
* @returns true
622+
*/
623+
public detach(): Bluebird<boolean> {
624+
return this.connection().then((conn) => new DetachCommand(conn).execute(this.serial));
625+
}
606626
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Command from '../../command';
2+
import Protocol from '../../protocol';
3+
import Bluebird from 'bluebird';
4+
5+
export default class AttachCommand extends Command<boolean> {
6+
execute(serial: string): Bluebird<boolean> {
7+
this._send(`host-serial:${serial}:attach`);
8+
return this.parser.readAscii(4).then((reply) => {
9+
switch (reply) {
10+
case Protocol.OKAY:
11+
return true;
12+
case Protocol.FAIL:
13+
return this.parser.readError();
14+
default:
15+
return this.parser.unexpected(reply, 'OKAY or FAIL');
16+
}
17+
});
18+
}
19+
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Command from '../../command';
2+
import Protocol from '../../protocol';
3+
import Bluebird from 'bluebird';
4+
5+
export default class DetachCommand extends Command<boolean> {
6+
execute(serial: string): Bluebird<boolean> {
7+
this._send(`host-serial:${serial}:detach`);
8+
return this.parser.readAscii(4).then((reply) => {
9+
switch (reply) {
10+
case Protocol.OKAY:
11+
return true;
12+
case Protocol.FAIL:
13+
return this.parser.readError();
14+
default:
15+
return this.parser.unexpected(reply, 'OKAY or FAIL');
16+
}
17+
});
18+
}
19+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { default as AttachCommand } from './attach';
2+
export { default as DetachCommand } from './detach';
13
export { default as ForwardCommand } from './forward';
24
export { default as GetDevicePathCommand } from './getdevicepath';
35
export { default as GetSerialNoCommand } from './getdevicepath';

0 commit comments

Comments
 (0)