Android – Bluetooth technology enables you to realize data transmission between terminals

Bluetooth technology has many uses in intelligent hardware. Today, I'll share the usage methods and skills of Bluetooth in Android system, and realize the data transmission between two terminals.

Bluetooth is a short-range wireless communication technology standard. Bluetooth protocol is divided into four layers, namely core protocol layer, cable substitution protocol layer, telephone control protocol layer and other adopted protocol layers.

The most important of the four protocols is the core protocol. The core protocol of Bluetooth includes baseband, link management, logical link control and adaptation protocol. Link management (LMP) is responsible for establishing the connection between Bluetooth components. Logical link control and Adaptation Protocol (L2CAP) is located on the baseband protocol layer and belongs to the data link layer. It is an adaptation protocol that shields the baseband protocol for high-level transmission and application layer protocols.

1. Turn Bluetooth on and off

The first method is relatively simple. Directly call the system dialog box to start Bluetooth:

Add the required permissions to the androidmanifest file, and dynamic authorization is not required for higher versions:

If you don't want users to see this dialog box, we can also choose the second method to turn on Bluetooth silently.

The second method is to open it silently, and there will be no dialog box for method 1:

Add the required permissions to the androidmanifest file:

Since the permissions required by Bluetooth include dangerous permissions, we need to carry out dynamic authorization processing in Java code:

Next, we can turn on Bluetooth silently:

Let's take a look at how to search Bluetooth devices through code.

2. Search Bluetooth device through code

Search is divided into active search and passive search.

We started active search:

(1) Create a bluetoothadapter object

(2) Let's get and display the list of paired bluetooth devices first

(3) Next we define the broadcast receiver

The broadcast receiver of Bluetooth device is as follows:

(4) We create a button to search when the button is clicked. The button click event is as follows:

3. UUID of Bluetooth

Two Bluetooth devices need to use the same UUID when connecting. However, many readers may find that many models of mobile phones (possibly non Android phones) use different programs and can also use Bluetooth for communication. On the surface, it is almost impossible to use the same UUID between them.

The format of UUID is as follows:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

The format of UUID is divided into 5 segments, of which the number of characters in the middle 3 segments is the same, all of which are 4, the first segment is 8 characters, and the last segment is 12 characters. So UUID is actually an 8-4-4-4-12 string.

In fact, UUID, like TCP port, also has some default values. For example, a service that simulates Bluetooth as a serial port uses a standard UUID:

00001101-0000-1000-8000-00805F9B34FB

In addition, there are many standard UUIDs, such as the following two standard UUIDs:

Information synchronization service: 00001104-0000-1000-8000-00805f9b34fb

File transfer service: 00001106-0000-1000-8000-00805f9b34fb

4. Data transmission between Bluetooth terminals

Transmitting data through Bluetooth is similar to socket. In the network, socket and ServerSocket are used to control the data reading and writing of client and server. Bluetooth communication is also completed by the client and server socket. The Bluetooth client socket is bluetoothsocket, and the Bluetooth server socket is bluetoothserversocket. Both classes are in the Android. Bluetooth package.

Both Bluetooth socket and Bluetooth ServerSocket need a UUID (universal unique identifier). The UUID is equivalent to the port of the socket, and the Bluetooth address is equivalent to the IP of the socket.

We start to simulate a Bluetooth data transmission:

First, let's look at the client:

(1) Define global constant variables

(2) Initialize in oncreate method

The broadcast receiver of Bluetooth device is as follows:

(4) We create a button to search when the button is clicked. The button click event is as follows:

(5) Next, we set the click event of the list:

Next, let's look at the server:

The server uses another mobile phone to receive and display the information sent by the above mobile phone through Bluetooth.

(1) Define global constant variables:

(2) Define the server thread class:

(3) Initialize the thread class in the oncreate method and open it

Let's run the program and have a look at the renderings:

Click the "search Bluetooth device" button to search the Bluetooth information of another mobile phone. If we click the entry, the other mobile phone will show the following changes:

Toast pops up, which proves that our Bluetooth data has been transmitted.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>