Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script2 and adb #8

Closed
nicktones opened this issue Feb 15, 2018 · 7 comments
Closed

script2 and adb #8

nicktones opened this issue Feb 15, 2018 · 7 comments
Labels

Comments

@nicktones
Copy link

I'm trying to get script2 to work with a adb (android debugger) shell script I have to remotely start a Nvidia Shield. This script works fine when executed in the shell, however doesn't work when executing with script2. When checking the logs, everything looks correct, however the shield doesn't start. Note again that the script works when fired in the shell.

Any help would be gratefully received!

#!/bin/sh

adb kill-server 
adb start-server 
adb connect 192.168.1.21
sleep 5
adb shell "input keyevent KEYCODE_SLEEP"
sleep 3
adb kill-server
@pponce
Copy link
Owner

pponce commented Feb 15, 2018

Can you share the configuration settings?
Things to try:
1.) Insure that the full path of the script file is entered into the configuration settings.
2.) Insure the script is accessible by the homebridge user.

@nicktones
Copy link
Author

Absolutely. Here is my configuration...

{
        "accessory": "Script2",
        "name": "Shield",
        "on": "/var/homebridge/shield-wake.sh",
        "off": "/var/homebridge/shield-sleep.sh",
        "state": "/var/homebridge/shield-state.sh",
        "on_value" : "true"
}

Checking the log after flipping the switch shows that the script runs and it shows the exact same output as when running in the shell, however the Shield doesn't power off. I think it's an issue with the 'adb shell..." line of the script when running with Script2.

@pponce
Copy link
Owner

pponce commented Feb 15, 2018

Where is adb located?
Can you try changing the script to use
1.) #!/bin/bash instead of sh.
2.) Putting the full path to adb in your script?

I'm shooting in the dark a bit here. But i noticed in my scripts I
cd into the location and then execute the command. Not sure why I did this but perhaps i ran into the same issue.

@nicktones
Copy link
Author

nicktones commented Feb 16, 2018

Thanks for the point in the right direction. I ended up with this script, which works perfectly with Script2. This will be useful for anyone wanting to use adb and a script to remotely control an Android device.

#!/bin/bash
# Sleep Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 5
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_SLEEP"
/bin/sleep 5
/usr/bin/adb kill-server
EOF

This script opens a shell as the default pi user, and passes in the commands to it. Ensuring everything works as it should.

@nicktones
Copy link
Author

nicktones commented Feb 16, 2018

Ok, so here is the complete solution for anyone that wants to control their Nvidia Shield (or any Android device) through HomeBridge with Script2.

  • Install adb, then use config and scripts as below

HomeBridge Config Extract

{
        "accessory": "Script2",
        "name": "Shield",
        "on": "/var/homebridge/shield-wake.sh",
        "off": "/var/homebridge/shield-sleep.sh",
        "state": "/var/homebridge/shield-state.sh",
        "on_value" : "ON"
}

shield-wake.sh

#!/bin/bash
# Wake Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_WAKEUP"
/usr/bin/adb kill-server
EOF

shield-sheep.sh

#!/bin/bash
# Sleep Nvidia Shield Script v1.0

sudo -u pi bash << EOF
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
/usr/bin/adb -s 192.168.1.21:5555 shell "input keyevent KEYCODE_SLEEP"
/usr/bin/adb kill-server
EOF

shield-state.sh

#!/bin/bash
# State Nvidia Shield Script v1.0

sudo -u pi bash << EOF
{
/usr/bin/adb kill-server 
/usr/bin/adb start-server 
/usr/bin/adb connect 192.168.1.21
/bin/sleep 3
} 1>/dev/null 2>&1
if /usr/bin/adb shell dumpsys input_method | grep -q mInteractive=true
then
    echo "ON"
    
else
    echo "OFF"
fi
/usr/bin/adb kill-server
EOF

@pponce
Copy link
Owner

pponce commented Feb 16, 2018

Glad you figured it out and thanks for the write up. I'm sure it will help others.
Do you know what user you run homebridge as? Do you run it as user PI?
I my setup i run it as user "homebridge" and i gave user "homebridge" assess to my scripts.

@nicktones
Copy link
Author

No problem - hope it helps others.

Yeah, I have it setup the same way. Homebridge runs as the homebridge user. I gave it access to this script, and sudo without password permissions - however I think this one is due to the fact the adb launches its own shell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants