rspec/rules/S6926/java/rule.adoc

95 lines
3.5 KiB
Plaintext

`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
* Use `CONNECTION_PRIORITY_LOW_POWER` for `requestConnectionPriority` method.
* Use `ADVERTISE_MODE_LOW_POWER` for `setAdvertiseMode` method to promote low power consumption.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=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_HIGH); // Noncompliant
}
}
};
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
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
[source,java,diff-id=2,diff-type=noncompliant]
----
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
[source,java,diff-id=2,diff-type=compliant]
----
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
* https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback[Android Developers - BluetoothGattCallback]
* https://developer.android.com/reference/android/bluetooth/BluetoothGatt[Android Developers - BluetoothGatt]
* https://developer.android.com/reference/android/bluetooth/BluetoothDevice[Android Developers - BluetoothDevice]
* https://developer.android.com/reference/android/bluetooth/le/AdvertiseSettings.Builder[Android Developers - AdvertiseSettings.Builder]