Fairy tales,

Source: Internet
Author: User

Fairy tales,

 

 

Gao Fei dog:

"You often think, now you... "

Bruto:

"Harrow! Gao Fei! Good mood! "

Gao Fei dog:

"Hi! I encountered another difficult problem. I had to think about it. I was dizzy and in a bad mood! "

Bruto:

"What high-tech projects are you studying? "

Gao Fei dog:

"I have a PM2.5 monitoring device that can measure and record the ambient temperature, humidity, and particulate matter concentration. Due to restrictions of application conditions, no display screen is provided, to be able to configure, manage, and obtain measurement data on the site, connect a bluetooth module on the device and use the mobile phone to implement these functions through Bluetooth communication. "

Bruto:

"Bluetooth communication is a very mature technology and should not be difficult to solve. "

Gao Fei dog:

"Of course, it is easy to find my bluetooth module on the mobile phone and match it successfully. But I am confused how to develop an App to communicate with the PM2.5 monitoring device! "

Bruto:

"You can use Android Studio to develop a mobile App. "

Gao Fei dog:

"Apps developed by Android Studio cannot run on iOS. Besides, I only use the C ++ language, while Android Studio uses the java language for development. "

Bruto:

"Let's look for Dr. Daisy and hear what she thinks. "

Gao Fei dog:

"Looking for her again? Sorry. "

Bruto:

"Dr. dais is a very enthusiastic and helpful person. The last time we went to ask for help, she had no shelves at all, and her attitude towards you was greatly improved. Didn't you see it? Maybe people are looking forward to asking you questions every day! "

Gao Fei dog:

"Really? You see the transfer is coming? I still have a chance? "

Bruto:

"Let's go! "

Gao Fei dog and Bruto again came to the daisi house and pressed the doorbell, "Ding Ling... Dingling bell... ".

Daisi:

"Who? Ah! Two of you. Please come in! "

"Please sit down and have some coffee or green tea? "

Gao Fei dog:

"You are welcome. Excuse me, I have to bother you again today. "

Daisi:

"What is it? "

Gao Fei dog:

"I want to develop a mobile App using C ++ to manage my PM2.5 monitoring device through Bluetooth communication, and this App must be running on Android and iOS. "

Daisi:

"What's the problem. "

Gao Fei dog:

"I just don't know how to do it, and I have no idea at all. "

Daisi:

"I have just created a similar project recently. I just want to talk to you about it. For me, it's just a review of what I 've learned. "

Bruto:

"You are so modest! "

Daisi:

"Let's talk about the principles of Bluetooth communication, and then let's talk about the specific implementation. "

"The principles of Bluetooth communication and TCP/UDP are almost the same. See the following table. "

 

Communication features

TCP/UDP Communication

Bluetooth Communication

Communication Mode

Client-server mode

Client-server mode

Communication endpoint

TCP port

Service

Communication endpoint Identification Method

TCP port number, which is a 16-bit binary number

Service UUID, which is a 128-bit binary number

Client Communication Process

Create client Socket

Connect to the server

Send data

Receive data

Create client Socket

Connect to the server

Send data

Receive data

 

Daisi:

"You can see that, compared with TCP/UDP, the difference is that the Bluetooth service replaces the port, the Service UUID replaces the port, and the service UUID is long. "

"Use a mobile App to manage embedded devices. A mobile phone is a client, and a bluetooth module on an embedded device is a server. "

Gao Fei dog:

"It's really clear! "

Daisi:

"TCP/UDP standards define a set of fixed ports corresponding to a set of standardized services. For example, TCP port 80 is used for Web Services, and TCP port 21 is used for FTP services. "

"Similarly, the bluetooth standard also defines a set of Fixed Service UUID corresponding to a set of standardized services. "See the following table:

 

Bluetooth service name

Service UUID

Serial Port

20171101-0000-1000-8000-00805f9b34fb

LAN Access Using PPP

20171102-0000-1000-8000-00805f9b34fb

DialupNetworking

20171103-0000-1000-8000-00805f9b34fb

OBEXObjectPush

20171105-0000-1000-8000-00805f9b34fb

OBEXFileTransfer

20171106-0000-1000-8000-00805f9b34fb

Cordless Telephony

20171109-0000-1000-8000-00805f9b34fb

Audio Source

2017110a-0000-1000-8000-00805f9b34fb

 

Daisi:

"Because the Bluetooth service UUID is too long, it is expressed in hexadecimal notation, which follows the 8-4-4-12 method. "

"The key difference between bluetooth communication and TCP/UDP communication is that the Bluetooth server has an SDP Service, that is, the Service Discovery Service, which allows the client to query what services the Bluetooth server can provide. "

"Generally, the bluetooth module for embedded devices provides the Serial Port service, SPP for short, and then provides a UART to connect to the embedded device," As shown in.

 

Daisi:

"As a result, the basic process of communication between the mobile App and the embedded device is as follows (assuming that the bluetooth module has been paired with the mobile phone ):

Step 1: select the bluetooth module for communication

Step 2: query the services of the bluetooth module. The service list of the paired bluetooth device is usually cached on the mobile phone.

Step 3: If the SPP service exists, congratulations!

Step 4: Use the SPP service UUID to create a client Socket.

Step 5: Use a client Socket to connect to the bluetooth module.

Step 6: Call SendData () of the client Socket to send data to the bluetooth module. The bluetooth module forwards the received data to the embedded device through UART.

Step 7: If the embedded device sends data to the bluetooth module, the bluetooth module forwards the data to the mobile phone and the mobile phone caches the data. The App can call the client Socket ReceiveData () to receive the data. "

Daisi:

"This is how Bluetooth communication works. "

Gao Fei dog:

"Daisy, you have made a plain speech. I must invite you and kiss you tonight!

Daisi:

"You will know what you think! "

Gao Fei dog:

"How to implement it? "

Daisi:

"The specific implementation method is also very simple. We only need to select Rad Studio as the development tool and write a set of source code in C ++ to generate an App for running on Android and iOS. Isn't it cool? "

"Rad Studio is not mentioned here. The GUI Design is also very simple. We focus on how to send and receive data through Bluetooth. "

 

Use the Bluetooth SPP service to implement communication between mobile phones and embedded devices

Note 1: This article aims to clarify the principles of Bluetooth communication and only list the Code. If you need the complete code, contact me.

NOTE 2: Make sure that Bluetooth is enabled for the mobile phone.

NOTE 3: Make sure that the bluetooth module is in the paired Device List of the mobile phone.

// Obtain and save the Bluetooth manager object

Tbluw.thmanager * BtManager = tbluw.thmanager: Current;

 

// Obtain and save the Bluetooth adapter object

BtAdapter = BtManager-> CurrentAdapter;

 

// Obtain the list of paired Bluetooth devices

Tbluw.thdevicelist * inclureddevices = BtManager-> getinclureddevices ();

 

// Display the bluetooth device list in the cbw.reddevices combo box (Code omitted)

 

// Obtain the selected bluetooth device object in the combo box

Tblustmthdevice * SelDevice = repeated reddevices-> Items [cb1_reddevices-> ItemIndex];

 

// Obtain the service list of the bluetooth device

Tbluw.thservicelist * ListServices = SelDevice-> GetServices ();

 

// Check whether the SPP service is supported in the service list (Code omitted)

 

// If the SPP service is supported

 

// Use the SPP service UUID to create a client Socket.

Tblustmthsocket * BtSocket = SelDevice-> CreateClientSocket (StringToGUID (SPP_GUI), false );

 

// Connect the bluetooth module with a client Socket.

BtSocket-> Connect ();

 

// Call SendData () of the client Socket to send data to the bluetooth module. The bluetooth module forwards the received data to the embedded device through UART.

BtSocket-> SendData (DataArray); // DataArray is an array

 

// If the embedded device sends data to the bluetooth module, the bluetooth module forwards the data to the mobile phone and the mobile phone caches the data. The App can call the client Socket ReceiveData () to receive the data. "

BtSocket-> ReceiveData (50); // receives data, waiting for 50 ms

 

Daisi:

"The above is the basic process for sending and receiving data between the mobile App and the bluetooth module and its implementation. "

Gao Fei dog:

"Excuse me, you have spoken too deeply! Ah, it's almost time to eat. I 'd like to invite you to have a meal. Thank you! "

Daisi:

"Someone has asked me to have dinner, so you can go back and try again. If you have any questions, Call me at any time. "

Related Article

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.