Then the above serial port protocol, this article again describes how to implement the Android side. Now the programmer division is very clear, many will C not Java, Java will not necessarily develop Android. And my job is to develop Java background, Android development can only be an amateur player, but the development of a small car control program is still possible.
Let's start with the Android development environment, Android Studio. Such as:
After we create a new project, the main files to be changed are in this main folder. The Java folder is a. java file that puts the program logic. The res file is placed in the configuration file for the interface presentation, Androidmanifest.xml is the ability to configure the entire project, entry code, and so on.
My design is this, in the first interface to show the Bluetooth list and control buttons. After selecting the Bluetooth connection, hide the Bluetooth list, the interface is only the control button. The following progress bar-like control is used to set the speed using the Android PWM feature.
After connecting the Bluetooth:
This set up a lot of small technical points, such as setting a horizontal screen, how to hide the control, how the control adapts to the screen size. These in the professional Android programmer eye may not be a thing, but I still spend a lot of time to check when I do. For example, how to automatically adapt to the screen after hiding, I use a relative layout. This can be seen in the Dark Horse Programmer's Android video course.
Here's a simple and important technical point, how does Bluetooth stay connected? During the actual operation, the user may find that Bluetooth is disconnected for no apparent reason. At first I had to restart the app to connect, really to change the following lines of code:
try {
Os.write (Msg.getbytes ());
} catch (IOException e) {
E.printstacktrace ();
Send exception, re-connect socket
SYSTEM.OUT.PRINTLN ("Send exception, re-connect socket");
Connectsocket (lvdevices);
}
Explain the OS is the OutputStream, Bluetooth output stream. MSG is the message content to be sent. If an exception occurs, then connect to Bluetooth. In fact, the best thing is to send the content again after the connection.
Well, a simple control program is finished.
Smart Car Seven "PC Android"