实现Android手机通过WIFI和PC连接

移动开发 Android
大家如果手边没有路由器,只有无线网卡,一样可以使用手机自带的Wifi上网,前提电脑必须具备有线网络,并且无线网卡驱动已经正常安装!Let's go!其它Andro手机应该也可以使用。

最近一段事件一直在研究如何用wifi和PC连接,但是在网上找了很久,也看过很多例子。都没有成功。无奈只好自己研究。***自己写了一个小Demo。分享一下。[[69567]]

1.在程序中通过

  1. Runtime.getRuntime().exec("su"); 

获得手机root权限(手机必须是root之后的)。

2.重新启动adbd

  1. exec("stop adbd"); 
  2. exec("start adbd"); 

3.与PC建立连接(我是通过bat文件进程处理的)

  1. /** 
  2.          * 手机连接wifi. 
  3.          * 
  4.          * @param host 手机ip:端口号。例如:192.168.10.124:8888 
  5.          */ 
  6.         public int connectWifi(String host) { 
  7.             String cmd = ParseProperties.getProperties("dir") 
  8.                     + "bin/ConnectWifi.bat " + host; 
  9.             BufferedReader reader = null
  10.             int retcode = 0
  11.             try { 
  12.                 Process process = Runtime.getRuntime().exec(cmd); 
  13.       
  14.                 reader = new BufferedReader(new InputStreamReader( 
  15.                         process.getInputStream())); 
  16.                 @SuppressWarnings("unused") 
  17.                 String line = null
  18.                 String returnLine = null
  19.                 System.out.println("*****************************"); 
  20.                 while ((line = reader.readLine()) != null) { 
  21.                     if (line != null) 
  22.                         returnLine = line
  23.                     System.out.println(line); 
  24.                 } 
  25.                 if (returnLine.trim().startsWith("connected to")) { 
  26.                     retcode = SUCCESS
  27.                 } else if (returnLine.trim().startsWith("already connected to")) { 
  28.                     retcode = SUCCESS
  29.                 } else { 
  30.                     retcode = FAILE
  31.                 } 
  32.                 System.out.println("*****************************"); 
  33.             } catch (IOException e) { 
  34.                 e.printStackTrace(); 
  35.                 retcode = FAILE
  36.             } finally { 
  37.                 if (reader != null) { 
  38.                     try { 
  39.                         reader.close(); 
  40.                     } catch (Exception e) { 
  41.                         e.printStackTrace(); 
  42.                     } 
  43.                 } 
  44.             } 
  45.             if (retcode == 0) { 
  46.                 retcode = FAILE
  47.             } 
  48.             return retcode; 
  49.         } 

bat文件

bat文件中的内用很简单 adb connect %1

通过上述方法就能通过wifi和PC连接在一起了。注意:手机和PC机要在同一个局域网中。

责任编辑:闫佳明 来源: oschina
相关推荐

2020-08-28 10:01:08

AndroidPC手机屏幕

2018-01-24 16:30:43

Linux命令Wifi

2019-11-08 10:00:09

Windows手机应用程序

2023-06-12 15:35:04

综合布线光纤以太网

2009-12-16 10:23:04

家庭无线路由设置

2021-04-30 12:17:18

ChromiumEdge浏览器

2022-09-11 19:55:46

MAC 地址WiFi 地址

2013-01-24 11:09:57

索尼存储NFC

2021-09-14 09:50:29

物联网超连接IOT

2021-11-08 05:33:25

PC安卓手机投屏

2011-12-15 13:14:08

免费wifi私有云

2011-12-15 09:58:38

Wifi私有云

2012-02-22 11:08:07

UbuntuAndroid

2013-09-16 15:00:15

2010-05-18 10:07:23

桌面PC云计算

2020-11-02 11:56:57

鸿蒙 WiFi操作

2014-10-29 10:38:36

手机PC协议

2023-03-29 10:03:36

2010-08-16 14:17:46

无线路由

2021-06-23 09:03:19

iPhone WiFi bug漏洞
点赞
收藏

51CTO技术栈公众号