Android hardware development - Bluetooth technology

foreword

This article mainly describes the Android hardware development Bluetooth related technologies, including:

  • Introduction to Bluetooth
  • Turn bluetooth devices on and off
  • Search for bluetooth devices

Bluetooth technology

Introduction to Bluetooth

Bluetooth (Bluetooth) is a short-range wireless communication technology standard. The name comes from the 10th century Danish king Harald Blatand, whose English name is Harold Bluetooth. After discussions organized by the wireless industry association, it was decided that the wireless technology could not be better named after King Blatand, who unified Norway, Sweden and Denmark, just as the technology will be unified The same goes for wireless communications. So far, the name of Bluetooth has been set.

The Bluetooth protocol is divided into 4 layers, namely the core protocol layer, the cable replacement protocol layer, the telephone control protocol layer and the adopted other protocol layers. The most important of these 4 protocols is the core protocol. The core protocol of Bluetooth includes four parts: baseband, link management, logical link control and adaptation protocol. Among them, link management (LMP) is responsible for the establishment of connection between Bluetooth components. The 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.

Turn bluetooth devices on and off

The first way to turn on bluetooth

The first way to turn on Bluetooth will pop up a dialog box, waiting for the user to confirm

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);

The second way to turn on bluetooth

Permissions must be set

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter()
adapter.enable();
adapter.disable();

Bluetooth data transmission

  • Bluetooth data transmission
  • Bluetooth UUID

Bluetooth data transmission

Transferring data over Bluetooth is similar to Socket. Use Socket and ServerSocket in the network to control the data reading and writing of the client and server. The Bluetooth communication is also completed by the client and the server Socket. The Bluetooth client socket is a BluetoothSocket, and the Bluetooth server socket is a BluetoothServerSocket. Both classes are in the android.bluetooth package.

Whether it is a BluetoothSocket or a BluetoothServerSocket, a UUID (Universally Unique Identifier) ​​is required. The format is as follows:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
The format of the UUID is divided into 5 segments, of which the number of characters in the middle 3 segments is the same , both are 4, the first segment is 8 characters, and the last segment is 12 characters. So the UUID is actually a string of 8-4-4-4-12.
The UUID is equivalent to the port of the Socket, and the Bluetooth address is equivalent to the IP of the Socket.

Bluetooth UUID

Two Bluetooth devices need to use the same UUID to connect. But many readers may find that there are many models of mobile phones (possibly non-Android mobile phones) that use different programs to communicate using Bluetooth. On the surface, it's almost impossible to use the same UUID between them.

In fact, UUIDs, like TCP ports, also have some default values. For example, a service that emulates 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

example

Reference code:  BluetoothSearch
renderings:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325358954&siteId=291194637