您的位置:首页 > 移动开发 > Android开发

实现android手机通过WIFI和PC连接。

2014-03-14 15:42 786 查看
实现android手机通过WIFI和PC连接。
发表于11个月前(2013-04-07 16:20) 阅读(4671) | 评论(23
101人收藏此文章,
我要收藏

赞1

WIFI

wifi
android手机
android手机通过wifi连接电脑

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



1.在程序中通过

view source

print?

1
Runtime.getRuntime().exec(
"su"
);
获得手机root权限(手机必须是root之后的)。

2.重新启动adbd

view source

print?

1
exec(
"stop adbd"
);
2
exec(
"start adbd"
);
3.与PC建立连接(我是通过bat文件进程处理的)

view source

print?

01
/**
02
* 手机连接wifi.
03
*
04
* @param host 手机ip:端口号。例如:192.168.10.124:8888
05
* @return retcode 成功:1 ,失败:2
06
*/
07
public

int
connectWifi(String host) {
08
   
String cmd = ParseProperties.getProperties(
"dir"
)
09
 
+
"bin/ConnectWifi.bat "
+ host;
10
   
BufferedReader reader =
null
;
11
   
int

retcode =
0
;
12
   
try

{
13
  
Process process = Runtime.getRuntime().exec(cmd);
14
15
  
reader =
new
BufferedReader(
new

InputStreamReader(
16
process.getInputStream()));
17
  
@SuppressWarnings
(
"unused"
)
18
  
String line =
null
;
19
  
String returnLine =
null
;
20
  
System.out.println(
"*****************************"
);
21
  
while

((line = reader.readLine()) !=
null
) {
22
 
if

(line !=
null
)
23
returnLine = line;
24
 
System.out.println(line);
25
  
}
26
  
if

(returnLine.trim().startsWith(
"connected to"
)) {
27
 
retcode =SUCCESS;
28
  
}
else
if

(returnLine.trim().startsWith(
"already connected to"
)) {
29
 
retcode =SUCCESS;
30
  
}
else
{
31
 
retcode =FAILE;
32
  
}
33
  
System.out.println(
"*****************************"
);
34
   
}
catch
(IOException e) {
35
  
e.printStackTrace();
36
  
retcode =FAILE;
37
   
}
finally
{
38
  
if

(reader !=
null
) {
39
 
try

{
40
reader.close();
41
 
}
catch
(Exception e) {
42
e.printStackTrace();
43
 
}
44
  
}
45
   
}
46
   
if

(retcode ==
0
) {
47
  
retcode =FAILE;
48
   
}
49
   
return

retcode;
50
}
bat文件

bat文件中的内用很简单 adb connect %1
通过上述方法就能通过wifi和PC连接在一起了。注意:手机和PC机要在同一个局域网中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: