How can I connect to Android with ADB over TCP?

2018-12-31 00:43发布

I am attempting to debug an application on a Motorola Droid, but I am having some difficulty connecting to the device via USB. My development server is a Windows 7 64-bit VM running in Hyper-V, and so I cannot connect directly via USB in the guest or from the host.

I installed a couple of different USB-over-TCP solutions, but the connection appears to have issues since the ADB monitor reports "devicemonitor failed to start monitoring" repeatedly. Is there a way to connect directly from the client on the development machine to the daemon on the device using the network instead of the USB connection or possibly another viable options?

30条回答
浅入江南
2楼-- · 2018-12-31 01:21

I've found a convenient method that i would like to share.

For Windows

Having USB Access Once

No root required

Connect your phone and pc to a hotspot or run portable hotspot from your phone and connect your pc to it.

Get the ip of your phone as prescribed by brian (Wont need if you're making hotspot from your phone)

adb shell ip -f inet addr show wlan0

Open Notepad

Write these

@echo off
cd C:\android\android-sdk\platform-tools
adb tcpip 5555
adb connect 192.168.43.1:5555

Change the location given above to where your pc contains the abd.exe file

Change the ip to your phone ip.

Note : The IP given above is the basic IP of an android device when it makes a hotspot. If you are connecting to a wifi network and if your device's IP keeps on changing while connecting to a hotspot every time, you can make it static by configuring within the wifi settings. Google it.

Now save the file as ABD_Connect.bat (MS-DOS batch file).

Save it somewhere and refer a shortcut to Desktop or Start button.

Connect through USB once, and try running some application. After that whenever you want to connect wirelessly, double click the shortcut.

Note : Sometimes you need to open the shortcut each time you debug the application. So making a shortcut key for the shortcut in desktop will be more convenient. I've made a shortcut key like Ctrl+Alt+S. So whenever i wish to debug, i'll press Shift+F9 and Ctrl+Alt+S

Note : If you find device=null error on cmd window, check your IP, it might have changed.

查看更多
孤独总比滥情好
3楼-- · 2018-12-31 01:22

To connect your tablet using TCP port. Make sure your system and device is connected to same network.

  1. Open console cmd.exe
  2. Type adb tcpip 5555
  3. Go to System -> Development option -> USB debugging --> Uncheck it for TCPIP connection
  4. Type adb connect 192.168.1.2 this is your device IP address
  5. Connected to 192.168.1.2

Connected using port forward Try to do port forwarding,

adb forward tcp:<PC port> tcp:<device port>

like:

adb forward tcp:5555 tcp:5555.

C:\Users\abc>adb forward tcp:7612 tcp:7612

C:\Users\abc>adb tcpip 7612 restarting in TCP mode port: 7612

C:\Users\abc>adb connect 10.0.0.1:7612

connected to 10.0.0.1:7612

If you get message error: device not found connect a usb device to system then follow same procedure.
for a rooted device

setprop service.adb.tcp.port 5555
stop adbd
start adbd
查看更多
荒废的爱情
4楼-- · 2018-12-31 01:22

Bash util function:

function adb-connect-to-wifi {
    ip="$(adb shell ip route | awk '{print $9}')"
    port=5555
    adb tcpip ${port}
    adb connect ${ip}:${port}
}
查看更多
步步皆殇っ
5楼-- · 2018-12-31 01:25

From adb --help:

connect <host>:<port>         - Connect to a device via TCP/IP

That's a command-line option by the way.

You should try connecting the phone to your Wi-Fi, and then get its IP address from your router. It's not going to work on the cell network.

The port is 5554.

查看更多
梦寄多情
6楼-- · 2018-12-31 01:27

From a computer on a non-rooted device

(Note that this can be done using a rooted device as well, but you can use a shell on a rooted device which doesn't require a USB connection)

Firstly, open command prompt (CMD). If you use Android Studio or IntelliJ there is a console included in there you can use.

If possible, open the SDK location, right click, and press "start command prompt here". Not all have this option so you have to do this (/these) commands as well:

Change the drive (if applicable)

D: 

And access the sdk and platform tools. Replace this path with your SDK location:

D:/sdk/path/here/platform-tools

Now you have access to the Android debug bridge.

Now, with the device connected to the computer, do:

adb tcpip <port> 
adb connect <ip>:<port>

Where is the port you want to connect to (default is 5555) and is the IP of the device you want to connect to.

Please note: 5555 is the default port and just writing the IP address connects it. If you use a custom port you can at least improve the security a bit. USB debugging over wifi can be abused, but only if the device is connected to the computer who wants to abuse the device. Using a non-default port at least makes it a bit harder to connect.

If you use a custom port, make sure to add it after the IP. Writing no port connects to 5555 and if you didn't use that the connection will fail.

You can find the IP address of a device in two ways:

  • Depending on your device, the exact names may vary. Open settings and go to About device -> Status -> IP address

  • Use ADB to get the IP

From the console, do:

adb shell ip -f inet addr show wlan0

And once you are finished with the connection, you can disconnect the device from your computer by doing:

adb disconnect <ip>:<port>

Or no IP to disconnect all devices. If you used a custom port, you must specify which port to disconnect from. The default is 5555 here as well.

To disable the port (if that is something you want to do) you do this command with the device connected:

adb usb

Or you can restart the device to remove the tcpip connection

From a computer on a rooted device

Firstly, you need access to the shell. You either connect the device using a usb cable and use adb shell or download an app from Google Play, FDroid, or some other source.

Then you do:

su
setprop service.adb.tcp.port <port>
stop adbd
start adbd

And to connect the device, you do as in the non-rooted version by doing adb connect <ip>:<port>.

And if you want to disable the port and go back to USB listening:

setprop service.adb.tcp.port -1
stop adbd
start adbd

You can also use an Android Studio plugin to do it for you (don't remember the name right now), and for rooted users there's also the option of downloading an Android app to set up the phone connection (adb connect is probably still required).

Some phones have a setting in developer options (this applies to some unrooted phones, though probably some rooted phones too) that allows for toggling ADB over wifi from the device itself without root or a computer connection to start it. Though there are few phones that have that

查看更多
浮光初槿花落
7楼-- · 2018-12-31 01:27

To switch between TCP and USB modes with just one command, you can add this to /init.rc:

on property:service.adb.tcp.port=*
    restart adbd

on property:service.adb.tcp.enable=1
    setprop service.adb.tcp.port 5555

on property:service.adb.tcp.enable=0
    setprop service.adb.tcp.port -1

And now you can use property service.adb.tcp.enable to enable or disable listening on port 5555. Run netstat to check whether it's listening. As you can see it will also trigger if you do wish to change service.adb.tcp.port manually.

查看更多
登录 后发表回答