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.