Connect Android phones to PCs through WIFI

Source: Internet
Author: User

Recently, I have been studying how to connect to a PC using wifi, but I have been searching for it on the internet for a long time and have seen many examples. Failed. I had to study it myself. Finally, I wrote a Demo myself. Share it.

1. Pass

Runtime.getruntime(cmd.exe c ("su ");

Obtain the root permission of the mobile phone (the mobile phone must be after the root permission ).

2. Restart adbd

Exec ("stop adbd ");

Exec ("start adbd ");

3. Establish a connection with the PC (I handled it through the bat file process)

/**
* Connect to wifi via mobile phone.
*
* @ Param host: Mobile Phone ip Address: port number. Example: 192.168.10.124: 8888
* @ Return retcode success: 1, failure: 2
*/
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(cmd.exe c (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 ")){
Retcode = SUCCESS;
} Else if (returnLine. trim (). startsWith ("already connected ")){
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 file

In the bat file, use adb connect % 1.

By using the above method, you can connect to the PC through wifi. Note: mobile phones and PCs must be in the same LAN.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.