Android Bluetooth instance (communication with MCU Bluetooth module)

Recently, after completing the design, I need to write a simple Bluetooth app for interaction. I also found a lot of information on the Internet, and finally got it. Let's share ^ ^ ^.

The programming of Bluetooth version 3.0 and below requires the use of UUID. UUID is a universal unique identifier, which is a standard for software construction and a part of the distributed computing environment organized and applied by the open source foundation. In Bluetooth 3.0 and the next version, UUID is used to uniquely identify a service, such as file transfer service, serial port service, printer service, etc., as follows:

#Bluetooth serial port service serialportserviceclass_ UUID = '{00001101-0000-1000-8000-00805F9B34FB}' LANAccessUsingPPPServiceClass_ UUID = '{00001102-0000-1000-8000-00805F9B34FB}'

#Dial up networking servicedialupnetworkingserviceclass_ UUID = '{00001103-0000-1000-8000-00805F9B34FB}'

#Information synchronization service irmcsyncserviceclass_ UUID = '{00001104-0000-1000-8000-00805F9B34FB}' SDP_ OBEXObjectPushServiceClass_ UUID = '{00001105-0000-1000-8000-00805F9B34FB}'

#File transfer service obexfiletransferserviceclass_ UUID = '{00001106-0000-1000-8000-00805F9B34FB}' IrMCSyncCommandServiceClass_ UUID = '{00001107-0000-1000-8000-00805F9B34FB}'

Bluetooth connection has master-slave devices, and those providing services can be regarded as slave devices. The master device accesses the slave device through UUID and provides services with the same UUID, so as to establish the customer service server (C / s) mode.

Android needs to obtain permission to use Bluetooth. The Bluetooth permission obtaining code is as follows:

Get the local Bluetooth adapter. If Bluetooth is not turned on, turn on the Bluetooth device:

Search for paired bluetooth devices and add them to the paired list:

Search for unpaired Bluetooth devices and add them to the unpaired list:

If it is the server side, you need to establish monitoring. Note that the monitoring is the UUID of a service. The server monitoring classes are as follows:

After the connection between the client and the server is successful, the connectedthread class needs to receive and send data:

So far, this is the general steps of Bluetooth development, which does not involve the establishment of connection class for Bluetooth client, but you can refer to ble and classic Bluetooth Android development.

The Bluetooth app needs to receive the data sent by the MCU through the Bluetooth module, and the Bluetooth app can also send data to the MCU for control. The page layout is as follows: one is the overall page, the other is the setting page, and the test mobile phone is Meilan note. Because Bi set up the intersection traffic light control system, all page layouts have four LED lights, representing the four traffic lights at the intersection respectively. Different colors (red / Green / yellow) will be displayed according to different time, and the countdown will be displayed. Finally, an overall diagram of the traffic light system will be given.

activity_ The main.xml file is as follows:

The mainactivity.java file is as follows:

menu_ The main.xml file is as follows:

The strings.xml file is as follows:

So far, the whole app has been developed and can be tested in person. If there are any errors, please comment and correct and talk about ^ ^ ^.

1. Ble and classic Bluetooth Android Development

  2、 http://developer.android.com/intl/zh-cn/guide/topics/connectivity/bluetooth.html

3. The first line of Android code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>