參考:
http://blog.csdn.net/zimo2013/article/details/39552129?utm_source=tuicool&utm_medium=referral
1. 用usb線將手機和PC連接起來
2. 然后在手機端執行
adb root 然后使用adb shell登陸手機, 執行如下命令: setprop service.adb.tcp.port 5555 stop adbd && start adbd &
3. 然后在PC端執行
adb tcpip 5555 adb connect 192.168.*.* (手機ip):5555 (如果還連着USB, 輸入其他命令時先輸入adb devices, 輸入adb -s device_name 命令) 用下面的命令切回到usb方式 adb usb
4. 此時拔掉USB線, 使用adb shell命令,發現還可以登陸手機
為了省事,可以將上面的操作放到一個腳本中:
#!/bin/bash if [ "x$1" == "xf" ];then # 有時在wifi斷了后, 又重新連上了,可以直接使用./wifi.sh f phone_ip=`cat /tmp/phone_ip.1234` if [ "x$phone_ip" != "x" ];then
echo "connect to $phone_ip" adb -s $phone_ip connect $phone_ip fi exit 0
fi devices=`adb devices | grep "\<device\>" | awk '{print $1}'` count=0
for i in $devices do let count++
done
if [ $count -gt 1 ];then
select device in $devices; do break; done
elif [ $count -eq 0 ];then device=""
else device=$devices fi
if [ "x$device" == "x" ];then
echo "device: $device not exist" exit -1
fi port=`adb -s $device wait-for-device && adb -s $device shell "getprop service.adb.tcp.port"` if [ "x$port" != "x5555" ];then adb -s $device wait-for-device && adb -s $device root adb -s $device wait-for-device && adb -s $device shell "setprop service.adb.tcp.port 5555" adb -s $device wait-for-device && adb -s $device shell "stop adbd && start adbd &" adb -s $device wait-for-device && adb -s $device tcpip 5555
sleep 1
fi tmp=`adb -s $device wait-for-device && adb -s $device shell ifconfig | grep "172.16" | awk -F ':' '{print $2}' | awk '{print $1}'` if [ "x$tmp" == "x" ];then
echo "ip is none" exit -1
fi
echo "connect to $tmp"
echo "$tmp:5555" > /tmp/phone_ip.1234 adb -s $tmp:5555 connect $tmp
對於不能root的手機也可以:
1、 確保電腦和Android設備連接在同一個WIFI網絡環境。
2、 用USB線連接Android設備。連接上之后你的電腦就會檢查到設備並且ADB將會以USB模式啟動。可以通過adb devices命令檢查連接上的設備,用adb usb命令確認adb是運行在usb模式下面。
1 $ adb devices 2 List of devices attached 3 04bdc4c9252391b9 device 4
5 $ adb usb 6 restarting in USB mode
3、用adb tcpip模式重啟adb
1 $ adb tcpip 5555
2 restarting in TCP mode port: 5555
4、 查看Android設備的IP地址,這里有三種方式查看Android設備IP。
-
設置-關於手機-狀態信息-ip地址中查看
-
設置-WLAN-點擊當前鏈接上的Wi-Fi查看IP
-
通過ADB命令查看設備IP地址:adb shell netcfg 或者 adb shell ifconfig
5、知道設備IP地址之后,就可以用adb connect命令通過IP和端口號連接ADB了。
1 $ adb connect 192.168.1.3:5555
2 connected to 192.168.1.3:5555
3
4 #查看一下連接上的設備,usb連接和wifi連接都存在 5 adb devices 6 List of devices attached 7 04bdc4c9252391b9 device 8 192.168.1.3:5555 device
拔掉USB線,你會發現設備仍然是連接上的,如果沒有連接上,用剛才的命令重現嘗試一下。
完.
