BluetoothGATT is a class to provide a functionality to enable communication with Bluetooth Smart or Smart Ready devices.
To connect to a remote peripheral device, a BluetoothGattCallback is used and a method BluetoothDevice#connectGatt is
used to get an instance of this class. GATT-capable devices can be discovered using the Bluetooth device discovery or BLE scan process.
Using high power consumption modes for Bluetooth operations can drain the device battery faster and may not be suitable for scenarios where power efficiency is crucial.
This rule identifies instances where high power consumption Bluetooth operations are used, specifically when requestConnectionPriority
or setAdvertiseMode methods are invoked with arguments other than those promoting low power consumption.
CONNECTION_PRIORITY_LOW_POWER for requestConnectionPriority method. ADVERTISE_MODE_LOW_POWER for setAdvertiseMode method to promote low power consumption.
public class BluetoothExample {
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// ...
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH); // Noncompliant
}
}
};
}
public class BluetoothExample {
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
// ...
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER); // Compliant
}
}
};
}
public class BluetoothExample {
private void startAdvertising() {
AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY); // Noncompliant
// Other settings configuration...
}
}
public class BluetoothExample {
private void startAdvertising() {
AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); // Compliant
// Other settings configuration...
}
}