The Android terminal continuously scans AP information and sends it to the server. First, a virtual network link is formed between the Android terminal and the PC based on the TCP protocol. Use ServerSocket to create a TCP server, and then use the Socket constructor on the Android client to connect to the server. The Android terminal is connected to the PC through WIFI in the same LAN.
1. Enable ServerSocket on the PC Server
Before establishing a virtual link, the two communication entities must prepare one party to actively accept connection requests from other communication entities.
Use the ServerSocket object to listen for Socket connections from the client.
[Plain] // create a ServerSocket object
ServerSocket ss = new ServerSocket (30000 );
// Listen for requests from the client
While (true ){
Socket s = ss. accept ();
...
}
// Create a ServerSocket object
ServerSocket ss = new ServerSocket (30000 );
// Listen for requests from the client
While (true ){
Socket s = ss. accept ();
...
}
If no connection is established, the Instance remains in the waiting state. After receiving the connection request, the system obtains the message to the input stream and saves it to the file.
[Plain]/receive client messages
BufferedReader in = new BufferedReader (new InputStreamReader (client. getInputStream ()));
String str;
BufferedWriter bw = new BufferedWriter (new FileWriter ("D:/ApInfo" + (I ++) + ". txt "));
While (str = in. readLine ())! = Null ){
System. out. println (str );
Bw. write (str );
Bw. newLine ();
}
// Receives client messages
BufferedReader in = new BufferedReader (new InputStreamReader (client. getInputStream ()));
String str;
BufferedWriter bw = new BufferedWriter (new FileWriter ("D:/ApInfo" + (I ++) + ". txt "));
While (str = in. readLine ())! = Null ){
System. out. println (str );
Bw. write (str );
Bw. newLine ();
}
2. Use Socket to communicate with Android Terminals
The client uses the Socket constructor to connect to the server. You can specify the Server IP address and port number.
Socket s = new Socket ("192.168.1.100", 30000 );
In this way, the accept () method on the server gets a response, and then runs down. The server and client form a pair of interconnected sockets. There is no distinction between the server and the client for further communication, and both communication is performed through the input and output streams.
Detailed steps:
Handler and TimerTask are used to periodically scan AP information and send it to the server. TimerTask specifies the task to be performed at the specified time.
[Plain] TimerTask task = new TimerTask (){
Public void run (){
Message message = new Message ();
Message. what = 1;
Handler. sendMessage (message );
}
};
TimerTask task = new TimerTask (){
Public void run (){
Message message = new Message ();
Message. what = 1;
Handler. sendMessage (message );
}
};
Handler transmits the message content:
[Plain] Handler handler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
// Tasks passed by handler after the timer time is reached
Break;
}
Super. handleMessage (msg );
}
};
Handler handler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
// Tasks passed by handler after the timer time is reached
Break;
}
Super. handleMessage (msg );
}
};
Then, the AP information is scanned and sent to the server, and the result is saved.
[Plain] WifiManager wifiManager = (WifiManager) getSystemService (WIFI_SERVICE );
WifiManager. startScan ();
MWifiList = wifiManager. getScanResults ();
WifiManager wifiManager = (WifiManager) getSystemService (WIFI_SERVICE );
WifiManager. startScan ();
MWifiList = wifiManager. getScanResults ();
WifiManager indicates that it can be used to process the configured network, the current connected network, and the scanning of AP information.
Send messages to the server:
[Plain] socket = new Socket ("192.168.1.211", 30000 );
// Send a message to the server
PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket. getOutputStream (), true );
Out. println (message );
Socket = new Socket ("192.168.1.211", 30000 );
// Send a message to the server
PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket. getOutputStream (), true );
Out. println (message );
The message is the obtained AP information, and the format of the information received in the test is:
SSID: ICIS_LAB, BSSID: 1c: af: f7: 9a: 65: e4, capabilities: [WPA-PSK-TKIP + CCMP], level:-80, frequency: 2437
Unity3d learns how to implement Socket communication between Android clients and PC servers