WIFI communication of Android hardware communication

1. Introduction

1.1 With the popularization of the network and the development of communication technology, the transmission speed of the network is getting faster and faster, and wifi technology has also become the most basic configuration of mobile devices. We can realize the information transmission between the mobile phone and the mobile phone through wifi, and of course it can also be transmitted with any other device with a wifi module.

1.2 wifi and bluetooth are the most common and commonly used wireless communication technologies in our lives, and contemporary smart homes are basically implemented based on these two protocols. We can compare the following two technologies in order to choose the appropriate communication method:

Compared Bluetooth wifi
Transmission distance Short, generally about 10m Length, about 100m-400m
Transmission power consumption Low power consumption, 5mA High power consumption, 10-50mA
transfer speed Slow, 1Mbps Fast speed, 300Mbps
safety performance High, stable transmission Low, the password is cracked
Application field Low-power devices such as wearable devices and smart homes Big data transmission scenarios such as factory equipment and printers

1.3 With the development of the Internet of Things, wireless communication technology has been involved in every corner of our lives, such as

industry

 smart home

Wifi communication has become an important technology for these Internet of Things communication. Let's take a look at the detailed steps of wifi communication

Two wifi communication steps

2.1 Before wifi communication, both communication parties must be in the same local area network. If the connection is a factory machine, the machine is generally equipped with a wifi module

There are two ways to establish a connection

Method 1: Make both the mobile phone and the machine wifi connect to the external network (that is, the wifi of your indoor router), so that the two devices are in the same local area network and can be connected and communicated

Method 2: The mobile phone is directly connected to the wifi module network of the machine. This has a disadvantage that it cannot access the Internet and can only transmit data with the wifi of the machine.

So the general steps are to connect to the wifi module network of the machine first, and then open the network configuration webpage. The opening method is to enter the IP address of the wifi module (such as http://192.168.0.1) in the browser, and connect to the external network through the network configuration page, so that the mobile phone It can surf the Internet, request server data, and communicate with machines

2.2 Create IP, serial communication address, and establish wifi connection through Socket

Runnable wifiConnectRunnable = new Runnable() {
	@Override
	public void run() {
		try {
			IP = PreferencesUtils.getString(App.getInstance(), PreferencesUtils.MODEL_IP, "192.168.2.1");
			PORT = PreferencesUtils.getString(App.getInstance(), PreferencesUtils.MODEL_PORT, "9100");
			socketAddress = new InetSocketAddress(IP, Integer.parseInt(PORT)); //获取sockaddress对象
			socket = new Socket(); //实例化socket
			socket.connect(socketAddress, 2000); //设置超时参数
			Log.e(TAG, "wifi: " + "wifi连接成功");
			handler.sendEmptyMessage(1);
		} catch (Exception e) {
			Log.e(TAG, "wifi: " + e.getMessage());
			e.printStackTrace();
		}
	}
};

2.3 Get the IO stream of Socket

mOutputStream = socket.getOutputStream();
mInputStream = socket.getInputStream();

2.4 Read wifi message

private class ReadThread extends Thread {
	@Override
	public void run() {
		super.run();
		while (!isInterrupted()) {
			int size;
			try {
				byte[] buffer = new byte[512];
				if (mInputStream == null) return;
				size = mInputStream.read(buffer);
				if (size > 0) {
					String mReception=new String(buffer, 0, size);
					String msg = mReception.toString().trim();
					Log.e(TAG, "接收短消息:" + msg);
				}
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
		}
	}
}

 2.5 Send wifi message

private class WriteRunnable implements Runnable {
	@Override
	public void run() {
		try {
			String cmd="KZMT;";
			Log.e(TAG, "发送短消息:" + cmd);
			mOutputStream.write(cmd.getBytes());
			mOutputStream.flush();
		} catch (IOException e) {

		}
	}
}

2.6 Disconnect the wifi socket connection

/**
 * 关闭Wifi的Socket连接
 */
public void closeWifiStream() {
	try {
		if (mOutputStream != null) {
			mOutputStream.close();
			mOutputStream = null;
		}
		if (mInputStream != null) {
			mInputStream.close();
			mInputStream = null;
		}
		if (socket != null) {
			socket.close();
			socket = null;
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

 2.7 Summary

Wi-Fi is a LAN wireless communication technology, so the communication device must be in the same LAN segment, and then maintain a socket long connection, obtain IO stream, and transmit data through the IO stream channel.

Guess you like

Origin blog.csdn.net/qq_29848853/article/details/130258276