首页 > 其他分享 >如何用adb连接android手机

如何用adb连接android手机

时间:2023-01-30 14:55:33浏览次数:37  
标签:usb wifi adb 手机 android 连接 USB

如何用adb连接android手机

以下为手机“root”情况下操作。。。。手机没root请拉到笔记最后,有解决方法
利用adb来连接手机, 有两种方式:

  1. wifi
  2. usb

通过wifi, 利用adb来连接手机。

在pc的cmd中输入命令 adb connect 192.168.1.100
其中 192.168.1.100 就是手机局域网的ip. 如果连接成功, 就可以进入android的shell了。

我自己在操作过程中,pc能ping通过手机,但是adb连接手机,出现了一点点问题,提示unable to connect to 192.168.1.100:5555,这个是什么原因呢?原来手机的默认adb服务是没有打开的,否则,别人adb能随便连,那岂不是很不安全么?所以,我们要想办法把手机上的adb服务打开。怎么搞呢?首先要在手机上下载一个android模拟器(当然,如果你有,就不用下载了),然后切到root权限,并执行如下命令打开adb服务:

su
setprop service.adb.tcp.port 5555
stop adbd
start adbd

然后adb就可以连接手机了, 如图:
adb 连接手机结果截图

以下为非root手机环境下操作:

  1. 确保PC正确安装了ADB驱动并且能够识别你的Android设备 。
  2. Android设备USB调试模式已打开
  3. 用USB数据线将PC与设备相连接
  4. 在android-sdk\platform-tools\该路径下打开命令行
  5. 执行以下命令:
    adb kill-server
    adb start-server
    adb tcpip 5555
    adb connect xxx.xxx.xxx.xxx:5555
    

其中xxx.xxx.xxx.xxx为你手机的无线局域网的ip地址,显示连接成功之后拔掉数据线即可。


简介

在默认情况下adb是通过USB连接的,但是adb也支持通过wifi连接,前提是使用adb命令的电脑终端与待调试的手机在同一网段下。这样,在没有usb或者远程下都可以完成调试手机。但是手机的调试模式改为wifi后,手机连接usb就会无效,包括充电。可以将手机在wifi下切回USB即可。

连接USB线转为WIFI(前提adb通过usb连接)

在命令行中执行
adb tcpip 5555
//可以断开USB线,此时会发现usb充电无显示,连接数据线已无效。
//连接wifi
adb connect android设备IP地址(如:adb connect 192.168.43.144)
//断开wifi
adb disconnect
//WIIF转为USB(前提adb通wifi连接)
adb usb
//此时USB数据线可以正常使用。

adb无连接到WIFI

//需要取得超级管理员权限执行su,再执行
setprop service.adb.tcp.port 5555
stop adbd
start adbd
//连接wifi
adb connect android设备IP地址(如:adb connect 192.168.43.144)

adb无连接到USB

//需要取得超级管理员权限执行su,再执行
setprop service.adb.tcp.port -1
stop adbd
start adbd

脚本切换

新建一个文件命名为adbTowifi.sh

#!/bin/bash
#Modify this with your IP range
MY_IP_RANGE="192\.168\.43"
#You usually wouldn't have to modify this
PORT_BASE=5555
#List the devices on the screen for your viewing pleasure
adb devices
echo
#Find USB devices only (no emulators, genymotion or connected devices
declare -a deviceArray=(`adb devices -l | grep -v emulator | grep -v vbox | grep -v "${MY_IP_RANGE}" | grep " device " | awk '{print $1}'`)
echo "found ${#deviceArray[@]} device(s)"
echo
for index in ${!deviceArray[*]}
do
echo "finding IP address for device ${deviceArray[index]}"
IP_ADDRESS=$(adb -s ${deviceArray[index]} shell ifconfig wlan0 | awk '{print $3}')
echo "IP address found : $IP_ADDRESS "
echo "Connecting..."
adb -s ${deviceArray[index]} tcpip $(($PORT_BASE + $index))
adb -s ${deviceArray[index]} connect "$IP_ADDRESS:$(($PORT_BASE + $index))"
echo
echo
done
adb devices -l
#exit

//以上脚本文件,mac或者Linux直接可以运行,windows上需要安装一些如msysgit或者Cygwin才可运行以上Linux shell //前提需要usb连接adb,待执行玩命令后,可以拔掉usb数据线,此时手机切换至wifi连接,待连接上wifi后,如切回至usb,使用adb usb或者重启设备即可 
sh adbTowifi.sh

标签:usb,wifi,adb,手机,android,连接,USB
From: https://www.cnblogs.com/VoidCom/p/17075925.html

相关文章