Android Bluetooth Serial program development

Source: Internet
Author: User



This paper mainly introduces the development of Bluetooth serial host computer for Android.

First, frame definition

Android client in accordance with a certain data frame format through the Bluetooth serial port to connect to the MCU Bluetooth slave, MCU received data, according to the definition of frame format, receive data frames, and parse the data frame, to 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



The sum of the frame checksum is the sum of all the data from the beginning of the frame to the frame check and the 256 remainder. That is sum%256.

Pitch roll Yaw data 16 bits, composed of high and low 8 bits. When the MCU receives the data, it needs to be re-synthesized into a 16-bit data.

0x5A: Frame Header

14: Pin length

0xa1: Frame function, indicating that this frame data is pitch yaw roll content.

Pitch_set_h:pitch 8-bit high

Pitch_set_l:pitch Low 8-bit

...

...

Frame check: (frame length + frame function + data)%256

0XA5: End of frame.

(Note: The frame is so defined, but when actually writing the receiver, I use the frame tail 0xa5 as the frame header of the frame data.) So the frame head becomes 0xa5 0x5A. There is no end of frame at this time)

Here is the program that the Android client sends byte information:

<span style= "White-space:pre" ></span>b[0] = (byte) 0xa5;  Frame head         b[1] = (byte);     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);  Checksum             b[14] = (byte) 0x5A;     End of frame

1.2MCU Receiving frame data

Because the serial port is to send data in a single byte format, so the MCU receives the data, in accordance with the definition of the frame format sent by the client, to find the beginning and end of a frame of data, to confirm that a frame of data received without error, then the data in the resolution. Use one to sign (state machine) The receive state for a burst of data.

The DSP2808 receiver program is written as follows:

Defining 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 ()//Receive data {//Receive program Sci_data in an interrupted manner.        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)//Judging the end of frame if the end of 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 is 13 bytes of data from frame length to frame check {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&LT;12)//Fetch CHECKSUM {sci_data.sum + = Sci_data.rx_buf[sci_ Data.buf_index];} sci_data.buf_index++;} if (Sci_data.buf_index >=) {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;}}

This allows the DSP2808 to receive data sent from the client. Saved in Sci_data.pitchset, Sci_data.rollset, sci_data.yawset three variables.


Second, the Android client development process

Android Bluetooth Serial program state machine






Android Bluetooth Serial program development

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.