Bluetooth Classic vs. BLE (Bluetooth Low Energy)

What you need to know about BLE & Android BLE AP

- Generic Attribute Profile (GATT) is a general specification for sending and receiving short pieces of data known as “attributes” over a BLE link.
- The Bluetooth SIG (Bluetooth Special Interest Group) defines many profiles for BLE devices. A profile specifies how a device works in a particular application.
- Attribute Protocol (ATT) on top of which GATT is built. Each attribute is uniquely identified by a Universally Unique Identifier (UUID), a standardized 128-bit format for a string ID. The attributes transported by ATT are formatted as characteristics and services.
- Characteristic describes a device feature containing a single value and descriptors — an entity containing meaningful data that can typically be read or written.
- Descriptors are defined attributes that describe the characteristic that they are attached to.
- Service is a collection of characteristics.

How to set communication flow in an Android app using BLE technology in a few steps
Step #1: Configure permissions
Android apps require specific runtime permissions from users granted to enable Bluetooth connectivity. The configuration would depend on the Android version that the app is targeting.- Android 11 and older — they require location permission to be granted. It is also recommended to check whether the location services are turned on.
- Android 12 and new versions — they require new Bluetooth permissions to be granted.
Step #2: Scan devices
The phone has a central role in Bluetooth communication, so it must initiate the BLE scanning process to discover the nearby devices that advertise their availability for connection. Before scanning for devices, the app can configure appropriate scan filter options. For example, to adjust the scan interval, define a time interval to be notified about found results, set MAC addresses for searching for specific devices, etc. The scan results are received containing discovered BluetoothDevice objects. By analyzing the device object properties and identifiers, it can be determined whether there is a device that the app is searching for (e.g., by comparing MAC address, name, UUID, RSSI signal strength). After the target device is discovered nearby, the app should stop the scan process and proceed with a connection process.Step #3: Connect to the device
The app must establish a Bluetooth connection to start communicating with the found BLE device. BLE connection process is often referred to as Bonding or Pairing, but these terms are not the same. The difference is that pairing is the exchange of temporary encryption keys that enable bonding, which is the exchange of long-term encryption keys. The motivation behind creating a bond is to set up an encrypted communication channel between a BLE central and peripheral. Service discovery is essential after establishing a BLE connection with a device. Through Bluetooth GATT, the Android can access the services and characteristics that are present on the BLE device. That allows the app to explore the device’s capabilities. When establishing a connection, an Android app as a client can request a preferred ATT MTU (Maximum Transmission Unit), which stands for the maximum length of a data packet. Usually, a final MTU value will differ from the originally requested one because the request must be negotiated between Android and the BLE device. Another important configuration that can be set upon establishing a connection is a PHY. PHY (Physical Layer) is responsible for transmitting and receiving information over the air via radio waves. The application can choose to request either 1M or 2M PHY (Megabit – Mega symbols per second) to impact the throughput. The final value is negotiated with the BLE device.Operations
Once the connection to the device is established, there are 3 types of operations you can perform on a characteristic:- Write Command — it can be executed with or without a response acknowledgment by sending a byte array to the specified characteristic.
- Notifications — these are the primary mechanism that allows BLE devices to communicate with the Android device. Upon established connection, the app can enable receiving the notifications and subscribe to receive BLE notifications from a characteristic.
- Read Response — allows the app to obtain information from the BLE device (e.g., battery levels). After executing the read command, the app receives a byte array that contains the appropriate data.
Step #4: Configure communication protocol
It is necessary to have a defined communication protocol to enable clear Bluetooth communication between Android and BLE devices. The BLE binary communication protocol represents an API for data message flow. Therefore, these messages should be in an appropriate, predetermined format, where it is precisely defined which type of value can be found in which position. In addition, most BLE firmware utilizes little-endian byte order, so it is crucial to ensure parsing the bytes in the correct order. The verification mechanism should be encapsulated within the communication protocol to check that each sent/received message is valid according to the protocol. One of the examples is calculating the checksum on message values and setting it as a part of the message for verification, both on Android and BLE device’s firmware. To provide a robust scheme for avoiding framing errors, COBS (Consistent Overhead Byte Stuffing) encoding/decoding is often used on message data. And finally, to secure the message content, the protocol messages may require encryption, especially if an app is communicating with a medical device that deals with sensitive patient data. An Android app should implement a specific BLE data parser to support such BLE protocol. Also, it is necessary to implement support for sending and receiving messages (commands, responses). It’s important to keep in mind that the large data packets, which exceed the MTU, might split into multiple smaller chunks causing the application not to parse those chunks. This is why you should consider implementing an appropriate and thread-safe buffering mechanism, queues for incoming messages, and commands that are to be sent.Step #5: Manage connections
Now that you have successfully connected the device, how do you manage to maintain the connection? The Android application should provide a central point of access to information regarding the current status of the BLE connection. This point of access should contain some of the following options:- Status of connection to the BLE device
- Device discovery status
- Options to initiate and stop device discovery
- Options to connect and disconnect from a device
- Options to send and receive BLE messages
- Mechanism to handle connection or communication errors
