Skip to content

Instantly share code, notes, and snippets.

@aMir733
Created July 12, 2022 14:38
Show Gist options
  • Save aMir733/85b85a9379f0255952d08c598f76c5f0 to your computer and use it in GitHub Desktop.
Save aMir733/85b85a9379f0255952d08c598f76c5f0 to your computer and use it in GitHub Desktop.
Enable wireless debugging and connect using adb.
#!/usr/bin/env bash
# Enable wireless debugging and connect using adb.
command -v adb su >/dev/null || exit 1
# Variables
_SUDO="su -c"
_ADB="adb"
# Functions
adb_wifi_enabled () { $_SUDO "settings put global adb_wifi_enabled $1" ;}
get_latest_port () { $_SUDO logcat -d --format=raw | grep 'adb wifi started on port' | tail -n 1 | cut -d' ' -f6 ; }
# Restart adb_wifi and get the port from logcat
OLD_ADB_PORT="$(get_latest_port)"
adb_wifi_enabled 0 ; adb_wifi_enabled 1
i=0 ; while true ; do
ADB_PORT="$(get_latest_port)"
[[ "${ADB_PORT}" != "${OLD_ADB_PORT}" ]] && [[ "${#ADB_PORT}" > 0 ]] && break
((i++))
[[ $i == 10 ]] && exit 1
sleep 2
done
# Connect to adb
ADB_IP="127.0.0.1:${ADB_PORT}"
$_ADB connect "$ADB_IP" &>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment