Teach you to do the Bluetooth car (ii)

Source: Internet
Author: User

5th Section Btchat

This section begins with an introduction to the Arduino Bluetooth module, which works with Android apps to enable a Bluetooth chat app.

5.1 What is Bluetooth

Simply speaking is a technology of point-to-point communication between different devices.
There are large articles of the Bluetooth various protocols, various specifications ...
This course only covers the content and does not expand any more.

5.2 SDP

Service Discovery Protocol, or SDP, is a protocol that allows devices to discover services supported by other devices.
The Bluetooth protocol assigns a UUID to each service to differentiate between the various services.
The UUID of the SDP is 00001101-0000-1000-8000-00805F9B34FB .

5.3 Android Bluetooth app

Android provides a Bluetooth instance called Bluetoothchat
Here is the relevant introduction
Https://developer.android.com/samples/BluetoothChat/index.html
In the upper right corner there is a download, click Download to an Android project under Eclipse.

This app is a chat app for Android and needs to modify its UUID to chat with the Arduoni Bluetooth module.
Or you can download the apk that I shared on the bean network directly.

5.4 Arduino realizes Bluetooth

The Arduino encapsulates Bluetooth as a serial port.
We just need to connect the Bluetooth chip to a specific serial port, and then it can be treated as a serial port.

5.5 Arduino Module Connection

Let's review the serial hardware definition
Uno Example

USB interface and 0-, 1-pin are serial objects

Mega Example

USB interface and 0, 1 pins are serial objects.
14, 15 pins are SERIAL3 objects.
16, 17 pins are Serial2 objects.
18, 19 pins are Serial1 objects.

If you use UNO to develop Bluetooth applications, only the computer connected to the USB port, Bluetooth is also connected to 0, 1 pins.
Both connections can cause the USB port not to burn code.
The Bluetooth connection must be unplugged in order to burn the code.
Then connect the Bluetooth connection before you can debug Bluetooth.
It was a nightmare.
This is one of the reasons I recommend the Mega Development Board at the beginning of this article.

With Mega It's simple, it provides 4 serial ports, and we don't have the upper conflict with any other port than serial.
It's my way of connecting.

The VCC,GND pin of the Bluetooth module is connected to the mega positive and negative pins respectively.
The TX pin of the Bluetooth module is connected to the Mega RX3 pin, and the RX PIN is connected to the Mega TX3 pin.
Now, the USB port is the serial object, and Bluetooth is the Serial3 object.

5.6 Phone and Arduino connection

The Arduino is connected by the line on the previous section and connected to the computer with a USB cable.
Turn on Bluetooth, install btchat.apk to your phone, open the app, reference.

5.5 Arduino Code Modification

Because you want to use serial and SERIAL3, two serial ports need to be initialized

void setup() {  Serial.begin(9600);  Serial3.begin(9600);}

Use the Serial3.available() function to check whether the Bluetooth serial port has data input.
Serial3.read()read the input content of the Bluetooth serial port.
Use the Serial.print(Serial3.read()) serial monitor to output the contents of the previous step to the PC side.

void loop() {  while (Serial3.available()) {    Serial.print(Serial3.read());  }}

The code to read the output in the opposite direction

void loop() {  while (Serial3.available()) {    Serial.print(Serial3.read());  }  while (Serial.available()) {    Serial3.print(Serial.read());  }}

Try it, the phone and Arduino can communicate simply.

5.6 Bluetoothchat's Bug

Enter a long string, such as "abcdefghijklmnopqrstuvwxyz1234567890", through the serial monitor.

The data displayed by the cell phone is such that it is obviously not the result we want.

Let's see how the Android side is collecting data.
The relevant code for the data collected in
Bluetoothchat\application\src\main\java\com\example\android\bluetoothchat\ Bluetoothchatservice.java file, in the function of the Connectedthread class run() .

Start by running the Run function and request a piece of memory first.

...bytenewbyte[1024];...

The collection data code snippet is constantly circulating.

...while (true) {...

Read serial data into buffer

...bytes = mmInStream.read(buffer);...

Send the received data to fragment and display the content in the interface.

...mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)                    .sendToTarget();...

This is the run() function-related code

 Public void Run() {log.i (TAG,"BEGIN mconnectedthread");byte[] buffer =New byte[1024x768];intbytes//Keep listening to the InputStream while connected     while(true) {Try{//Read from the InputStreambytes = mminstream.read (buffer);//Send the obtained bytes to the UI ActivityMhandler.obtainmessage (Constants.message_read, Bytes,-1, buffer). Sendtotarget ();..

In this example, the Android side first receives ' a ', which is sent to the interface display.
Also received ' Bcdefghij ', continue to send to the interface display.
Until finally received ' 567890 '.
Everything looks perfect.
However, the problem is that the data is shared with a buffer several times, and the interface display is asynchronous processing. The previous interface has not been updated, and the next time the new data has been received. The interface then displays the last data in error.

This tutorial is dominated by Arduino, so the Android code is simple to modify and can be used on the line.
Move the code that applies the memory to the while loop.

bytenewbyte[1024];

The modified code.

 Public void Run() {log.i (TAG,"BEGIN mconnectedthread");intbytes//Keep listening to the InputStream while connected     while(true) {byte[] buffer =New byte[1024x768];Try{//Read from the InputStreambytes = mminstream.read (buffer);//Send the obtained bytes to the UI ActivityMhandler.obtainmessage (Constants.message_read, Bytes,-1, buffer). Sendtotarget ();..

Teach you to do the Bluetooth car (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.