From 1d7b396d137857dc8d0bf0f2153b6b01e7ffd6c8 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Mon, 16 Apr 2018 13:05:33 +0200 Subject: [PATCH] Moving battery level views to profile activities, part 1 --- .../android/nrftoolbox/bpm/BPMManager.java | 11 +-- .../android/nrftoolbox/cgms/CGMSManager.java | 17 ++-- .../android/nrftoolbox/csc/CSCActivity.java | 24 ++++++ .../android/nrftoolbox/csc/CSCManager.java | 80 +++++++++++++++++-- .../nrftoolbox/csc/CSCManagerCallbacks.java | 2 + .../android/nrftoolbox/csc/CSCService.java | 30 ++++++- .../nrftoolbox/gls/GlucoseManager.java | 13 +-- .../android/nrftoolbox/hrs/HRSManager.java | 11 +-- .../android/nrftoolbox/hts/HTSManager.java | 7 +- .../profile/BleProfileActivity.java | 21 ----- .../BleProfileExpandableListActivity.java | 15 ---- .../nrftoolbox/profile/BleProfileService.java | 18 +---- .../BleProfileServiceReadyActivity.java | 20 +---- .../BleMulticonnectProfileService.java | 7 +- .../nrftoolbox/proximity/DeviceAdapter.java | 14 ++-- .../proximity/ProximityManager.java | 43 +++++----- .../proximity/ProximityService.java | 12 +++ .../android/nrftoolbox/rsc/RSCManager.java | 7 +- .../nrftoolbox/template/TemplateManager.java | 13 +-- .../android/nrftoolbox/uart/UARTManager.java | 9 ++- 20 files changed, 222 insertions(+), 152 deletions(-) diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/bpm/BPMManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/bpm/BPMManager.java index 841b7c4a..f41c58b3 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/bpm/BPMManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/bpm/BPMManager.java @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Calendar; import java.util.Deque; @@ -74,7 +75,7 @@ public class BPMManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); if (mICPCharacteristic != null) requests.add(Request.newEnableNotificationsRequest(mICPCharacteristic)); @@ -83,7 +84,7 @@ public class BPMManager extends BleManager { } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { BluetoothGattService service = gatt.getService(BP_SERVICE_UUID); if (service != null) { mBPMCharacteristic = service.getCharacteristic(BPM_CHARACTERISTIC_UUID); @@ -93,7 +94,7 @@ public class BPMManager extends BleManager { } @Override - protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) { + protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { return mICPCharacteristic != null; } @@ -104,7 +105,7 @@ public class BPMManager extends BleManager { } @Override - protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // Intermediate Cuff Pressure characteristic read Logger.a(mLogSession, "\"" + IntermediateCuffPressureParser.parse(characteristic) + "\" received"); @@ -112,7 +113,7 @@ public class BPMManager extends BleManager { } @Override - protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // Blood Pressure Measurement characteristic read Logger.a(mLogSession, "\"" + BloodPressureMeasurementParser.parse(characteristic) + "\" received"); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSManager.java index 3a740fe9..81d8b68c 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSManager.java @@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import android.util.SparseArray; import java.util.Deque; @@ -111,7 +112,7 @@ public class CGMSManager extends BleManager { return managerInstance; } - public CGMSManager(Context context) { + public CGMSManager(final Context context) { super(context); } @@ -126,7 +127,7 @@ public class CGMSManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); requests.add(Request.newEnableNotificationsRequest(mCGMMeasurementCharacteristic)); if (mCGMOpsControlPointCharacteristic != null) { @@ -139,7 +140,7 @@ public class CGMSManager extends BleManager { } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(CGMS_UUID); if (service != null) { mCGMMeasurementCharacteristic = service.getCharacteristic(CGM_MEASUREMENT_UUID); @@ -150,7 +151,7 @@ public class CGMSManager extends BleManager { } @Override - protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) { + protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(CGMS_UUID); if (service != null) { mCGMOpsControlPointCharacteristic = service.getCharacteristic(CGM_OPS_CONTROL_POINT_UUID); @@ -159,7 +160,7 @@ public class CGMSManager extends BleManager { } @Override - public void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { } @Override @@ -170,7 +171,7 @@ public class CGMSManager extends BleManager { } @Override - protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { if (characteristic.getUuid().equals(RACP_UUID)) { Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" sent"); } else { // uuid == CGM_OPS_CONTROL_POINT_UUID @@ -179,7 +180,7 @@ public class CGMSManager extends BleManager { } @Override - public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + CGMMeasurementParser.parse(characteristic) + "\" received"); // CGM Measurement characteristic may have one or more CGM records @@ -201,7 +202,7 @@ public class CGMSManager extends BleManager { } @Override - protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { if (characteristic.getUuid().equals(RACP_UUID)) { Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" received"); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCActivity.java index 9e74295d..61ae99f0 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCActivity.java @@ -52,6 +52,7 @@ public class CSCActivity extends BleProfileServiceReadyActivity { /** - * Cycling Speed and Cadence service UUID + * Cycling Speed and Cadence service UUID. */ public final static UUID CYCLING_SPEED_AND_CADENCE_SERVICE_UUID = UUID.fromString("00001816-0000-1000-8000-00805f9b34fb"); /** - * Cycling Speed and Cadence Measurement characteristic UUID + * Cycling Speed and Cadence Measurement characteristic UUID. */ private static final UUID CSC_MEASUREMENT_CHARACTERISTIC_UUID = UUID.fromString("00002A5B-0000-1000-8000-00805f9b34fb"); + /** + * Battery Service UUID. + */ + private final static UUID BATTERY_SERVICE_UUID = UUID.fromString("0000180F-0000-1000-8000-00805f9b34fb"); + /** + * Battery Level characteristic UUID. + */ + private final static UUID BATTERY_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A19-0000-1000-8000-00805f9b34fb"); private final SharedPreferences preferences; private BluetoothGattCharacteristic mCSCMeasurementCharacteristic; + private BluetoothGattCharacteristic mBatteryLevelCharacteristic; CSCManager(final Context context) { super(context); @@ -63,18 +75,62 @@ public class CSCManager extends BleManager { return mGattCallback; } + public void readBatteryLevelCharacteristic() { + readCharacteristic(mBatteryLevelCharacteristic) + .with(new BatteryLevelCallback() { + @Override + public void onBatteryValueChanged(final int batteryLevel) { + mCallbacks.onBatteryLevelChanged(getBluetoothDevice(), batteryLevel); + } + + @Override + public void onInvalidDataReceived(final @NonNull Data data) { + log(LogContract.Log.Level.WARNING, "Invalid Battery Level data received: " + data); + } + }) + .fail(status -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found")); + } + + public void enableBatteryLevelCharacteristicNotifications() { + // If the Battery Level characteristic is null, the request will be ignored + enableNotifications(mBatteryLevelCharacteristic) + .with(new BatteryLevelCallback() { + @Override + public void onBatteryValueChanged(final int batteryLevel) { + mCallbacks.onBatteryLevelChanged(getBluetoothDevice(), batteryLevel); + } + + @Override + public void onInvalidDataReceived(final @NonNull Data data) { + log(LogContract.Log.Level.WARNING, "Invalid Battery Level data received: " + data); + } + }) + .done(() -> log(LogContract.Log.Level.INFO, "Battery Level notifications enabled")) + .fail(status -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found")); + } + + public void disableBatteryLevelCharacteristicNotifications() { + disableNotifications(mBatteryLevelCharacteristic) + .done(() -> log(LogContract.Log.Level.INFO, "Battery Level notifications disabled")); + } + /** * BluetoothGatt callbacks for connection/disconnection, service discovery, receiving indication, etc */ private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected void initialize(final BluetoothDevice device) { + protected void initialize(@NonNull final BluetoothDevice device) { + enableBatteryLevelCharacteristicNotifications(); + + // CSC characteristic is required enableNotifications(mCSCMeasurementCharacteristic) .with(new CyclingSpeedAndCadenceCallback() { @Override - public void onDataReceived(@NonNull final Data data) { + public void onDataReceived(final @NonNull Data data) { log(LogContract.Log.Level.APPLICATION, "\"" + CSCMeasurementParser.parse(data) + "\" received"); + + // Pass through received data super.onDataReceived(data); } @@ -94,14 +150,14 @@ public class CSCManager extends BleManager { } @Override - public void onInvalidDataReceived(@NonNull final Data data) { - log(LogContract.Log.Level.WARNING, "Invalid data received: " + data); + public void onInvalidDataReceived(final @NonNull Data data) { + log(LogContract.Log.Level.WARNING, "Invalid CSC Measurement data received: " + data); } }); } @Override - public boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(CYCLING_SPEED_AND_CADENCE_SERVICE_UUID); if (service != null) { mCSCMeasurementCharacteristic = service.getCharacteristic(CSC_MEASUREMENT_CHARACTERISTIC_UUID); @@ -109,9 +165,19 @@ public class CSCManager extends BleManager { return mCSCMeasurementCharacteristic != null; } + @Override + protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { + final BluetoothGattService service = gatt.getService(BATTERY_SERVICE_UUID); + if (service != null) { + mBatteryLevelCharacteristic = service.getCharacteristic(BATTERY_LEVEL_CHARACTERISTIC_UUID); + } + return mBatteryLevelCharacteristic != null; + } + @Override protected void onDeviceDisconnected() { mCSCMeasurementCharacteristic = null; + mBatteryLevelCharacteristic = null; } }; } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCManagerCallbacks.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCManagerCallbacks.java index 18e25c98..c60d21ed 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCManagerCallbacks.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCManagerCallbacks.java @@ -29,4 +29,6 @@ public interface CSCManagerCallbacks extends BleManagerCallbacks { void onDistanceChanged(final BluetoothDevice device, final float totalDistance, final float distance, final float speed); void onCrankDataChanged(final BluetoothDevice device, final float crankCadence, final float gearRatio); + + void onBatteryLevelChanged(final BluetoothDevice device, final int batteryLevel); } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCService.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCService.java index df633a77..3872b007 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCService.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/csc/CSCService.java @@ -45,16 +45,20 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks private static final String TAG = "CSCService"; public static final String BROADCAST_WHEEL_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_WHEEL_DATA"; + /** Speed in meters per second. */ public static final String EXTRA_SPEED = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_SPEED"; - /** Distance in meters */ + /** Distance in meters. */ public static final String EXTRA_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_DISTANCE"; - /** Total distance in meters */ + /** Total distance in meters. */ public static final String EXTRA_TOTAL_DISTANCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_TOTAL_DISTANCE"; public static final String BROADCAST_CRANK_DATA = "no.nordicsemi.android.nrftoolbox.csc.BROADCAST_CRANK_DATA"; public static final String EXTRA_GEAR_RATIO = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_GEAR_RATIO"; public static final String EXTRA_CADENCE = "no.nordicsemi.android.nrftoolbox.csc.EXTRA_CADENCE"; + public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL"; + public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL"; + private static final String ACTION_DISCONNECT = "no.nordicsemi.android.nrftoolbox.csc.ACTION_DISCONNECT"; private final static int NOTIFICATION_ID = 200; @@ -62,6 +66,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks private final static int DISCONNECT_REQ = 1; private final LocalBinder mBinder = new CSCBinder(); + private CSCManager mManager; /** * This local binder is an interface for the bonded activity to operate with the RSC sensor @@ -77,7 +82,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks @Override protected BleManager initializeManager() { - return new CSCManager(this); + return mManager = new CSCManager(this); } @Override @@ -102,10 +107,21 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks protected void onRebind() { // when the activity rebinds to the service, remove the notification cancelNotification(); + + if (isConnected()) { + // This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property). + // If the Battery Level characteristic has only the NOTIFY property, it will only try to enable notifications. + mManager.readBatteryLevelCharacteristic(); + } } @Override protected void onUnbind() { + // When we are connected, but the application is not open, we are not really interested in battery level notifications. + // But we will still be receiving other values, if enabled. + if (isConnected()) + mManager.disableBatteryLevelCharacteristicNotifications(); + // when the activity closes we need to show the notification that user is connected to the sensor createNotification(R.string.csc_notification_connected_message, 0); } @@ -129,6 +145,14 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast); } + @Override + public void onBatteryLevelChanged(final BluetoothDevice device, final int value) { + final Intent broadcast = new Intent(BROADCAST_BATTERY_LEVEL); + broadcast.putExtra(EXTRA_DEVICE, getBluetoothDevice()); + broadcast.putExtra(EXTRA_BATTERY_LEVEL, value); + LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast); + } + /** * Creates the notification * diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/gls/GlucoseManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/gls/GlucoseManager.java index 1c573fd7..265495b9 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/gls/GlucoseManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/gls/GlucoseManager.java @@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; import android.os.Handler; +import android.support.annotation.NonNull; import android.util.SparseArray; import java.util.Calendar; @@ -128,7 +129,7 @@ public class GlucoseManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); requests.add(Request.newEnableNotificationsRequest(mGlucoseMeasurementCharacteristic)); if (mGlucoseMeasurementContextCharacteristic != null) { @@ -153,7 +154,7 @@ public class GlucoseManager extends BleManager { } @Override - public boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(GLS_SERVICE_UUID); if (service != null) { mGlucoseMeasurementCharacteristic = service.getCharacteristic(GM_CHARACTERISTIC); @@ -164,7 +165,7 @@ public class GlucoseManager extends BleManager { } @Override - protected boolean isOptionalServiceSupported(BluetoothGatt gatt) { + protected boolean isOptionalServiceSupported(@NonNull BluetoothGatt gatt) { return mGlucoseMeasurementContextCharacteristic != null; } @@ -176,12 +177,12 @@ public class GlucoseManager extends BleManager { } @Override - protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" sent"); } @Override - public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { final UUID uuid = characteristic.getUuid(); if (GM_CHARACTERISTIC.equals(uuid)) { @@ -329,7 +330,7 @@ public class GlucoseManager extends BleManager { } @Override - protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + RecordAccessControlPointParser.parse(characteristic) + "\" received"); // Record Access Control Point characteristic diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/hrs/HRSManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/hrs/HRSManager.java index 31fe9bbf..85f289fd 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/hrs/HRSManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/hrs/HRSManager.java @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Deque; import java.util.LinkedList; @@ -75,7 +76,7 @@ public class HRSManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); if (mHRLocationCharacteristic != null) requests.add(Request.newReadRequest(mHRLocationCharacteristic)); @@ -84,7 +85,7 @@ public class HRSManager extends BleManager { } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID); if (service != null) { mHRCharacteristic = service.getCharacteristic(HR_CHARACTERISTIC_UUID); @@ -93,7 +94,7 @@ public class HRSManager extends BleManager { } @Override - protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) { + protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(HR_SERVICE_UUID); if (service != null) { mHRLocationCharacteristic = service.getCharacteristic(HR_SENSOR_LOCATION_CHARACTERISTIC_UUID); @@ -102,7 +103,7 @@ public class HRSManager extends BleManager { } @Override - public void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + BodySensorLocationParser.parse(characteristic) + "\" received"); final String sensorPosition = getBodySensorPosition(characteristic.getValue()[0]); @@ -117,7 +118,7 @@ public class HRSManager extends BleManager { } @Override - public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + HeartRateMeasurementParser.parse(characteristic) + "\" received"); int hrValue; diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/hts/HTSManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/hts/HTSManager.java index 38615376..9dc838ad 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/hts/HTSManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/hts/HTSManager.java @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Deque; import java.util.LinkedList; @@ -72,14 +73,14 @@ public class HTSManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); requests.add(Request.newEnableIndicationsRequest(mHTCharacteristic)); return requests; } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(HT_SERVICE_UUID); if (service != null) { mHTCharacteristic = service.getCharacteristic(HT_MEASUREMENT_CHARACTERISTIC_UUID); @@ -93,7 +94,7 @@ public class HTSManager extends BleManager { } @Override - public void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + TemperatureMeasurementParser.parse(characteristic) + "\" received"); try { diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java index 30def333..f4b617e4 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileActivity.java @@ -61,7 +61,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl private BleManager mBleManager; private TextView mDeviceNameView; - private TextView mBatteryLevelView; private Button mConnectButton; private ILogSession mLogSession; @@ -131,7 +130,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl getSupportActionBar().setDisplayHomeAsUpEnabled(true); mConnectButton = findViewById(R.id.action_connect); mDeviceNameView = findViewById(R.id.device_name); - mBatteryLevelView = findViewById(R.id.battery); } @Override @@ -274,17 +272,12 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl runOnUiThread(() -> { mConnectButton.setText(R.string.action_connect); mDeviceNameView.setText(getDefaultDeviceName()); - mBatteryLevelView.setText(R.string.not_available); }); } @Override public void onLinklossOccur(final BluetoothDevice device) { mDeviceConnected = false; - runOnUiThread(() -> { - if (mBatteryLevelView != null) - mBatteryLevelView.setText(R.string.not_available); - }); } @Override @@ -312,20 +305,6 @@ public abstract class BleProfileActivity extends AppCompatActivity implements Bl showToast(R.string.bonding_failed); } - @Override - public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) { - // Yes, we want battery level updates - return true; - } - - @Override - public void onBatteryValueReceived(final BluetoothDevice device, final int value) { - runOnUiThread(() -> { - if (mBatteryLevelView != null) - mBatteryLevelView.setText(getString(R.string.battery, value)); - }); - } - @Override public void onError(final BluetoothDevice device, final String message, final int errorCode) { DebugLogger.e(TAG, "Error occurred: " + message + ", error code: " + errorCode); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java index 47a0d2ee..36ae618d 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileExpandableListActivity.java @@ -61,7 +61,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct private BleManager mBleManager; private TextView mDeviceNameView; - private TextView mBatteryLevelView; private Button mConnectButton; private ILogSession mLogSession; @@ -131,7 +130,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct getSupportActionBar().setDisplayHomeAsUpEnabled(true); mConnectButton = findViewById(R.id.action_connect); mDeviceNameView = findViewById(R.id.device_name); - mBatteryLevelView = findViewById(R.id.battery); } @Override @@ -274,17 +272,12 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct runOnUiThread(() -> { mConnectButton.setText(R.string.action_connect); mDeviceNameView.setText(getDefaultDeviceName()); - mBatteryLevelView.setText(R.string.not_available); }); } @Override public void onLinklossOccur(final BluetoothDevice device) { mDeviceConnected = false; - runOnUiThread(() -> { - if (mBatteryLevelView != null) - mBatteryLevelView.setText(R.string.not_available); - }); } @Override @@ -318,14 +311,6 @@ public abstract class BleProfileExpandableListActivity extends ExpandableListAct return true; } - @Override - public void onBatteryValueReceived(final BluetoothDevice device, final int value) { - runOnUiThread(() -> { - if (mBatteryLevelView != null) - mBatteryLevelView.setText(getString(R.string.battery, value)); - }); - } - @Override public void onError(final BluetoothDevice device, final String message, final int errorCode) { DebugLogger.e(TAG, "Error occurred: " + message + ", error code: " + errorCode); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java index 196a3efd..84025daa 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileService.java @@ -53,6 +53,7 @@ public abstract class BleProfileService extends Service implements BleManagerCal public static final String BROADCAST_SERVICES_DISCOVERED = "no.nordicsemi.android.nrftoolbox.BROADCAST_SERVICES_DISCOVERED"; public static final String BROADCAST_DEVICE_READY = "no.nordicsemi.android.nrftoolbox.DEVICE_READY"; public static final String BROADCAST_BOND_STATE = "no.nordicsemi.android.nrftoolbox.BROADCAST_BOND_STATE"; + @Deprecated public static final String BROADCAST_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.BROADCAST_BATTERY_LEVEL"; public static final String BROADCAST_ERROR = "no.nordicsemi.android.nrftoolbox.BROADCAST_ERROR"; @@ -66,6 +67,7 @@ public abstract class BleProfileService extends Service implements BleManagerCal public static final String EXTRA_BOND_STATE = "no.nordicsemi.android.nrftoolbox.EXTRA_BOND_STATE"; public static final String EXTRA_SERVICE_PRIMARY = "no.nordicsemi.android.nrftoolbox.EXTRA_SERVICE_PRIMARY"; public static final String EXTRA_SERVICE_SECONDARY = "no.nordicsemi.android.nrftoolbox.EXTRA_SERVICE_SECONDARY"; + @Deprecated public static final String EXTRA_BATTERY_LEVEL = "no.nordicsemi.android.nrftoolbox.EXTRA_BATTERY_LEVEL"; public static final String EXTRA_ERROR_MESSAGE = "no.nordicsemi.android.nrftoolbox.EXTRA_ERROR_MESSAGE"; public static final String EXTRA_ERROR_CODE = "no.nordicsemi.android.nrftoolbox.EXTRA_ERROR_CODE"; @@ -242,12 +244,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal if (!mActivityIsChangingConfiguration) onRebind(); - - if (!mActivityIsChangingConfiguration && mBleManager.isConnected()) { - // This method will read the Battery Level value, if possible and then try to enable battery notifications (if it has NOTIFY property). - // If the Battery Level characteristic has only the NOTIFY property, it will only try to enable notifications. - mBleManager.readBatteryLevel(); - } } /** @@ -266,10 +262,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal if (!mActivityIsChangingConfiguration) onUnbind(); - // When we are connected, but the application is not open, we are not really interested in battery level notifications. But we will still be receiving other values, if enabled. - if (!mActivityIsChangingConfiguration && mBleManager.isConnected()) - mBleManager.disableBatteryLevelNotifications(); - // We want the onRebind method be called if anything else binds to it again return true; } @@ -391,12 +383,6 @@ public abstract class BleProfileService extends Service implements BleManagerCal // empty default implementation } - @Override - public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) { - // By default the Battery Level notifications will be enabled only the activity is bound. - return mBinded; - } - @Override public void onDeviceConnecting(final BluetoothDevice device) { final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java index 890c03ac..4a266470 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/profile/BleProfileServiceReadyActivity.java @@ -81,7 +81,6 @@ public abstract class BleProfileServiceReadyActivity manager = mBleManagers.get(device); @@ -415,12 +416,6 @@ public abstract class BleMulticonnectProfileService extends Service implements B } } - @Override - public boolean shouldEnableBatteryLevelNotifications(final BluetoothDevice device) { - // By default the Battery Level notifications will be enabled only the activity is bound. - return mBinded; - } - @Override public void onDeviceConnecting(final BluetoothDevice device) { final Intent broadcast = new Intent(BROADCAST_CONNECTION_STATE); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/DeviceAdapter.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/DeviceAdapter.java index 96dec28a..66d5f080 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/DeviceAdapter.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/DeviceAdapter.java @@ -24,6 +24,7 @@ package no.nordicsemi.android.nrftoolbox.proximity; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; +import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.view.LayoutInflater; @@ -40,19 +41,20 @@ public class DeviceAdapter extends RecyclerView.Adapter mDevices; - public DeviceAdapter(final ProximityService.ProximityBinder binder) { + DeviceAdapter(final ProximityService.ProximityBinder binder) { mService = binder; mDevices = mService.getManagedDevices(); } + @NonNull @Override - public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) { + public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) { final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_feature_proximity_item, parent, false); return new ViewHolder(view); } @Override - public void onBindViewHolder(final ViewHolder holder, final int position) { + public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) { holder.bind(mDevices.get(position)); } @@ -88,13 +90,13 @@ public class DeviceAdapter extends RecyclerView.Adapter= 0) { batteryView.getCompoundDrawables()[0 /*left*/].setLevel(batteryValue); batteryView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityManager.java index 2cd6bb8d..3ed5a23b 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityManager.java @@ -21,10 +21,12 @@ */ package no.nordicsemi.android.nrftoolbox.proximity; +import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Deque; import java.util.LinkedList; @@ -32,6 +34,7 @@ import java.util.UUID; import no.nordicsemi.android.ble.BleManager; import no.nordicsemi.android.ble.Request; +import no.nordicsemi.android.log.LogContract; import no.nordicsemi.android.log.Logger; import no.nordicsemi.android.nrftoolbox.parser.AlertLevelParser; import no.nordicsemi.android.nrftoolbox.utility.DebugLogger; @@ -52,6 +55,7 @@ public class ProximityManager extends BleManager { private BluetoothGattCharacteristic mAlertLevelCharacteristic, mLinklossCharacteristic; private boolean mAlertOn; + private int mBatteryLevel; public ProximityManager(final Context context) { super(context); @@ -73,14 +77,12 @@ public class ProximityManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { - final LinkedList requests = new LinkedList<>(); - requests.add(Request.newWriteRequest(mLinklossCharacteristic, HIGH_ALERT)); - return requests; + protected void initialize(@NonNull final BluetoothDevice device) { + writeCharacteristic(mLinklossCharacteristic, HIGH_ALERT); } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService llService = gatt.getService(LINKLOSS_SERVICE_UUID); if (llService != null) { mLinklossCharacteristic = llService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); @@ -89,7 +91,7 @@ public class ProximityManager extends BleManager { } @Override - protected boolean isOptionalServiceSupported(final BluetoothGatt gatt) { + protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService iaService = gatt.getService(IMMEDIATE_ALERT_SERVICE_UUID); if (iaService != null) { mAlertLevelCharacteristic = iaService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); @@ -104,11 +106,6 @@ public class ProximityManager extends BleManager { // Reset the alert flag mAlertOn = false; } - - @Override - protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { - Logger.a(mLogSession, "\"" + AlertLevelParser.parse(characteristic) + "\" sent"); - } }; /** @@ -128,19 +125,27 @@ public class ProximityManager extends BleManager { if (!isConnected()) return; - if (mAlertLevelCharacteristic != null) { - mAlertLevelCharacteristic.setValue(on ? HIGH_ALERT : NO_ALERT); - writeCharacteristic(mAlertLevelCharacteristic); - mAlertOn = on; - } else { - DebugLogger.w(TAG, "Immediate Alert Level Characteristic is not found"); - } + log(LogContract.Log.Level.VERBOSE, on ? "Setting alarm to HIGH..." : "Disabling alarm..."); + writeCharacteristic(mAlertLevelCharacteristic, on ? HIGH_ALERT : NO_ALERT) + .done(() -> { + mAlertOn = on; + log(LogContract.Log.Level.APPLICATION, "\"" + AlertLevelParser.parse(mAlertLevelCharacteristic) + "\" sent"); + }) + .fail(status -> log(LogContract.Log.Level.APPLICATION, "Alert Level characteristic not found")); } /** * Returns true if the alert has been enabled on the proximity tag, false otherwise. */ - public boolean isAlertEnabled() { + boolean isAlertEnabled() { return mAlertOn; } + + /** + * Returns the last obtained Battery Level value in percent. + * @return battery level value + */ + int getBatteryLevel() { + return mBatteryLevel; + } } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityService.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityService.java index bb20c4e0..52a02c5c 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityService.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/proximity/ProximityService.java @@ -45,6 +45,7 @@ import java.util.LinkedList; import java.util.List; import no.nordicsemi.android.ble.BleManager; +import no.nordicsemi.android.ble.BleManagerCallbacks; import no.nordicsemi.android.log.LogContract; import no.nordicsemi.android.nrftoolbox.FeaturesActivity; import no.nordicsemi.android.nrftoolbox.R; @@ -104,6 +105,16 @@ public class ProximityService extends BleMulticonnectProfileService implements P final ProximityManager manager = (ProximityManager) getBleManager(device); return manager.isAlertEnabled(); } + + /** + * Returns the last received battery level value. + * @param device the device of which battery level should be returned + * @return battery value or -1 if no value was received or Battery Level characteristic was not found + */ + public int getBatteryLevel(final BluetoothDevice device) { + final ProximityManager manager = (ProximityManager) getBleManager(device); + return manager.getBatteryLevel(); + } } @Override @@ -225,6 +236,7 @@ public class ProximityService extends BleMulticonnectProfileService implements P @Override public void onUnbind() { + createBackgroundNotification(); } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/rsc/RSCManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/rsc/RSCManager.java index 84afc684..d8c15bf8 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/rsc/RSCManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/rsc/RSCManager.java @@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Deque; import java.util.LinkedList; @@ -63,14 +64,14 @@ public class RSCManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); requests.add(Request.newEnableNotificationsRequest(mRSCMeasurementCharacteristic)); return requests; } @Override - public boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(RUNNING_SPEED_AND_CADENCE_SERVICE_UUID); if (service != null) { mRSCMeasurementCharacteristic = service.getCharacteristic(RSC_MEASUREMENT_CHARACTERISTIC_UUID); @@ -84,7 +85,7 @@ public class RSCManager extends BleManager { } @Override - public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + RSCMeasurementParser.parse(characteristic) + "\" received"); // Decode the new data diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/template/TemplateManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/template/TemplateManager.java index 8d5d4beb..97cceeb0 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/template/TemplateManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/template/TemplateManager.java @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import java.util.Deque; import java.util.LinkedList; @@ -64,7 +65,7 @@ public class TemplateManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); // TODO initialize your device, enable required notifications and indications, write what needs to be written to start working requests.add(Request.newEnableNotificationsRequest(mCharacteristic)); @@ -72,7 +73,7 @@ public class TemplateManager extends BleManager { } @Override - protected boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(SERVICE_UUID); if (service != null) { mCharacteristic = service.getCharacteristic(MEASUREMENT_CHARACTERISTIC_UUID); @@ -96,7 +97,7 @@ public class TemplateManager extends BleManager { } @Override - protected void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // TODO this method is called when a notification has been received // This method may be removed from this class if not required @@ -114,19 +115,19 @@ public class TemplateManager extends BleManager { } @Override - protected void onCharacteristicIndicated(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicIndicated(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // TODO this method is called when an indication has been received // This method may be removed from this class if not required } @Override - protected void onCharacteristicRead(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // TODO this method is called when the characteristic has been read // This method may be removed from this class if not required } @Override - protected void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + protected void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // TODO this method is called when the characteristic has been written // This method may be removed from this class if not required } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/uart/UARTManager.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/uart/UARTManager.java index a0938579..ec7a4d64 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/uart/UARTManager.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/uart/UARTManager.java @@ -26,6 +26,7 @@ import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Context; +import android.support.annotation.NonNull; import android.text.TextUtils; import java.io.UnsupportedEncodingException; @@ -66,14 +67,14 @@ public class UARTManager extends BleManager { private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() { @Override - protected Deque initGatt(final BluetoothGatt gatt) { + protected Deque initGatt(@NonNull final BluetoothGatt gatt) { final LinkedList requests = new LinkedList<>(); requests.add(Request.newEnableNotificationsRequest(mTXCharacteristic)); return requests; } @Override - public boolean isRequiredServiceSupported(final BluetoothGatt gatt) { + public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID); if (service != null) { mRXCharacteristic = service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID); @@ -103,7 +104,7 @@ public class UARTManager extends BleManager { } @Override - public void onCharacteristicWrite(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicWrite(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { // When the whole buffer has been sent final byte[] buffer = mOutgoingBuffer; if (mBufferOffset == buffer.length) { @@ -123,7 +124,7 @@ public class UARTManager extends BleManager { } @Override - public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { + public void onCharacteristicNotified(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) { final String data = characteristic.getStringValue(0); Logger.a(mLogSession, "\"" + data + "\" received"); mCallbacks.onDataReceived(gatt.getDevice(), data);