Skip to content

Commit

Permalink
Add option for power on/off keycode config
Browse files Browse the repository at this point in the history
  • Loading branch information
dwaan committed Aug 8, 2023
1 parent 4ebaf7d commit e4f8fc4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Check and run [test.js](./test.js) for complete examples
* *timeout* - Request timeout in milliseconds, default: 1000
* *playbackDelayOff* - Playback delay before it considered there's no media playing, default: 10000
* *retryPowerOn* - How many retry before power on request considered failure, default: 10
* *keycodePowerOn* - Keycode to power on device, default: 'KEYCODE_POWER'
* *keycodePowerOff* - Keycode to power on device, default: 'KEYCODE_POWER'

## Methods

Expand All @@ -40,6 +42,7 @@ Check if Android device have `tail` command.
Wrapper to run ADB command

**parameters**
Available parameters:

* **params**: string - the adb command, eg: `dumpsys`

Expand All @@ -54,6 +57,7 @@ Wrapper to run ADB command
Wrapper to run shell command

**parameters**
Available parameters:

* **params**: string - the shell command, eg: `dumpsys`
* *id*: string - process id
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class adb extends EventEmitter {
const _playbackDelayOff = config.playbackDelayOff || 10000;
const _retryPowerOn = config.retryPowerOn || 5;

// Keycode
const _keycodePowerOn = config.keycodePowerOn || 'KEYCODE_POWER';
const _keycodePowerOff = config.keycodePowerOff || 'KEYCODE_POWER';

// Device state
let _connected = this.DISCONNECTED;
let _currentAppID = false;
Expand Down Expand Up @@ -212,7 +216,7 @@ class adb extends EventEmitter {
do {
await this.sendKeycode(keycode || `KEYCODE_POWER`);
await _sleep(100);
await _state();
await _state(true);

_emitUpdate(`debugPower${isPowerOn ? `On` : `Off`}`, { awake: _isAwake }, retry);

Expand Down Expand Up @@ -242,15 +246,15 @@ class adb extends EventEmitter {
*/
this.powerOn = async keycode => {
if (_isAwake) return { result: true, message: `Device already awake` };
return await _power(keycode || `KEYCODE_POWER`);
return await _power(keycode || _keycodePowerOn || `KEYCODE_POWER`);
}
/**
* Turn off device
* @param {string} keycode - (optional) ADB keycode for power off/sleep
*/
this.powerOff = async keycode => {
if (!_isAwake) return { result: true, message: `Device already sleep` };
return await _power(keycode || `KEYCODE_POWER`, false);
return await _power(keycode || _keycodePowerOff || `KEYCODE_POWER`, false);
}
/**
* Get power information
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodejs-adb-wrapper",
"version": "1.3.4",
"version": "1.3.5",
"description": "Node.js module to connect to Android device using ADB",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit e4f8fc4

Please sign in to comment.