Skip to content

Instantly share code, notes, and snippets.

@kikermo
Last active November 17, 2023 19:27
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 kikermo/c3f30105c1a8d2ebbbbfd05e456cc4d7 to your computer and use it in GitHub Desktop.
Save kikermo/c3f30105c1a8d2ebbbbfd05e456cc4d7 to your computer and use it in GitHub Desktop.
Simple script that list all Android device connected to a Raspberry Pi via USB and connects your local machine to them over tcp/ip
#!/bin/bash
server="raspberrypi"
devicePort=5555
# Function to display the menu
display_menu() {
clear
echo "===== Connect to ADB device ====="
for ((i=0; i<${#options[@]}; i++)); do
echo "$((i+1)). ${options[$i]}"
done
echo "0. Exit"
}
# Function to handle user input
handle_input() {
read -p "Enter your choice: " choice
if [[ $choice -ge 0 && $choice -le ${#options[@]} ]]; then
if [ $choice -eq 0 ]; then
echo "Exiting..."
exit 0
else
selection=${options[$((choice-1))]}
trimmedSelection=$(echo $selection | awk '{print $1}')
echo "Connecting to: $trimmedSelection"
connect_to_device "$trimmedSelection"
exit 0
fi
else
echo "Invalid choice. Please try again."
read -p "Press Enter to continue..."
fi
}
# Function to connect to the server
connect_to_device() {
deviceId=$1
echo "Obtaining ip Address..."
deviceIp=$(ssh $server adb -s $deviceId shell ifconfig | grep -v "127.0.0.1" | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | awk -F':' '{print $2}')
ssh $server adb -s $deviceId tcpip $devicePort
adb connect ${deviceIp}:$devicePort
}
# Get the list of devices
options=($(ssh $server adb devices -l | tail -n +2 | awk '{print $1}' ))
# Main loop
while true; do
display_menu
handle_input
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment