轉自: http://www.cnblogs.com/shangdawei/p/4480278.html
可用wifi、網口。
1.先要獲取root權限
$su
#stop adbd
#setprop service.adb.tcp.port 5555
#start adbd
然后在電腦端cmd中輸入adb命令:
adb connect <手機ip地址(端口默認是5555)>
$su
#stop adbd
#setprop service.adb.tcp.port 0
#start adbd
2.Android手機WiFi調試,查看logcat
在項目開發過程中,遇到這樣的場景:
有寫特殊Android設備只有一個USB口,當掛載U盤之后就無法連接USB實時進行調試了。這兒時候如果設備可以開啟WiFi,那就可以用WiFi進行調試;
要開啟網絡調試,執行下面指令即可:
$su
#stop adbd
#setprop service.adb.tcp.port 5555
#start adbd
上面完成之后就可以用adb命令:
adb connect 192.168.1.xx//Android設備的ip地址;
上述在設備連接電腦執行cmd時在
"stop adbd",// 關閉adbd
這一步會有問題,執行之后USB就斷開了,無法執行接下來的打開指令;
那怎么辦呢?
想到直接做一個apk,安裝在手機上執行,於是就有了下面的apk:
1 public void excuteStartShell() { 2 String[] commands = new String[] { 3 "setprop service.adb.tcp.port 5555",// 設置監聽的端口,端口可以自定義,如5554,5555是默認的 4 "stop adbd",// 關閉adbd 5 "start adbd",// 重新啟動adbd 6 }; 7 try { 8 List<String> temp = RootTools.sendShell(commands, 10, 3000); 9 for (int i = 0; i < temp.size(); i++) { 10 Log.i(TAG, "__This is result from root:__" + temp.get(i)); 11 } 12 } catch (Exception e) { 13 e.printStackTrace(); 14 } 15 } 16 17 public void excuteStopShell() { 18 String[] commands = new String[] { 19 "setprop service.adb.tcp.port -1",// 設置監聽的端口,端口可以自定義,如5554,5555是默認的 20 "stop adbd",// 關閉adbd 21 "start adbd",// 重新啟動adbd 22 }; 23 try { 24 List<String> temp = RootTools.sendShell(commands, 10, 3000); 25 for (int i = 0; i < temp.size(); i++) { 26 Log.i(TAG, "__This is result from root:__" + temp.get(i)); 27 } 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 }
3.通過網絡使用adb
在adb的說明文檔中提到:
“An ADB transport models a connection between the ADB server and one device
or emulator. There are currently two kinds of transports:
- USB transports, for physical devices through USB
- Local transports, for emulators running on the host, connected to
the server through TCP”
大意是說,在物理設備上,adb是通過USB連接到設備上的,而在模擬器上,adb是通過TCP協議連接到設備上的。
實際上在物理設備上,也可以讓adb通過TCP協議來連接設備(當然前提條件是你的設備要有網口)。
首先看一下下面這段源代碼,出自system/core/adb/adb.c,第921行:
1 /* for the device, start the usb transport if the 2 ** android usb device exists and "service.adb.tcp" 3 ** is not set, otherwise start the network transport. 4 */ 5 property_get("service.adb.tcp.port", value, "0"); 6 if (sscanf(value, "%d", &port) == 1 && port > 0) { 7 // listen on TCP port specified by service.adb.tcp.port property 8 local_init(port); 9 } else if (access("/dev/android_adb", F_OK) == 0) { 10 // listen on USB 11 usb_init(); 12 } else { 13 // listen on default port 14 local_init(ADB_LOCAL_TRANSPORT_PORT); 15 }
分析上述代碼可以發現,在adbd啟動時首先檢查是否設置了service.adb.tcp.port,
如果設置了,就是使用TCP作為連接方式;
如果沒設置,就去檢查是否有adb的USB設備(dev/android_adb),如果有就用USB作為連接方式;
如果沒有USB設備,則還是用TCP作為連接方式。
因此只需要在啟動adbd之前設置service.adb.tcp.port,就可以讓adbd選則TCP模式,
也就可以通過網絡來連接adb了。
這需要修改init.rc文件。如果不想修改,也可以在系統啟動之后,在控制台上執行下列命令:
#stop adbd
#set service.adb.tcp.port 5555
#start adbd
這樣就可以在主機上通過下列命令來連接設備了:
adb connetc <ip-of-device>:5555
4.adb同時支持USB和TCP調試
最近要調試otg功能,usb被占用,只能用無線adb來調試。
無線刷機不方便,網上下載的無線adb經常需要root,並且有寫無線adb無法使用,push apk比較麻煩。
附上修改的代碼,同時支持USB和tcp調試,不用每次刷機后push apk。
- 在build\tools\buildinfo.sh添加
echo"service.adb.tcp.port=5555"打開無線adb連接
也可以在system/build.prop里面直接添加service.adb.tcp.port=5555
- 在system\core\adb\adb.c里面將usbadb和無線adb監聽修改下判斷,完了變下boot就可以了
1 property_get("service.adb.tcp.port",value, "0"); 2 if(sscanf(value, "%d", &port) == 1 && port > 0) { 3 // listen on TCP port specified byservice.adb.tcp.port property 4 local_init(port); 5 } else if(access("/dev/android_adb", F_OK) == 0) { 6 // listen on USB 7 usb_init(); 8 } else { 9 // listen on default port 10 local_init(ADB_LOCAL_TRANSPORT_PORT); 11 }
修改后
1 property_get("service.adb.tcp.port", value, "0"); 2 if (sscanf(value, "%d",&port) == 1 && port > 0) { 3 // listen on TCP port specified byservice.adb.tcp.port property 4 local_init(port); 5 } 6 7 if (access("/dev/android_adb",F_OK) == 0) { 8 // listen on USB 9 usb_init(); 10 } else { 11 // listen on default port 12 local_init(ADB_LOCAL_TRANSPORT_PORT); 13 }
要兩個一起修改,不然修改了第一點USB就不能用了。