Android Low Energy Bluetooth (BLE) Development (1)

This article mainly introduces BLE-related concepts. For code examples, please refer to "Android Bluetooth Low Energy (BLE) Development (2)"

1. Introduction

BLE refers to Bluetooth Low Energy (Bluetooth Low Energy), also known as Bluetooth 4.0. It's a variation of Bluetooth technology designed to consume less power, be portable, and allow low-speed data transmission over short wireless transmission distances. BLE technology has been widely used in smart home, health monitoring, Internet of Things and other scenarios.

2. Advantages

  • Low Power: One of the main features of BLE is low power communication. It employs a series of technologies and optimizations that allow devices to communicate with lower energy consumption, thereby extending battery life. This makes BLE ideal for devices that need to run for a long time and rely on battery power.

  • Fast connection and disconnection: BLE provides a mechanism for fast connection and disconnection, enabling devices to quickly establish a communication connection when needed, and then immediately disconnect to reduce loss.

  • Short-range communication: The transmission distance of BLE is usually short, which is suitable for short-range communication scenarios. This helps reduce interference and power consumption, and provides better privacy and security.

  • Simplified protocol stack: BLE adopts a simplified protocol stack, including L2CAP (Logical Link Control and Adaptation Protocol), ATT (Attribute Protocol) and GATT (General Attribute Profile). This simplifies the development and deployment of BLE devices, reducing development cost and complexity.

  • Advertising and scanning: BLE supports advertising and scanning mechanisms. The device can send data by broadcasting, and other devices can discover nearby BLE devices by scanning. This mechanism enables devices to communicate and exchange data without establishing a connection.

  • Multiple application scenarios: BLE is widely used in various fields, including Internet of Things (IoT), health monitoring, smart home, motion tracking, location services, etc. It provides low power consumption, fast connection and simple data exchange for these applications.

3. Related concepts

1. Broadcasting (Advertising)

BLE advertising (Advertising) is a mechanism for discovery and connection between devices. By broadcasting, BLE devices can send information to surrounding devices in a connectionless way so that other devices can discover them and establish a connection.

The main features and principles of BLE broadcasting are as follows:

  1. Connectionless broadcast: BLE broadcast is a connectionless communication method that does not need to establish an actual BLE connection. Devices can send information in the form of advertising packets without prior knowledge of the existence of surrounding devices.

  2. Advertising Packet: Advertising Packet is a special kind of data packet used to transmit data without connection. It contains data such as device identifiers, service information, and feature values, so that other devices can identify and discover it.

  3. Broadcast frequency and interval: BLE devices send broadcast packets at a certain broadcast frequency and interval. The broadcast frequency determines the speed at which broadcast packets are sent, and the broadcast interval determines the time interval between broadcast packets.

  4. Broadcast mode: BLE broadcast supports different broadcast modes, including connectable mode (Connectable), non-connectable mode (Non-connectable) and scannable mode (Scannable). The connectable mode allows other devices to communicate with the broadcasting device by establishing a connection. The non-connectable mode is only used for broadcasting information and does not accept connection requests. The scanable mode allows other devices to scan to the broadcasting device.

  5. The content of broadcast data: broadcast data can contain various information, such as device name, service UUID, manufacturer custom data, etc. This information helps other devices to identify and distinguish different BLE devices.

  6. Low power consumption design: BLE broadcast adopts a series of optimization measures to reduce power consumption and extend the battery life of the device. For example, the device can select an appropriate broadcast frequency and interval to balance broadcast effects and power consumption.

Through BLE broadcast, a device can announce its own existence, provide basic information and services, so that other devices can discover and communicate with it further. Broadcasting is the first step in BLE communication, which allows initial interaction and identification between devices before a connection is established.

In Android, you can use the BluetoothLeScanner class to scan and identify BLE devices. BLE device connection via BluetoothGatt class.

2. Service (service)

Service (Service) is a logical unit used to provide the functions and operations of BLE devices. Each BLE device can contain one or more services, and each service can contain one or more characteristic values ​​(Characteristic).

A BLE service consists of the following properties:

  1. UUID (Universal Unique Identifier): Each service has a UUID to uniquely identify it. The UUID can be a standard 16-bit or 128-bit UUID, or a custom UUID. Standard UUIDs are often used for common BLE functions such as heart rate monitoring, temperature sensors, etc. Custom UUIDs are used for specific applications and devices.

  2. Characteristics: A service contains one or more characteristics. The characteristic value is the basic unit of data stored on the BLE device, which is used to provide specific functions and operations. Characteristic UUIDs and properties describe their behavior and functionality.

  3. Descriptors: Services and characteristic values ​​can contain one or more descriptors. Descriptors provide metadata information about services and characteristic values, such as name, organization, authority, etc.

BLE services are defined and managed in the device's GATT (Generic Attribute Profile). GATT is a protocol framework for BLE communication, which is used to describe the service, characteristic value and descriptor of the device. It provides a standard way to organize and exchange data of BLE devices.

In Android, use the BluetoothGattService class to represent and operate BLE services. Through the BluetoothGattService object, you can get the UUID of the service, the list of included characteristic values, and the descriptors associated with the characteristic values.

Here are some commonly used service operation methods:

  • getUuid(): Get the UUID of the service.
  • getCharacteristics(): Get a list of characteristic values ​​associated with the service.
  • getIncludedServices(): Get a list of other services included in this service.
  • getCharacteristic(UUID uuid): Obtain a specific characteristic value object according to UUID.
  • addCharacteristic(BluetoothGattCharacteristic characteristic): Adds a characteristic value to the service.

BLE services provide a way to organize and manage the capabilities of BLE devices. By defining different services and characteristic values, devices can provide different functions and operations, such as sensor data, control commands, etc. When developing BLE, it is necessary to design and implement the corresponding service structure according to specific equipment and application requirements.

3. Characteristic

Characteristic (Characteristic) is the basic unit of data stored on BLE devices. Each feature value has a unique UUID (Universally Unique Identifier), which is used to identify the type and function of the feature value.

A BLE characteristic value consists of the following properties:

  1. UUID (Universal Unique Identifier): Each feature value has a UUID to uniquely identify it. The UUID can be a standard 16-bit or 128-bit UUID, or a custom UUID. Standard UUIDs are often used for common BLE functions such as heart rate monitoring, temperature sensors, etc. Custom UUIDs are used for specific applications and devices.

  2. Properties: A feature value has a set of properties that describe its behavior and functionality. Properties can be readable, writable, notification, indication, etc. Common properties include:

    • Readable (Read): The characteristic value can be read, allowing the remote device to read the value of the characteristic value.
    • Write: The characteristic value can be written, allowing the remote device to write new values ​​to the characteristic value.
    • Notify: The characteristic value can send a notification, when the value of the characteristic value changes, a notification will be sent to the connected device.
    • Indicate: Similar to notifications, but notifications have characteristics that cannot guarantee reliable delivery, while indications can ensure that notifications are received.
  3. Value: The data value stored by the feature value. Values ​​can be integers, floats, byte arrays, etc., depending on the purpose and data type of the characteristic value.

  4. Descriptor: A feature value can contain one or more descriptors that provide additional information about the feature value. Descriptors are optional and provide metadata about characteristic values ​​such as units, permissions, etc.

In Android, use the BluetoothGattCharacteristic class to represent and manipulate BLE characteristic values. Through the BluetoothGattCharacteristic object, you can read the value of the characteristic value, write a new value, enable notifications and indications, and other operations.

The following are some commonly used eigenvalue manipulation methods:

  • getValue(): Get the current value of the feature value.
  • setValue(byte[] value): Set the value of the feature value.
  • getProperties(): Get the attribute of the feature value.
  • getDescriptors(): Get a list of descriptors associated with feature values.
  • getUuid(): Get the UUID of the characteristic value.
  • getPermissions(): Permission to get characteristic values.

4. Descriptor

 A descriptor (Descriptor) is a data structure used to provide metadata information about BLE services (Service) and characteristic values ​​(Characteristic). Descriptors contain configuration parameters, permissions, and other attributes related to services and characteristic values.

Following are some important features and functions about BLE Descriptors:

  1. UUID (Universal Unique Identifier): Each descriptor has a UUID to uniquely identify it. The UUID can be a standard 16-bit or 128-bit UUID, or a custom UUID. Standard UUIDs are commonly used for common BLE functions such as Client Characteristic Configuration Descriptor and Report Reference Descriptor. Custom UUIDs are used for specific applications and devices.

  2. Attributes and permissions: A descriptor has a set of attributes and permissions that describe and control access to services and characteristic values. These attributes and permissions may indicate read and write permissions for descriptors, configuration of notifications and indications, data formats, etc.

  3. Associated with characteristic values: Descriptors are usually associated with characteristic values ​​and are used to provide additional information and configuration parameters of characteristic values. For example, a client configuration descriptor is used to enable or disable notification or indication of characteristic values.

  4. Purpose and function: The function and purpose of a BLE descriptor varies by device and application. Common descriptors include Client Configuration Descriptor, Report Reference Descriptor, User Descriptor, Environment Awareness Descriptor, etc. These descriptors can provide additional information related to characteristic values, such as units, ranges, permissions, etc.

In Android, the BLE descriptor is represented and operated through the BluetoothGattDescriptor class. Through the BluetoothGattDescriptor object, you can get the UUID of the descriptor, read and write the value of the descriptor, and associate the descriptor with the characteristic value.

The following are some commonly used descriptor manipulation methods:

  • getUuid(): Get the UUID of the descriptor.
  • getValue(): Read the value of the descriptor.
  • setValue(byte[] value): Set the value of the descriptor.
  • getPermissions(): Get the permission of the descriptor.
  • getCharacteristic(): Get the feature value object associated with the descriptor.

4. Equipment type

BLE devices can be divided into two types: central devices (Central) and peripheral devices (Peripheral).

  1. Central device (Central): It can be regarded as a client. The central device is usually a device with computing power and connectivity, such as a smartphone, tablet or computer. The central device is responsible for initiating BLE connection requests, scanning and discovering nearby peripheral devices, and exchanging data with peripheral devices.

  2. Peripheral: It can be regarded as a server. Peripherals are usually low-power sensors, sports equipment, health monitors and other devices. Peripheral devices provide some specific functions or sensor data and wait for connection requests and commands from the central device. After receiving the request from the central device, the peripheral device responds and provides data or performs a specific operation.

The BLE communication between the central device and the peripheral is based on the GATT (Generic Attribute Profile) protocol. The central device scans and discovers nearby peripheral devices, and performs connection and data exchange according to the GATT configuration of the peripheral devices. The central device can read and write the characteristic value (Characteristic) of the peripheral device to realize data transmission and control.

The role of a BLE device (Central or Peripheral) typically depends on the functionality and purpose of the device. For example, a smartphone often acts as a central device, establishing connections with multiple peripheral devices and collecting sensor data. Sensor devices, health monitors, etc. usually act as peripheral devices, waiting for connection requests from the central device and providing data.

It should be noted that the same device may play the role of both a central device and a peripheral device in different scenarios. For example, a smartphone can be used as a central device to connect to a sensor device, but it can also serve as a peripheral device to connect other devices.

5. Security Issues

In BLE communication, there are some security issues, especially in the process of data transmission and connection establishment. Here are some common BLE security issues:

  1. Security of the connection process: When establishing a BLE connection, there is a process of connection request and connection response. Without proper security measures, an attacker could conduct a man-in-the-middle attack or eavesdrop on connection requests and responses, thereby obtaining sensitive information or interfering with connection establishment.

  2. Security of data transmission: Data transmission in BLE communication may be threatened by eavesdropping, tampering and replay attacks. Unencrypted data transmission can lead to data breaches and information security issues.

  3. Authentication and authorization: One party to the BLE connection needs to ensure the identity and authority of the other device. Failure to perform proper authentication and authorization processes can result in rogue device access or unauthorized data access.

  4. Security of Bluetooth pairing: In some cases, BLE devices may need to go through the Bluetooth pairing process to establish a secure connection. Insecure authentication and key exchange during the pairing process can lead to pairing data being obtained by an attacker, thereby compromising the security of the connection.

In order to deal with these security issues, the following measures can be taken to enhance the security of BLE communication:

  1. Encrypted data transmission: Use secure encryption algorithms to encrypt sensitive data to ensure the confidentiality and integrity of data during transmission. Use symmetric encryption or public key encryption mechanisms to protect the security of data.

  2. Bluetooth pairing security: During the Bluetooth pairing process, strong passwords and long keys are used for pairing to ensure the security of authentication and key exchange. Avoid weak passwords and vulnerable pairing methods.

  3. Device authentication: During the establishment of BLE connection, device authentication is performed to ensure the security and legitimacy of the connection. Use mechanisms such as digital certificates or security tokens to verify the identity of the device.

  4. Secure connection parameters: Use appropriate BLE connection parameter settings, such as connection interval, connection timeout, etc., to improve the security and reliability of the connection.

  5. Secure broadcast and scan strategy: Limit the broadcast and scan range of BLE devices to prevent unauthorized device access. Use secure broadcast mode and broadcast data policies to avoid leaking sensitive information.

  6. Secure Firmware Updates: Regularly update your device's firmware and software to fix known security vulnerabilities and issues. Make sure to use the latest versions of firmware and software to reduce your potential attack surface.

By taking these security measures comprehensively, the security of BLE communication can be enhanced, and potential attacks and data leakage risks can be reduced.

Guess you like

Origin blog.csdn.net/gs12software/article/details/131226724
Recommended