Generic Attribute Profile (GATT)
Profile generic specification for reading and writing small data of attribute classes over a BLE connection. All BLE application Profiles are now based on GATT.
Attribute Protocol (ATT)
GATT is based on the ATT Protocol.ATT is specifically optimized for BLE devices, specifically to use as little data as possible during transmission. Each attribute has a unique UUID, and attributes will be transmitted as CHARACTERISTICS AND SERVICES.
Characteristic
Characteristic can be understood as a datatype that consists of a value and from 0 to more than one description of sub-values (Descriptor).
Descriptor
A description of the Characteristic, e.g., range, unit of measure, etc.
Service
A collection of Characteristic. For example, a service called "Heart Rate Monitor" may contain multiple Characteristics, including one called "heart rate measurement".
Two roles and responsibilities:
There are two sets of roles for an Android device to interact with a BLE device:
Central vs. peripheral;
GATT server vs.
Central vs. peripheral:
The concepts of central and peripheral are specific to the BLE connection itself.
The central role is responsible for scan advertisement. and the peripheral role is responsible for make advertisement.
GATT server vs. GATT client:
The two roles depend on the way the communication between the two devices takes place after a successful BLE connection.
Example:
There is a BLE device with active tracking and a BLE-enabled Android device.The Android device supports the Central role, while the BLE device supports the peripheral role. To create a BLE connection, both roles need to be present, and a connection cannot be created if both roles are supported only in the Central role or both roles are supported only in the peripheral role.
When the connection is established, GATT data needs to be transferred between them. Who is the server and who is the client depends on the specific data transfer. For example, if the BLE device of the activity tracking needs to transmit sensor data to the Android device, the activity tracker naturally becomes the SERVER; while if the activity tracker needs to get updates from the Android device, the Android device may be more appropriate as the SERVER.
Three permissions and features:
Like classic Bluetooth, applications that use Bluetooth need to declare BLUETOOTH permissions, and if they need to scan the device or manipulate the Bluetooth settings, they also need BLUETOOTH_ADMIN permissions:
<uses-permission android :name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Besides the In addition to the Bluetooth permissions, if you need the BLE feature you need to declare the uses-feature:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/
When required is true, the app can only be installed on Android devices that support BLE; when required is false, it can be installed on any Android device, so you need to determine if the device supports the BLE feature when running the code:
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (! getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
Four, start Bluetooth:
Before you use Bluetooth BLE, you need to make sure that the Android device supports the BLE feature (when required is false), and also to need to confirm that Bluetooth is turned on.
If you find that BLE is not supported, you cannot use BLE-related features. If BLE is supported, but Bluetooth is not turned on, then Bluetooth needs to be turned on.
The steps to turn on Bluetooth:
1. Get BluetoothAdapter
BluetoothAdapter is required for all Bluetooth operations in Android, it corresponds to the Bluetooth module of the local Android device, and in the whole system BluetoothAdapter is the single instance. Once you get its instance, you can perform the relevant Bluetooth operations.
Get the BluetoothAdapter code example as follows:
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
( BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
Note: Here, we get the Bluetooth adapter via the getSystemService to get BluetoothManager, and then get BluetoothAdapter through BluetoothManager. BluetoothManager is supported on Android 4.3 and above (API level 18).
2. Determine whether Bluetooth is supported and turn on Bluetooth
After getting the BluetoothAdapter, you still need to determine whether Bluetooth is supported and whether Bluetooth is turned on.
If it's not on, you need to let the user turn on Bluetooth:
private BluetoothAdapter mBluetoothAdapter;
...
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}