OTG HID Events
Overview
- All OTG HID event module functions require hardware to use
- See OTG HID setup in Advanced — OTG HID Hardware: OTGHID Hardware
- The module object prefix is
otgEvent, e.g.otgEvent.click()
tip
- OTG HID is one click mode; accessibility, proxy mode, and root all support click and node fetch. If nodes are unavailable, use image/color matching
- For image capture permission, use
image.requestScreenCapturewith type=1 (permission-based capture) - OTG HID cannot use nodes; other features work the same with no special handling
init Initialize OTG Serial Port
- Initialize the OTG serial port
- Requires EC Android 11.40.0+
- @return
{null|string}null on success; otherwise an error message
// Test example for reference
var OTG_STEP_DELAY_MS = 2000;
/** Whether to run navigation tests: home / back / recents / systemKey */
var OTG_TEST_RUN_NAV_KEYS = false;
/** Touch test coordinates (adjust for target resolution) */
var OTG_TEST_X = 200;
var OTG_TEST_Y = 200;
var OTG_TEST_X2 = 400;
var OTG_TEST_Y2 = 400;
function _ok(name, err) {
if (err == null || err === "" || err === undefined) {
logd("[OTG-TEST] OK " + name);
return true;
}
loge("[OTG-TEST] FAIL " + name + " -> " + err);
return false;
}
function runOtgEventTests() {
logd("[OTG-TEST] ========== start ==========");
_ok("init", otgEvent.init());
sleep(OTG_STEP_DELAY_MS);
_ok("connectFirst", otgEvent.connectFirst());
sleep(OTG_STEP_DELAY_MS);
logd("[OTG-TEST] isConnected = " + otgEvent.isConnected());
// If unavailable, sleep 3s and retry
logd("mac "+otgEvent.getMacAddress())
_ok("setTimeouts(800,1500,2500)", otgEvent.setTimeouts(2000, 2000, 3000));
sleep(OTG_STEP_DELAY_MS);
_ok("clickPoint", otgEvent.clickPoint(200, 300));
sleep(OTG_STEP_DELAY_MS);
_ok("doubleClickPoint", otgEvent.doubleClickPoint(400, 500));
sleep(OTG_STEP_DELAY_MS);
_ok("press(600ms)", otgEvent.press(600, 700, 5000));
sleep(OTG_STEP_DELAY_MS);
_ok("swipe", otgEvent.swipe(OTG_TEST_X, OTG_TEST_Y, OTG_TEST_X2, OTG_TEST_Y2, 400));
sleep(OTG_STEP_DELAY_MS);
_ok("touchDown", otgEvent.touchDown(OTG_TEST_X, OTG_TEST_Y));
sleep(OTG_STEP_DELAY_MS);
_ok("touchMove(pressed=true)", otgEvent.touchMove(OTG_TEST_X + 30, OTG_TEST_Y + 30));
sleep(OTG_STEP_DELAY_MS);
_ok("touchMove(pressed=false)", otgEvent.touchMove(OTG_TEST_X + 60, OTG_TEST_Y + 60));
sleep(OTG_STEP_DELAY_MS);
_ok("touchUp", otgEvent.touchUp(OTG_TEST_X + 60, OTG_TEST_Y + 60));
sleep(OTG_STEP_DELAY_MS);
if (OTG_TEST_RUN_NAV_KEYS) {
_ok("systemKey(home)", otgEvent.systemKey("home"));
sleep(OTG_STEP_DELAY_MS);
_ok("systemKey(back)", otgEvent.systemKey("back"));
sleep(OTG_STEP_DELAY_MS);
_ok("systemKey(recents)", otgEvent.systemKey("recents"));
sleep(OTG_STEP_DELAY_MS);
_ok("recents()", otgEvent.recents());
sleep(OTG_STEP_DELAY_MS);
_ok("back()", otgEvent.back());
sleep(OTG_STEP_DELAY_MS);
_ok("home()", otgEvent.home());
sleep(OTG_STEP_DELAY_MS);
} else {
logd("[OTG-TEST] skip systemKey / recents / back / home (set OTG_TEST_RUN_NAV_KEYS = true to run)");
}
_ok("keyPressChar a", otgEvent.keyPressChar("", "a"));
sleep(OTG_STEP_DELAY_MS);
_ok("keyPress 97 (a)", otgEvent.keyPress("", 97));
sleep(OTG_STEP_DELAY_MS);
var seq = [
{"action": 0, "x": OTG_TEST_X, "y": OTG_TEST_Y, "pointer": 1, "delay": 40},
{"action": 2, "x": OTG_TEST_X + 50, "y": OTG_TEST_Y + 50, "pointer": 1, "delay": 40},
{"action": 2, "x": OTG_TEST_X + 100, "y": OTG_TEST_Y + 100, "pointer": 1, "delay": 40},
{"action": 1, "x": OTG_TEST_X + 100, "y": OTG_TEST_Y + 100, "pointer": 1, "delay": 40}
];
_ok("multiTouch", otgEvent.multiTouch(seq, 5000));
sleep(OTG_STEP_DELAY_MS);
_ok("close", otgEvent.close());
sleep(OTG_STEP_DELAY_MS);
logd("[OTG-TEST] ========== end ==========");
}
runOtgEventTests();