DEV Community

Иван Плесских
Иван Плесских

Posted on

How to (almost) forget about wires in Android/Flutter/mobile-web development

Ok, this is pretty straightforward. If you a android developer (or flutter/react native developer, or web developer who wants to test your local code in real device browser), your phone is permanently connected to your working station.

If you don't like this and want to use some modern technologies like, you know, some wireless solutions, adb can connect your phone via wi-fi, and even forward port from your computer to phone's localhost.

But it's not really fun to type all this commands by hands every time when you start working, so there is simple bash script to switch connected Android phone to wi-fi adb mode and optionally forward port from computer to phone:

#!/bin/bash

if ! command -v adb &> /dev/null
then
    echo "adb is not globally installed, using android sdk version"
    #you can just set your path to android platform tools
    export PATH="$PATH:~/Android/Sdk/platform-tools"
fi

adb disconnect
adb usb
sleep 1

echo "Wait for USB device..."
adb wait-for-usb-device
adb devices

IP=$(adb shell ip route | cut -d' ' -f9)
echo "IP: $IP"

echo "Switch to tcpip"
adb tcpip 5555

echo "Connecting..."
while true; do
  res=$(adb connect $IP | cut -d' ' -f1)
  [[ "$res" == "connected" ]] && break
  sleep 1
done
echo "Connected!"

PORT=$1
if [ -n "$PORT" ]
then
  echo "Forward port $PORT"
  sleep 2
  adb -e reverse tcp:$PORT tcp:$PORT
fi
Enter fullscreen mode Exit fullscreen mode

There is gist, which you could just star or add to bookmarks:
https://gist.github.com/Amareis/a39e0fa39260a7d23abb7908fc295226

Note what you still need to connect phone over wire at first time, but then while it connected to same wi-fi as your computer, you can safely remove wire.

Script can use android platform-tools adb version (if there is no globally installed adb), wait for phone connect (if there is no any connected device) and, of course, forward your local port to phone.

Usage is really simple:

#simple turning connected phone to adb wi-fi mode
./connect.sh

#turning phone to adb wi-fi mode and then forward local port to it
./connect.sh 8080
Enter fullscreen mode Exit fullscreen mode

Tested on some ubuntu, and maybe it can works on mac or wsl, but not for sure. If you'll make it, you can propose changes to gist.

Care about your wires and use wi-fi!

Top comments (0)