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.

Why is this an issue?

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.

How to fix it

Code examples

Noncompliant code example

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
            }
        }
    };
}

Compliant solution

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
            }
        }
    };
}

Noncompliant code example

public class BluetoothExample {
    private void startAdvertising() {
        AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
        settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY); // Noncompliant
        // Other settings configuration...
    }
}

Compliant solution

public class BluetoothExample {
    private void startAdvertising() {
        AdvertiseSettings.Builder settingsBuilder = new AdvertiseSettings.Builder();
        settingsBuilder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_POWER); // Compliant
        // Other settings configuration...
    }
}

Resources

Documentation