Skip to content

Instantly share code, notes, and snippets.

@bonnebulle
Last active December 26, 2022 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonnebulle/ae2472ad850b68fe9e174795fb7b2cbb to your computer and use it in GitHub Desktop.
Save bonnebulle/ae2472ad850b68fe9e174795fb7b2cbb to your computer and use it in GitHub Desktop.
SCRCPY Wifi / TCP-IP automation script (find IP)
# ADB SCRCPY via wifi
# https://github.com/Genymobile/scrcpy
#### DEPENDENCIE (notifications)
#### https://github.com/vlevit/notify-send.sh
export SCRCPY_CMD="scrcpy --always-on-top --power-off-on-close --shortcut-mod=lctrl --stay-awake --turn-screen-off"
export ANDROID_IDD="<YOUR ANDROID ID HERE>" # LIKE h73p2zed > $adb devices (when android is USB connected)
notify-send.sh "SCRCPY_WIFI" "\-\- Start : $ANDROID_IDD" --icon="scrcpy" -t 8000
export wlan_adb_ntwrk="wlan0"; # wlan1 will be use if you Tethering wifi (bridge/hotspot)
#########################
######################### if wlan_adb_ntwrk is incorrect (wlan 0 / 1)
######################### ... will try other one !
#########################
switch_wlan_num () {
echo
echo "--- try to fix/switch Wlan 0 / 1"
if [[ -z "$ANDROID_IP" ]] || $(adb -e shell "ip addr show wlan1" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1) | grep "does not exist"; then
echo "-- CANT FIND IP --1 -- check variable 'wlan_adb_ntwrk'"
notify-send.sh "ERROR WIFI : NO IP for $wlan_adb_ntwrk" "\-\- Try To Fix" --icon="scrcpy" -t 5000
if [ $wlan_adb_ntwrk == "wlan0" ]; then
echo "--- try WLAN 1"
if $(adb -e shell "ip addr show wlan0" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1) | grep "does not exist" ; then
exit
else
echo && echo "=== fixed ( wlan 0 -> 1 )"; notify-send.sh "fixed ( wlan O -> 1 )" "\-\- You can change \$wlan_adb_ntwrk" --icon="scrcpy" -t 5000
export ANDROID_IP=$(adb -e shell "ip addr show wlan1" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1)
fi
else
echo "--- try WLAN 0"
if $(adb shell "ip addr show wlan0" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1) | grep "does not exist" ; then
echo "exxit"
exit
else
echo && echo "=== fixed ( wlan 1 -> 0 )"; notify-send.sh "fixed ( wlan 1 -> 0 )" "\-\- You can change \$wlan_adb_ntwrk" --icon="scrcpy" -t 5000
export ANDROID_IP=$(adb shell "ip addr show wlan0" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1)
fi
fi
echo
else
notify-send.sh "! ANDROID IP" "\-\- ( $ANDROID_IP ) - GO" --icon="scrcpy" -t 5000
fi
}
#########################
#########################
#########################
#########################
if adb devices | grep $ANDROID_IDD ;
then
echo "OK ANDROID CONNECTED"
notify-send.sh "SCRCPY" "\-\- Connected" --icon="scrcpy" -t 4000
echo
elif adb devices | grep "5555" ;
then
echo "but 5555 previous connexion"
export ANDROID_IP=$(adb shell "ip addr show $wlan_adb_ntwrk" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1)
if [ -z "$ANDROID_IP" ]; then
echo "NO IP"
notify-send.sh "NO IP" "\-\- TRY RECONNECT USB" --icon="scrcpy" -t 9000
# sleep 10
adb disconnect $ANDROID_IDD
adb connect $ANDROID_IP
elif adb connect $ANDROID_IP | grep "offline"; then
echo "-- $ANDROID_IP ---- LOOKS OFFLINE"
adb disconnect $ANDROID_IDD
adb connect $ANDROID_IP
fi
else
echo "-- FIRST -- Connect the Android Phone via USB !!!"
notify-send.sh "SCRCPY ERROR" "\-\- PLEASE CONNECT WITH USB FIRST" --icon="scrcpy" -t 8000
echo
echo
sleep 10
if adb devices | grep $ANDROID_IDD ;
then
notify-send.sh "SCRCPY THANKS" "\-\- USB Connected" --icon="scrcpy" -t 4000
else
notify-send.sh "SCRCPY EXIT :(" "\-\- Not USB/WIFI Connected" --icon="scrcpy" -t 4000
exit ###### EXIT
fi
fi
####
if adb devices | grep "5555";
then
if adb devices | grep "offline"; then
echo "offline"
adb disconnect
else
echo "-- DEVICE TCPI_IP EXISTS -- 0"
adb devices
export ANDROID_IP=$(adb -e shell "ip addr show $wlan_adb_ntwrk" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1)
echo "IP ADRESS -- 0 == " $ANDROID_IP
# switch_wlan_num
sleep 1
adb disconnect $ANDROID_IDD
echo && echo " . . . "
adb connect $ANDROID_IP
sleep 1
$SCRCPY_CMD -s $ANDROID_IP &
disown && notify-send.sh "OK ! Run away !" "\-\- Now you can disconnect" --icon="scrcpy" -t 5000 && echo && echo && echo && echo "BYE" && echo "-- ALL DONE"
exit
###### EXIT
fi
else
echo "-- NO 5555 DEVICE"
notify-send.sh "NO DEVICE TCP_CONNECTED" "\-\- Connecting..." --icon="scrcpy" -t 5000
echo "(continue)" #### CONTINUE
fi
####
echo "-- RESET"
notify-send.sh "RESET !" "\-\- Diconnect + kill" --icon="scrcpy" -t 3000
adb kill-server
adb disconnect
sleep 2
echo
echo "-- USB"
notify-send.sh "USB" "\-\- Connected ( GO )" --icon="scrcpy" -t 3000
adb usb
sleep 3
adb devices
sleep 1
####
echo "-- ANDROID LAN IP"
export ANDROID_IP=$(adb shell "ip addr show $wlan_adb_ntwrk" | grep -o -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | head -1)
echo "IP ADRESS == 2--" $ANDROID_IP
echo "-- is ok WAN ?"
switch_wlan_num
####
echo
echo "-- TCP IP"
adb tcpip 5555
sleep 1
echo $ANDROID_IP
adb connect $ANDROID_IP
notify-send.sh "LETS_GOOOO !!!" "\-\- ( IP ok )" --icon="scrcpy" -t 2000
$SCRCPY_CMD -s $ANDROID_IP &
disown && notify-send.sh "OK ! Run away !" "\-\- Now you can disconnect" --icon="scrcpy" -t 5000 && echo && echo && echo && echo "BYE" && echo "-- ALL DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment