Based on the Communication experience between Android hardware devices and servers, the following uses UDP Byte communication as an example to establish a undirected connection, with a 0530 shouzha and android0530
During this period of time, all the tutorials of the enterprise number series were suspended because I grabbed several projects on hand and did not talk about working overtime, today, we have just rounded up the new project to around % 80.
Let's take a look. I'm going to have a full day tomorrow. I'll come over for a test on Monday. Many of my friends have come to ask questions. I really don't have time. Sorry!
Today, I will share my summary of this project. The source code will not be pasted. Because it is a commercial project, it only conveys the coding idea. I hope other friends will have a reference when encountering similar projects, cannot be found
A little thought
UDP is used for communication. Each command cannot exceed 1024 bytes. all multibyte integer data is transmitted in bytes in the network sequence. Each time a command is reported by the terminal, the platform returns a general confirmation message. Terminal example
If no general validation is received, it is recommended to resend it three times.
Ø location reporting: the current GPS location information and status information are reported based on the scheduled interval set by parameters.
Ø heartbeat: The heartbeat information is reported based on the heartbeat interval set by the parameter.
Take photos: receive instructions from the platform to take photos; or take photos automatically when exceptions occur;
Uploading images: upload photos to the platform one by one, and support platform package completion requests
Accept request: receive request commands from the platform, call the navigation interface, and transmit the longitude and latitude of the destination to the navigation system for automatic navigation
Ø ad display: The receiving center sends ad display instructions for AD display and TTS broadcast (call the TTS interface)
Ø
1. Interface display
Basically, the APP does not need to display the interface. The only display is the advertisement display function.
The advertisement must be displayed in a floating window, specifying the position and size of the window, specifying the font, and displaying the stay time according to the time specified in the agreement. After the stay time is exceeded, the display will automatically disappear.
After a user (individual user) performs a series of operations on the server (public platform), a third-party server will communicate a series of information and find the corresponding terminal number device, under this unique device ID, we need to verify and collect packets, parse and distribute protocols, and provide corresponding operations. Is it very easy to say? The truth is not what you think. Haha, I believe every programmer understands the meaning of this sentence!
Individual users perform remote control operations:
For example, in the red box, the user clicks on the vehicle positioning, and the command first reaches the third-party server through the public number. What the third party needs to do at this time is to receive and send the corresponding datagram based on the key, the following process can be continued only when the terminal is bound with a very number. The third-party server verifies the validity of the user through the port and unique ID plus the datagram length and foreign value specified by both parties.
This is the terminal that receives the UDP packet from a third party. After matching and verification, the client delivers the protocol and performs the operation. This is the byte [] context part, and other bytes are not attached.
I forgot to say that when sending and receiving packets, I have to judge the IP address, PORT, etc.
The mobile alarm contact involves some private information, so I will talk about the general process.
Mobile alarm (vibration warning ):
Have you ever been around? It is a gravity sensor that is connected together with the software through a serial port in the hardware, through a gravity sensor, acceleration, vibration, and so on, according to the threshold set by the Code to make the appropriate alarm, to implement this function, we need to call hardware. sensorEventListener
Determine the x, y, and zcoordinate changes by implementing the two functions of this interface.
Double speed = Math. sqrt (deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ)/timeInterval * 10000;
// Reach the speed threshold and send a prompt
If (speed> = SPEED_SHRESHOLD ){
// Vibration alarm
Handler. sendEmptyMessage (SENSOREVENT_STATE );
}
OnSensorChanged (SensorEvent event) is used in this function. During my test, once the gravity is affected or the terminal device is shaken, related TTS will be broadcast, at the same time, a UDP request will be sent to a third-party server to the user's mobile phone.
Contacts:
After a user clicks the Menu, the third-party server will send the current longitude and latitude of the user to the terminal. What the terminal needs to do is to verify the package, parse, distribute, and process the user, after resolution, you can use the conversion parameters to obtain the navigation latitude and longitude parameters and call the interface. This is relatively easy and the most troublesome thing is the protocol encapsulation, parsing, verification, and encapsulation.
In addition, you also need to listen on the ACC status, because the project needs to use this status, which needs to access the GPIO port and need to be called proactively. Considering the necessity of this status, to avoid unnecessary memory waste and OOM, I obtain the ACC status every 5s in the new sub-thread and mark it. When reporting the location, the flag is OK, in addition, when the positioning fails, the bytes to be filled must be calculated by yourself, and the packet should also be calculated, otherwise the program will definitely crash!
There is also an advertisement pushed,
Because the app does not have an Activity and does not have a startup icon, It is a background service type, self-listening, and self-starting service. WindowManager must be used to push this advertisement. Otherwise, an inexplicable error may occur, even if your CustomDialog sometimes has a problem, it will cause you a headache! In my code, I set a non-window mode for this ad pop-up window. That is to say, after my ad window comes out, clicking the return key and the area outside the area it occupies are invalid.
The window will not disappear, and the dot HOME window will not disappear. It is only displayed on the main interface, and TTS is also displayed on it.
There are many ways to disappear. Here I use TimerTask.
Exotic value calculation: From the instruction length to the exotic value of the content, it is the bitwise XOR of all bytes from the instruction length to the content, take a hexadecimal Integer as the verification code for each packet sent and received.
The package filling request will not be mentioned, which is very troublesome. If you really encounter a similar project, you can trust me
The most rare key is protocol encapsulation, protocol parsing, packets, data parsing, protocol distribution. It is estimated that none of the friends who are not in touch can understand the encapsulation protocol, in particular, data conversion will definitely give you a headache!
In fact, many smart home solutions and some small traffic consumption, a large number of applications use this, for example, QQ uses a combination of HTTP and UDP communication methods.
Remote photo taking:
The user clicks to take a photo remotely, instructs the user to go to the third-party server and then to the terminal. After the resolution is obtained, the user can retrieve the camera, take a photo, obtain the local time, and use the timestamp as the ID, this ID is converted to int to byte and then sent to a third party. Then, the Byte packet of the photo is reported. Because the Protocol can be 1 kb at most each time, the data packet must be sent through subcontracting.
Because UDP packet loss occurs frequently, protocol commands that need to be supplemented are provided in two ways at the same time.
Package completion command
I have consulted some colleagues from reading the document and solving the problem, including the agreement. It took me a few days to test and work overtime for several days, because I was also the first to get into touch with this stuff, I wrote down my experiences by the way, to facilitate the reference of my friends who came to access this project in the future.
I also spoke to the engineer standing at the front-line and said, "It's hard! Of course, it also includes me! Haha !!! Thank you!