實現Android手機通過WIFI和PC連接
作者:葬淚
大家如果手邊沒有路由器,只有無線網卡,一樣可以使用手機自帶的Wifi上網,前提電腦必須具備有線網絡,并且無線網卡驅動已經正常安裝!Let's go!其它Andro手機應該也可以使用。
最近一段事件一直在研究如何用wifi和PC連接,但是在網上找了很久,也看過很多例子。都沒有成功。無奈只好自己研究。***自己寫了一個小Demo。分享一下。
1.在程序中通過
- Runtime.getRuntime().exec("su");
獲得手機root權限(手機必須是root之后的)。
2.重新啟動adbd
- exec("stop adbd");
- exec("start adbd");
3.與PC建立連接(我是通過bat文件進程處理的)
- /**
- * 手機連接wifi.
- *
- * @param host 手機ip:端口號。例如:192.168.10.124:8888
- */
- public int connectWifi(String host) {
- String cmd = ParseProperties.getProperties("dir")
- + "bin/ConnectWifi.bat " + host;
- BufferedReader reader = null;
- int retcode = 0;
- try {
- Process process = Runtime.getRuntime().exec(cmd);
- reader = new BufferedReader(new InputStreamReader(
- process.getInputStream()));
- @SuppressWarnings("unused")
- String line = null;
- String returnLine = null;
- System.out.println("*****************************");
- while ((line = reader.readLine()) != null) {
- if (line != null)
- returnLine = line;
- System.out.println(line);
- }
- if (returnLine.trim().startsWith("connected to")) {
- retcode = SUCCESS;
- } else if (returnLine.trim().startsWith("already connected to")) {
- retcode = SUCCESS;
- } else {
- retcode = FAILE;
- }
- System.out.println("*****************************");
- } catch (IOException e) {
- e.printStackTrace();
- retcode = FAILE;
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- if (retcode == 0) {
- retcode = FAILE;
- }
- return retcode;
- }
bat文件
bat文件中的內用很簡單 adb connect %1
通過上述方法就能通過wifi和PC連接在一起了。注意:手機和PC機要在同一個局域網中。
責任編輯:閆佳明
來源:
oschina