I recently in the study of embedded serial communication, the task is to write a mobile phone-side remote control to control the double-wheel balance trolley. The interface only takes one hours to write, and the important question is how to communicate with the SPP-CA Bluetooth module that the board is carrying.
The Spp-ca module comes with code, and all I use here is the default mode of SPP-CA. Where the baud rate is 9600. Readers to modify their matching password, baud rate, etc. use the Serial debugging tool to modify the SPP-CA using the AT command. Refer to its technical manual for details.
First introduced the Android side, the official SDK has given a bluetoothchat version, this version can be modified to serial communication. Since the source code is readily available online, it is not duplicated here. Modify the method as follows:?
First of all? Modify the UUID, in the Android system, with the mobile phone to communicate with the UUID and Bluetooth serial communication with the UUID is different. Mobile communication can use the generated UUID to communicate, but with the Bluetooth serial port is not necessary to use the following UUID. (Location: Bluetoothchatservice.java)
1 private static final UUID My_uuid = uuid.fromstring ("00001101-0000-1000-8000-00805F9B34FB");?
Here, if all other resource files have been added and correctly followed, it is possible to run normally without a fatal error. But notice one place, in the Bluetoothchat file, The buttons that it uses for connect and discovery are implemented in the menu. Now the Android system is ubiquitous in the replacement menu key This feature, so there may be some small white found without these two buttons in the case, do not know how to jump to Bluetoothconnect.java this interface. WORKAROUND: You can set a button, press the button, and jump out of the menu. The Jump menu code is as follows:
1 menubutton.setonclicklistener (new Onclicklistener () { 2 3 @Overridepublic 4 5 void OnClick (View v) {6 7 openoptionsmenu (); 8 9 }1011 });
Finally, an important question is presented. I did not test on the mobile phone, but most of the internet I can find the mobile phone for Bluetooth communication blog is not marked this error?, should be the phone between the error does not appear. However, the same code with Bluetooth serial communication will have garbled!! There will be garbled!! There will be garbled!!
This garbled sometimes appears at the end, or a string of characters passed over, just show the last character and so weird strange phenomenon. This phenomenon found a problem after I carefully studied the run () method in his bluetoothchatservice and combined it with an online blog.
This problem is caused by the InputStream class. It reads all the characters in the InputStream every time the loop is read. However, when the Bluetooth serial communication, it may lead to a sentence is not finished, it has been read out. So there will be problems such as cohesion and garbled.
Here is the workaround:? By available method to know the number of strings in the InputStream, because I need to pass a character each time, so I am here buffer is char[1], after receiving the completion, the available minus the corresponding number of strings can be.
1 public void Run () {2 System.err.println ("++++++++++run thread+++++++++++++++++++++++"); 3 int bytes; Bytes returned from read () 4 int availablebytes = 0; 5//Keep listening to the InputStream until a exception occurs 6 while (true) {7 try {8 availablebytes + = Mminstream.available (); 9 if (available Bytes > 0) {byte[] buffer = new BYTE[1];11//buffer store for the STREAM12 Read from the InputStream13 bytes = mminstream.read (buffer); 14 AVAILABLEBYTES--;15//LOG.D ("mminstream.read (buffer);", new String (buffer); 16 if (bytes > 0) {//Send the obtained bytes to the UI activit Y18 Mhandler.obtainmessage(Bluetoothchat.message_read, Bytes,-1, buffer). Sendtotarget (); 19}20 }21} catch (IOException e) {log.d ("Error reading", E.getmessage ()); 23 E.printstacktrace (); }25 26}27}
The above is the complete bluetoothchat modification method. has been verified through the Serial debugging tool, send acceptance is no problem.
I update my blog for the first time, if there is insufficient please point out.
Modification method of Bluetoothchat for Bluetooth serial communication