mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-01-06 16:24:25 +01:00
Refactoring. Common DataCallbacks moved to BLE-Common-Library
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package no.nordicsemi.android.nrftoolbox.battery;
|
||||
|
||||
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.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.common.data.BatteryLevelDataCallback;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
|
||||
/**
|
||||
* The Ble Manager with Battery Service support.
|
||||
* @param <T> The profile callbacks type
|
||||
* @see BleManager
|
||||
*/
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public abstract class BatteryManager<T extends BatteryManagerCallbacks> extends BleManager<T> {
|
||||
/** 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 BluetoothGattCharacteristic mBatteryLevelCharacteristic;
|
||||
|
||||
/**
|
||||
* The manager constructor.
|
||||
*
|
||||
* @param context context
|
||||
*/
|
||||
public BatteryManager(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected abstract BatteryManagerGattCallback getGattCallback();
|
||||
|
||||
public void readBatteryLevelCharacteristic() {
|
||||
readCharacteristic(mBatteryLevelCharacteristic)
|
||||
.with(new BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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 BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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"));
|
||||
}
|
||||
|
||||
protected abstract class BatteryManagerGattCallback extends BleManagerGattCallback {
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
readBatteryLevelCharacteristic();
|
||||
enableBatteryLevelCharacteristicNotifications();
|
||||
}
|
||||
|
||||
@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() {
|
||||
mBatteryLevelCharacteristic = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package no.nordicsemi.android.nrftoolbox.battery;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManagerCallbacks;
|
||||
import no.nordicsemi.android.ble.common.profile.BatteryLevelCallback;
|
||||
|
||||
public interface BatteryManagerCallbacks extends BleManagerCallbacks, BatteryLevelCallback {
|
||||
}
|
||||
@@ -31,8 +31,8 @@ import java.util.Calendar;
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.profile.BloodPressureMeasurementCallback;
|
||||
import no.nordicsemi.android.ble.profile.IntermediateCuffPressureCallback;
|
||||
import no.nordicsemi.android.ble.common.profile.BloodPressureMeasurementCallback;
|
||||
import no.nordicsemi.android.ble.common.profile.IntermediateCuffPressureCallback;
|
||||
import no.nordicsemi.android.nrftoolbox.R;
|
||||
import no.nordicsemi.android.nrftoolbox.profile.BleProfileActivity;
|
||||
|
||||
|
||||
@@ -32,32 +32,24 @@ import android.support.annotation.Nullable;
|
||||
import java.util.Calendar;
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.callback.profile.BatteryLevelDataCallback;
|
||||
import no.nordicsemi.android.ble.callback.profile.BloodPressureMeasurementDataCallback;
|
||||
import no.nordicsemi.android.ble.callback.profile.IntermediateCuffPressureDataCallback;
|
||||
import no.nordicsemi.android.ble.profile.BloodPressureMeasurementCallback;
|
||||
import no.nordicsemi.android.ble.common.data.BloodPressureMeasurementDataCallback;
|
||||
import no.nordicsemi.android.ble.common.data.IntermediateCuffPressureDataCallback;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.log.Logger;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManager;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.BloodPressureMeasurementParser;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.IntermediateCuffPressureParser;
|
||||
|
||||
@SuppressWarnings({"unused", "WeakerAccess"})
|
||||
public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
public class BPMManager extends BatteryManager<BPMManagerCallbacks> {
|
||||
/** Blood Pressure service UUID. */
|
||||
public final static UUID BP_SERVICE_UUID = UUID.fromString("00001810-0000-1000-8000-00805f9b34fb");
|
||||
/** Blood Pressure Measurement characteristic UUID. */
|
||||
private static final UUID BPM_CHARACTERISTIC_UUID = UUID.fromString("00002A35-0000-1000-8000-00805f9b34fb");
|
||||
/** Intermediate Cuff Pressure characteristic UUID. */
|
||||
private static final UUID ICP_CHARACTERISTIC_UUID = UUID.fromString("00002A36-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 BluetoothGattCharacteristic mBPMCharacteristic, mICPCharacteristic;
|
||||
private BluetoothGattCharacteristic mBatteryLevelCharacteristic;
|
||||
|
||||
private static BPMManager managerInstance = null;
|
||||
|
||||
@@ -76,58 +68,19 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BleManagerGattCallback getGattCallback() {
|
||||
protected BatteryManagerGattCallback getGattCallback() {
|
||||
return mGattCallback;
|
||||
}
|
||||
|
||||
public void readBatteryLevelCharacteristic() {
|
||||
readCharacteristic(mBatteryLevelCharacteristic)
|
||||
.with(new BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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 BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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 notification, etc
|
||||
*/
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
private final BatteryManagerGattCallback mGattCallback = new BatteryManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
readBatteryLevelCharacteristic();
|
||||
enableBatteryLevelCharacteristicNotifications();
|
||||
super.initialize(device);
|
||||
|
||||
enableNotifications(mICPCharacteristic)
|
||||
.with(new IntermediateCuffPressureDataCallback() {
|
||||
@Override
|
||||
@@ -148,6 +101,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
log(LogContract.Log.Level.WARNING, "Invalid ICP data received: " + data);
|
||||
}
|
||||
});
|
||||
|
||||
enableIndications(mBPMCharacteristic)
|
||||
.with(new BloodPressureMeasurementDataCallback() {
|
||||
@Override
|
||||
@@ -182,10 +136,7 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
|
||||
@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);
|
||||
}
|
||||
super.isOptionalServiceSupported(gatt); // ignore the result of this
|
||||
return mICPCharacteristic != null;
|
||||
}
|
||||
|
||||
@@ -193,7 +144,6 @@ public class BPMManager extends BleManager<BPMManagerCallbacks> {
|
||||
protected void onDeviceDisconnected() {
|
||||
mICPCharacteristic = null;
|
||||
mBPMCharacteristic = null;
|
||||
mBatteryLevelCharacteristic = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -21,12 +21,11 @@
|
||||
*/
|
||||
package no.nordicsemi.android.nrftoolbox.bpm;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManagerCallbacks;
|
||||
import no.nordicsemi.android.ble.profile.BatteryLevelCallback;
|
||||
import no.nordicsemi.android.ble.profile.BloodPressureMeasurementCallback;
|
||||
import no.nordicsemi.android.ble.profile.IntermediateCuffPressureCallback;
|
||||
import no.nordicsemi.android.ble.common.profile.BloodPressureMeasurementCallback;
|
||||
import no.nordicsemi.android.ble.common.profile.IntermediateCuffPressureCallback;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManagerCallbacks;
|
||||
|
||||
interface BPMManagerCallbacks extends BleManagerCallbacks,
|
||||
BloodPressureMeasurementCallback, IntermediateCuffPressureCallback, BatteryLevelCallback {
|
||||
interface BPMManagerCallbacks extends BatteryManagerCallbacks,
|
||||
BloodPressureMeasurementCallback, IntermediateCuffPressureCallback {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package no.nordicsemi.android.nrftoolbox.cgms;
|
||||
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
|
||||
public class CGMSData {
|
||||
private static final byte OP_CODE_START_SESSION = 26;
|
||||
|
||||
static Data startSession() {
|
||||
return new Data(OP_CODE_START_SESSION);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
package no.nordicsemi.android.nrftoolbox.cgms;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
@@ -36,62 +37,62 @@ 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.nrftoolbox.battery.BatteryManager;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.CGMMeasurementParser;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.CGMSpecificOpsControlPointParser;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.RecordAccessControlPointParser;
|
||||
import no.nordicsemi.android.nrftoolbox.utility.DebugLogger;
|
||||
|
||||
public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
public class CGMSManager extends BatteryManager<CGMSManagerCallbacks> {
|
||||
private static final String TAG = "CGMSManager";
|
||||
|
||||
/**
|
||||
* Cycling Speed and Cadence service UUID
|
||||
*/
|
||||
public final static UUID CGMS_UUID = UUID.fromString("0000181F-0000-1000-8000-00805f9b34fb");
|
||||
public static final UUID CGMS_UUID = UUID.fromString("0000181F-0000-1000-8000-00805f9b34fb");
|
||||
private static final UUID CGM_MEASUREMENT_UUID = UUID.fromString("00002AA7-0000-1000-8000-00805f9b34fb");
|
||||
private static final UUID CGM_OPS_CONTROL_POINT_UUID = UUID.fromString("00002AAC-0000-1000-8000-00805f9b34fb");
|
||||
private final static int OP_CODE_START_SESSION = 26;
|
||||
/**
|
||||
* Record Access Control Point characteristic UUID
|
||||
*/
|
||||
private final static UUID RACP_UUID = UUID.fromString("00002A52-0000-1000-8000-00805f9b34fb");
|
||||
private static final UUID RACP_UUID = UUID.fromString("00002A52-0000-1000-8000-00805f9b34fb");
|
||||
|
||||
private final static int OP_CODE_REPORT_STORED_RECORDS = 1;
|
||||
private final static int OP_CODE_DELETE_STORED_RECORDS = 2;
|
||||
private final static int OP_CODE_ABORT_OPERATION = 3;
|
||||
private final static int OP_CODE_REPORT_NUMBER_OF_RECORDS = 4;
|
||||
private final static int OP_CODE_NUMBER_OF_STORED_RECORDS_RESPONSE = 5;
|
||||
private final static int OP_CODE_RESPONSE_CODE = 6;
|
||||
private static final int OP_CODE_REPORT_STORED_RECORDS = 1;
|
||||
private static final int OP_CODE_DELETE_STORED_RECORDS = 2;
|
||||
private static final int OP_CODE_ABORT_OPERATION = 3;
|
||||
private static final int OP_CODE_REPORT_NUMBER_OF_RECORDS = 4;
|
||||
private static final int OP_CODE_NUMBER_OF_STORED_RECORDS_RESPONSE = 5;
|
||||
private static final int OP_CODE_RESPONSE_CODE = 6;
|
||||
|
||||
private final static int OPERATOR_NULL = 0;
|
||||
private final static int OPERATOR_ALL_RECORDS = 1;
|
||||
private final static int OPERATOR_LESS_THEN_OR_EQUAL = 2;
|
||||
private final static int OPERATOR_GREATER_THEN_OR_EQUAL = 3;
|
||||
private final static int OPERATOR_WITHING_RANGE = 4;
|
||||
private final static int OPERATOR_FIRST_RECORD = 5;
|
||||
private final static int OPERATOR_LAST_RECORD = 6;
|
||||
private static final int OPERATOR_NULL = 0;
|
||||
private static final int OPERATOR_ALL_RECORDS = 1;
|
||||
private static final int OPERATOR_LESS_THEN_OR_EQUAL = 2;
|
||||
private static final int OPERATOR_GREATER_THEN_OR_EQUAL = 3;
|
||||
private static final int OPERATOR_WITHING_RANGE = 4;
|
||||
private static final int OPERATOR_FIRST_RECORD = 5;
|
||||
private static final int OPERATOR_LAST_RECORD = 6;
|
||||
|
||||
/**
|
||||
* The filter type is used for range operators ({@link #OPERATOR_LESS_THEN_OR_EQUAL}, {@link #OPERATOR_GREATER_THEN_OR_EQUAL}, {@link #OPERATOR_WITHING_RANGE}.<br/>
|
||||
* The syntax of the operand is: [Filter Type][Minimum][Maximum].<br/>
|
||||
* This filter selects the records by the sequence number.
|
||||
*/
|
||||
private final static int FILTER_TYPE_SEQUENCE_NUMBER = 1;
|
||||
private static final int FILTER_TYPE_SEQUENCE_NUMBER = 1;
|
||||
/**
|
||||
* The filter type is used for range operators ({@link #OPERATOR_LESS_THEN_OR_EQUAL}, {@link #OPERATOR_GREATER_THEN_OR_EQUAL}, {@link #OPERATOR_WITHING_RANGE}.<br/>
|
||||
* The syntax of the operand is: [Filter Type][Minimum][Maximum].<br/>
|
||||
* This filter selects the records by the user facing time (base time + offset time).
|
||||
*/
|
||||
private final static int FILTER_TYPE_USER_FACING_TIME = 2;
|
||||
private final static int RESPONSE_SUCCESS = 1;
|
||||
private final static int RESPONSE_OP_CODE_NOT_SUPPORTED = 2;
|
||||
private final static int RESPONSE_INVALID_OPERATOR = 3;
|
||||
private final static int RESPONSE_OPERATOR_NOT_SUPPORTED = 4;
|
||||
private final static int RESPONSE_INVALID_OPERAND = 5;
|
||||
private final static int RESPONSE_NO_RECORDS_FOUND = 6;
|
||||
private final static int RESPONSE_ABORT_UNSUCCESSFUL = 7;
|
||||
private final static int RESPONSE_PROCEDURE_NOT_COMPLETED = 8;
|
||||
private final static int RESPONSE_OPERAND_NOT_SUPPORTED = 9;
|
||||
private static final int FILTER_TYPE_USER_FACING_TIME = 2;
|
||||
private static final int RESPONSE_SUCCESS = 1;
|
||||
private static final int RESPONSE_OP_CODE_NOT_SUPPORTED = 2;
|
||||
private static final int RESPONSE_INVALID_OPERATOR = 3;
|
||||
private static final int RESPONSE_OPERATOR_NOT_SUPPORTED = 4;
|
||||
private static final int RESPONSE_INVALID_OPERAND = 5;
|
||||
private static final int RESPONSE_NO_RECORDS_FOUND = 6;
|
||||
private static final int RESPONSE_ABORT_UNSUCCESSFUL = 7;
|
||||
private static final int RESPONSE_PROCEDURE_NOT_COMPLETED = 8;
|
||||
private static final int RESPONSE_OPERAND_NOT_SUPPORTED = 9;
|
||||
|
||||
private BluetoothGattCharacteristic mCGMMeasurementCharacteristic;
|
||||
private BluetoothGattCharacteristic mCGMOpsControlPointCharacteristic;
|
||||
@@ -101,31 +102,28 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
private boolean mAbort;
|
||||
private long mSessionStartTime;
|
||||
|
||||
public CGMSManager(final Context context) {
|
||||
CGMSManager(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BleManagerGattCallback getGattCallback() {
|
||||
protected BatteryManagerGattCallback getGattCallback() {
|
||||
return mGattCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* BluetoothGatt callbacks for connection/disconnection, service discovery, receiving notification, etc
|
||||
*/
|
||||
private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
|
||||
private final BatteryManagerGattCallback mGattCallback = new BatteryManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected Deque<Request> initGatt(@NonNull final BluetoothGatt gatt) {
|
||||
final LinkedList<Request> requests = new LinkedList<>();
|
||||
requests.add(Request.newEnableNotificationsRequest(mCGMMeasurementCharacteristic));
|
||||
if (mCGMOpsControlPointCharacteristic != null) {
|
||||
mSessionStartTime = System.currentTimeMillis();
|
||||
requests.add(Request.newEnableIndicationsRequest(mCGMOpsControlPointCharacteristic));
|
||||
requests.add(Request.newWriteRequest(mCGMOpsControlPointCharacteristic, new byte[]{OP_CODE_START_SESSION}));
|
||||
}
|
||||
requests.add(Request.newEnableIndicationsRequest(mRecordAccessControlPointCharacteristic));
|
||||
return requests;
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
super.initialize(device);
|
||||
mSessionStartTime = System.currentTimeMillis();
|
||||
enableNotifications(mCGMMeasurementCharacteristic);
|
||||
enableIndications(mCGMOpsControlPointCharacteristic);
|
||||
writeCharacteristic(mCGMOpsControlPointCharacteristic, CGMSData.startSession());
|
||||
enableIndications(mRecordAccessControlPointCharacteristic);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -141,6 +139,7 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
|
||||
@Override
|
||||
protected boolean isOptionalServiceSupported(@NonNull final BluetoothGatt gatt) {
|
||||
super.isOptionalServiceSupported(gatt); // ignore the result
|
||||
final BluetoothGattService service = gatt.getService(CGMS_UUID);
|
||||
if (service != null) {
|
||||
mCGMOpsControlPointCharacteristic = service.getCharacteristic(CGM_OPS_CONTROL_POINT_UUID);
|
||||
@@ -148,12 +147,9 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
|
||||
return mCGMOpsControlPointCharacteristic != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicRead(@NonNull final BluetoothGatt gatt, @NonNull final BluetoothGattCharacteristic characteristic) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDeviceDisconnected() {
|
||||
super.onDeviceDisconnected();
|
||||
mCGMOpsControlPointCharacteristic = null;
|
||||
mCGMMeasurementCharacteristic = null;
|
||||
mRecordAccessControlPointCharacteristic = null;
|
||||
|
||||
@@ -25,8 +25,9 @@ package no.nordicsemi.android.nrftoolbox.cgms;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManagerCallbacks;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManagerCallbacks;
|
||||
|
||||
public interface CGMSManagerCallbacks extends BleManagerCallbacks {
|
||||
public interface CGMSManagerCallbacks extends BatteryManagerCallbacks {
|
||||
/**
|
||||
* Called when new CGM value has been obtained from the sensor.
|
||||
*/
|
||||
|
||||
@@ -33,27 +33,21 @@ import android.support.annotation.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManager;
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.callback.profile.BatteryLevelDataCallback;
|
||||
import no.nordicsemi.android.ble.callback.profile.CyclingSpeedAndCadenceDataCallback;
|
||||
import no.nordicsemi.android.ble.common.data.CyclingSpeedAndCadenceDataCallback;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
import no.nordicsemi.android.log.LogContract;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManager;
|
||||
import no.nordicsemi.android.nrftoolbox.csc.settings.SettingsFragment;
|
||||
import no.nordicsemi.android.nrftoolbox.parser.CSCMeasurementParser;
|
||||
|
||||
public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
public class CSCManager extends BatteryManager<CSCManagerCallbacks> {
|
||||
/** 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. */
|
||||
private final static 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);
|
||||
@@ -61,57 +55,18 @@ public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BleManagerGattCallback getGattCallback() {
|
||||
protected BatteryManagerGattCallback getGattCallback() {
|
||||
return mGattCallback;
|
||||
}
|
||||
|
||||
public void readBatteryLevelCharacteristic() {
|
||||
readCharacteristic(mBatteryLevelCharacteristic)
|
||||
.with(new BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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 BatteryLevelDataCallback() {
|
||||
@Override
|
||||
public void onBatteryLevelChanged(@NonNull final BluetoothDevice device, final int batteryLevel) {
|
||||
mCallbacks.onBatteryLevelChanged(device, batteryLevel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, 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() {
|
||||
private final BatteryManagerGattCallback mGattCallback = new BatteryManagerGattCallback() {
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull final BluetoothDevice device) {
|
||||
enableBatteryLevelCharacteristicNotifications();
|
||||
super.initialize(device);
|
||||
|
||||
// CSC characteristic is required
|
||||
enableNotifications(mCSCMeasurementCharacteristic)
|
||||
@@ -155,19 +110,10 @@ public class CSCManager extends BleManager<CSCManagerCallbacks> {
|
||||
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() {
|
||||
super.onDeviceDisconnected();
|
||||
mCSCMeasurementCharacteristic = null;
|
||||
mBatteryLevelCharacteristic = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -21,9 +21,8 @@
|
||||
*/
|
||||
package no.nordicsemi.android.nrftoolbox.csc;
|
||||
|
||||
import no.nordicsemi.android.ble.BleManagerCallbacks;
|
||||
import no.nordicsemi.android.ble.profile.BatteryLevelCallback;
|
||||
import no.nordicsemi.android.ble.profile.CyclingSpeedAndCadenceCallback;
|
||||
import no.nordicsemi.android.ble.common.profile.CyclingSpeedAndCadenceCallback;
|
||||
import no.nordicsemi.android.nrftoolbox.battery.BatteryManagerCallbacks;
|
||||
|
||||
interface CSCManagerCallbacks extends BleManagerCallbacks, CyclingSpeedAndCadenceCallback, BatteryLevelCallback {
|
||||
interface CSCManagerCallbacks extends BatteryManagerCallbacks, CyclingSpeedAndCadenceCallback {
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class CSCService extends BleProfileService implements CSCManagerCallbacks
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBatteryLevelChanged(final BluetoothDevice device, final int value) {
|
||||
public void onBatteryLevelChanged(@NonNull 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);
|
||||
|
||||
@@ -24,7 +24,7 @@ package no.nordicsemi.android.nrftoolbox.parser;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
|
||||
public class BloodPressureMeasurementParser {
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
*/
|
||||
package no.nordicsemi.android.nrftoolbox.parser;
|
||||
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
|
||||
public class CSCMeasurementParser {
|
||||
private static final byte WHEEL_REV_DATA_PRESENT = 0x01; // 1 bit
|
||||
|
||||
@@ -24,7 +24,7 @@ package no.nordicsemi.android.nrftoolbox.parser;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import no.nordicsemi.android.ble.callback.Data;
|
||||
import no.nordicsemi.android.ble.data.Data;
|
||||
|
||||
public class IntermediateCuffPressureParser {
|
||||
public static String parse(final Data data) {
|
||||
|
||||
Reference in New Issue
Block a user