Is there a chance to send Android power button keycode with Keyboard library

Scenario:

  • Arduino Pro Micro (aka Leonardo) - it is capable to send HID events
  • I'm using Keyboard library (#include <Keyboard.h>)
  • Connected to Android phone via USB OTG cable.

Power button on some my spare phone (Samsung J7 2016) is completely broken, and I desperately want to install TWRP, custom ROM, etc. on it. For this, I need power button, because straight after flashing of TWRP while phone is in download mode (bootloader), I must not allow it to boot to system, instead of that I need to hold POWER + VOLUME UP + HOME, in order to boot into recovery. It comes in my mind that Arduino Pro Micro is capable to send keyboard events, and that I already done this before. But for god sake, I cannot figure out would it be possible to send Android phone power button value.

Example:

#include <Keyboard.h>
#define KEY_DELAY 50 //delay between keystrokes for slow computers

//a command line to execute
const char command [] = "echo hello hackers";

void setup() {
  Keyboard.begin();
  //I recommend that you leave a short delay before start while prototyping.
  //It will will give you some time to reprogram a board before it starts typing.
  delay(3000);
  Keyboard.press(KEY_ESC);
  delay(KEY_DELAY * 5);
  Keyboard.releaseAll();
}

void loop() {
}

Code above would hold ESCAPE button for 250ms, then would release it, then wouldn't do anything else.

Below is screenshot from Keyboard.h:

As from my understanding, #define KEY_**** on left should be "friendly name" for use in Arduino code, which is mapped to actual key hex value on right, e.g. 0x80. Correct?

I tried to find hex value for Android power button, found something here, but no matter if I add this to Keyboard.h as #define KEY_POWER 0x1A, then using KEY_POWER in Arduino code, or I'm using hex value directly in code as Keyboard.press(0x1A);, it is not working. Phone is powered off, I connect Arduino Pro Micro to phone via USB and nothing happens, not turning on.

Is it possible at all to send such a key event as POWER button is?

Thanks.

are you sure it's a keyboard button at all? might be connected to a circuit controlling power

No I am not :grin:, it could be.
Btw, I just saw that in my code I had #define KEY_DELAY 50, after that delay(KEY_DELAY * 5);, which would be equal to 250ms, which would be insufficient to power up Android device I think. No luck after I changed it to delay(KEY_DELAY * 60); or delay(KEY_DELAY * 200); as well.

I think most probably this isn't possible. It would make sense for me that Android cannot receive keyboard events through USB until it's powered off.

you can read this on why power buttons lost favours on USB keyboards

I'm not using android but found this

Thanks for links. First was very informative and I am glad I read it. Second one, well.... Android is based on Linux, true, but Android is also widely accepted by main stream folks so unfortunately most of articles over the internet about Android are copy/paste one from other and lot of useless text. I mean, could be useful for ordinary folks, who knows. Want to say that already know for these methods, and on top, this phone is old with removable battery, so I am able to turn it on (if turned off) by:
Put it on charger
Wait for percentage to show
Remove battery while phone is on charger
Return battery while phone is on charger
Phone will turn on

Intention was to put it in recovery from turned off state. For this, I need to hold POWER + VOLUME UP + HOME together for 5-6 seconds, but power button not exist. Yes, I can boot it to recovery when it is turned on by executing adb reboot recovery from PC, but thing is I need to boot to recovery straight after flashing TWRP from ODIN while phone is in Download mode, and adb reboot recovery is not working here.

Excuse to all for off topic, I know this is not Android forum, but on the other hand, strongly believe now that what I wanted to do with Arduino, isn't possible at all.

50% is not bad :wink:
Wish I could help more, but I’m an iOS/iPhone user

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.