“Li Ziming, Class 6, Grade 3, your mother has two cans of Wangzai milk for you!” .

What is radio? Do you remember the loudspeakers on the radio station when you were in elementary school? There is nothing wrong. It’s a message delivered to an opening in a scene where everyone can hear it. They may be able to hear them all, but for the receivers of the broadcast, they are focused on the information they need.


chapter

  • Android communicating with iot Devices – Introduction to the concept
  • Communication between Android and iot Devices – the nature of data transfer
  • Communication between Android and iot devices – Layered network model
  • Communication between Android and Internet of Things devices – Principle of UDP protocol
  • Communication between Android and Internet of Things devices -TCP protocol principle
  • Communication between Android and iot devices – Customizes packets based on TCP/IP
  • Android communicates with iot Devices – What is byte order
  • Android communication with iot devices – byte message assembly and parsing
  • Android communicates with iot devices – using UDP broadcast to do device lookup
  • Android communicates with iot devices – achieves remote control of Android clients
  • Android communicates with iot devices -Android is a small server
  • Android communicating with iot Devices – Debugging tips
  • Android communicating with iot devices – parallel serial and queue
  • Android communicating with iot devices – data security
  • Android communicates with iot devices – heartbeat
  • Android communication with iot Devices – Network IO model

directory

  • demand
  • Ideas and Principles
  • implementation
  • conclusion

demand

We followed the air conditioner to say that you are now an Android iot engineer of XX smart air conditioner.

You now own the hardware:

  • Wifi air conditioning x3
  • Android devices x1
  • Wifi router X1

Your air-conditioner and Android devices are connected to the route, and they have been assigned their own Intranet IP addresses. We need some way to make cell phones and air conditioners recognize each other.

Ideas and Principles

According to the above conditions, we can analyze that they form a LAN through the way of routers. As long as the device has the CAPABILITY of UDP in the LAN, we can use the UDP broadcast packet function to find the device. (Please be familiar with broadcast, multicast and unicast)

UDP broadcast we only need to take advantage of the broadcast characteristics, to send data to a fixed port. IP uses a fixed broadcast address 255.255.255.255. So let me draw the flow.

The process is simple, we send a set of UDP broadcasts through the phone, then turn on the listen and wait for the device to respond to the phone. When multiple devices exist in the LAN, the device will reply its device number and IP address to the mobile phone. At this point, the phone will receive a reply message from multiple air conditioners. In this case, we only need to make a business distinction for SN. It’s usually an alias for a different SN. (Aliases help identify people, such as bedroom air conditioners, living room air conditioners, etc.) We often refer to this process as handshake protocol or search matching protocol.

Here’S a question I want you to think about: How do you fill in the wifi password when the device first connects to the route? There are many methods, and even some solutions have been mature to the hardware manufacturers. Hope we can have our own thinking.

Here are a few I know of:

  • 1. The built-in AP of the device first enables the hotspot for the mobile phone to connect to the device. After the connection, enter the SSID and password for the device to connect according to the proprietary protocol.
  • 2. The device has Bluetooth function, which can be paired with the mobile phone through Bluetooth and then connected to wifi.
  • 3. Use the Length field of the physical layer protocol of 802.11 for transparent transmission. Vendor products include SmartConfig and AirKiss solutions.
  • 4.NFC induction rf, etc.
  • 5. Sound transmission.

Among the above solutions, the 802.11 protocol scheme may be the most cost-effective hardware, and the rest of the solutions should be combined with other components and modules. However, this scheme will cause the failure of some special types of routers. The blogger has not found the cause yet, but it may be caused by the protocol encryption of the routing manufacturer. In the actual environment, you can use the second mobile phone to open the hot spot. In short, there is no optimal solution, only the one that most meets the requirements of the project.

implementation

Do not take up space, here do not paste the complete code, the important is the train of thought, important is the train of thought, important is the train of thought.

The following is the key code for sending UDP packets. It’s a collection of all the things that we’ve learned in the previous chapters. The protocol is set and the packet is disassembled according to the protocol.

Several key objects are provided for reference:

  • DatagramSocket contains sending and listening methods
  • InetSocketAddress Indicates the relationship between an address and a port
  • DatagramPacket packets
 public void sendData(String host, int port, byte[] data)  {
        clientSocket = new DatagramSocket();         
        serverAddress = new InetSocketAddress(host, port);  
        DatagramPacket packet = new DatagramPacket(data, data.length, serverAddress);
        clientSocket.send(packet);
        clientSocket.close();
        Log.d(TGA, "-- > port:" + port + " \n DATA:" + bytesToHexString(data, data.length));
    }
    
Copy the code

It is important to note that UDP packets are unreliable protocols, so we need to design our own protocols with recognizable header fields and data validation.


(‘ ◔ ‸◔ ‘) A month has passed and the progress of the article has been seriously delayed. All sorts of things. There is no more time for my blog. Finally settled down peacefully in Shanghai. Next week will be the journey to the new company. I hope I can still find time to write on weekends. Ha, ha, ha