Socket Communication (WiFi) between Android client and PC Server)

Source: Internet
Author: User

This article describes how to send the AP information continuously scanned by the android terminal 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. PCEnable serversocket on the 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

// Create a serversocket object
// By Wayne from www.cnblog.com/dwayne/
Serversocket Ss = new serversocket (30000); // listen to 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.

// Receives client messages
// By Wayne from www.cnblog.com/dwayne/

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. AndroidTerminal socketCommunication

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.

TimerTask task = new TimerTask(){    public void run() {        Message message = new Message();        message.what = 1;        handler.sendMessage(message);    }          }; 

Handler transmits the message content:

Handler handler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case 1: // The task break passed by handler after the timer execution time reaches;} super. handlemessage (MSG );}};

Because scanning tasks need to be executed continuously, a new thread is enabled to execute scheduled tasks.

// Start a separate thread to regularly send AP information to the server
// By Wayne from www.cnblogs.com/dwayne
New thread () {@ override public void run () {// todo auto-generated method stub timer. schedule (task,); // the method in which the timer is executed every 10 seconds after 2 seconds }}. start ();

Then, the AP information is scanned and sent to the server, and the result is saved.

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.

This class provides the primary API for managing all aspects of Wi-Fi connectivity. get an instance of this class by calling context. getsystemservice (context. wifi_service ). it deals with several categories of items:

  • The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
  • The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
  • Results of Access Point scans, containing enough information to make decisions about what access point to connect.
  • It defines the names of various intent actions that are broadcast upon any sort of change in Wi-Fi state.

Send messages to the server:

Socket = new socket ("192.168.1.211", 30000); // send the message printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (socket. getoutputstream (), true); out. println (Message );

Message indicates the AP information obtained.

The format of the information received by the test is:

SSID: icis_lab, bssid: 1c: AF: F7: 9A: 65: e4, capabilities: [WPA-PSK-TKIP + CCMP], level:-80, frequency: 2437

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.