Skip to content

Instantly share code, notes, and snippets.

@AnkitSuda
Created September 22, 2021 07:44
Show Gist options
  • Select an option

  • Save AnkitSuda/24939c3843330152b2aa8b24af1b2809 to your computer and use it in GitHub Desktop.

Select an option

Save AnkitSuda/24939c3843330152b2aa8b24af1b2809 to your computer and use it in GitHub Desktop.
Auto restart scrcpy on Android device reconnects
# to run commands as root
sudo whoami
EMPTY_ADB_OUTPUT="List of devices attached"
NO_PERMISSIONS="no permissions"
LAST_ADB_OUTPUT=""
while true
do
ADB_OUTPUT=$(adb devices)
ADB_OUTPUT="${ADB_OUTPUT%"${ADB_OUTPUT##*[![:space:]]}"}"
if [[ "${ADB_OUTPUT}" != "${LAST_ADB_OUTPUT}" ]]; then
echo "Changes detected"
if [[ "${ADB_OUTPUT}" != "${EMPTY_ADB_OUTPUT}" ]]; then
echo "Device connected"
if [[ "${ADB_OUTPUT}" != *"${NO_PERMISSIONS}"* ]]; then
scrcpy -w -S
fi
if [[ "${ADB_OUTPUT}" == *"${NO_PERMISSIONS}"* ]]; then
echo "No permissions restarting adb server"
sudo adb kill-server
sudo adb start-server
scrcpy -w -S
fi
fi
fi
LAST_ADB_OUTPUT="${ADB_OUTPUT}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment