Android Bluetooth serial port Program Development and android Program Development

Source: Internet
Author: User

Android Bluetooth serial port Program Development and android Program Development



This article mainly introduces the development of Bluetooth serial port PC for android.

I. frame definition

The android client sends data to the Bluetooth slave connected to the MCU according to a certain data frame format through the Bluetooth serial port. After the MCU receives the data, it receives and parses the data frame according to the definition of the frame format, obtain the required data.

The android client sends and receives data in the following frame format.




1.1 data frame content sent by the client

Sending frequency: 10Hz



The sum of frame verification is the sum of all data from the beginning of the frame length to the beginning of frame verification and the sum of 256. Sum % 256.

Pitch roll yaw each has 16 bits and consists of 8 bits. The MCU needs to combine the received data into a 16-bit data.

0x5A: frame Header

14: needle length

0xA1: frame function, indicating that the data in this frame is pitch yaw roll content.

Pitch_set_H: the pitch height is 8 bits

Pitch_set_L: pitch 8-bit low

...

...

Frame verification: (Frame Length + frame function + Data) % 256

0xA5: The end of the frame.

(Note: The frame is defined in this way, but when I write the receiving program, I use 0xA5 at the end of the frame as the frame header of a frame data. In this way, the frame header is changed to 0xA5 0x5A. At this time, no frame tail exists)

The Program for sending byte information from the android client is as follows:

<Span style = "white-space: pre"> </span> B [0] = (byte) 0XA5; // frame header B [1] = (byte) 14; // frame length B [2] = (byte) 0xA1; // frame function B [3] = (byte) (pitchSet> 8 ); // data B [4] = (byte) (pitchSet); B [5] = (byte) (rollSet> 8); B [6] = (byte) (rollSet); B [7] = (byte) (yawSet> 8); B [8] = (byte) (yawSet); B [9] = (byte) 0; B [10] = (byte) 0; B [11] = (byte) 0; B [12] = (byte) 0; int temp = 0; for (int j = 1; j <13; j ++) {temp = temp + B [j];} B [13] = (byte) (temp % 256 ); // verify B [14] = (byte) 0x5A; // end of the frame

1.2MCU receives frame data

Because the serial port sends data in one byte format, when receiving data, the MCU needs to locate the start and end of a frame of data according to the definition of the frame format sent by the client, after confirming that a frame of data is received correctly, parse the data. One is used to mark the receiving status of a data burst by the (state machine) receiver.

The DSP2808 receiving program is compiled as follows:

Define data types

typedef struct{int status;   //data statusint Rx_Data; //the receivced dataint data[15];//int rx_buf[13];int buf_index;int sum;int yawSet;int pitchSet;int rollSet;}Sci_Data;Sci_Data sci_data;

Interrupt void SCIB_RX_ISR () // receives data in interrupted mode {// The receiving program sci_data.Rx_Data = ScibRegs. SCIRXBUF. all; ScibRegs. SCIFFRX. bit. RXFFOVRCLR = 1; // clear overflow flagScibRegs. SCIFFRX. bit. RXFFINTCLR = 1; // clear the interrupt flagPieCtrlRegs. PIEACK. all | = PIEACK_GROUP9; // enable more interrupt from PIE group 9Decode_Frame (sci_data.Rx_Data); // data frame decoding} void Decode_Frame (int rx_data) {if (sci_data.status = 0 & rx_data = 0x5A) // determine the end of a frame. if the end of a frame is found, status = 1 {sci_data.status = 1 ;} else if (sci_data.status = 1 & rx_data = 0xA5) // The next character at the end of the frame is the frame header, status = 2 {sci_data.status = 2 ;} else if (sci_data.status = 2) // After the frame header, 13 bytes of data are transferred from the frame to the frame verification {if (sci_data.buf_index <13) {sci_data.rx_buf [sci_data.buf_index] = rx_data; // 0-12 to save data length function data... sumif (sci_data.buf_index <12) // calculate the checksum {sci_data.sum + = sci_data.rx_buf [sci_data.buf_index];} sci_data.buf_index ++;} if (sci_data.buf_index> = 13) {if (sci_data.rx_buf [12] = sci_data.sum % 256) {int j = 0; for (j = 0; j <10; j ++) {sci_data.data [j] = sci_data.rx_buf [j + 2]; // save data to the data buf} sci_data.pitchSet = sci_data.data [0] <8 | sci_data.data [1];} sci_data.buf_index = 0; sci_data.status = 0; sci_data.sum = 0 ;}} else {sci_data.buf_index = 0; sci_data.status = 0; sci_data.sum = 0 ;}}

In this way, DSP2808 receives the data sent from the client. Stored in sci_data.pitchSet, sci_data.rollSet, and sci_data.yawSet.


2. android client development process

Problem: Check the Bluetooth connection status

Android Bluetooth serial port program State Machine

In the program, broadcast is used to detect the Bluetooth connection status and determine the operation based on the Bluetooth connection status.

Turn on Bluetooth --- scan device --- Select device --- connect to normal --- send data ---






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.