Added an option to call BLE requests in synchronous way. Not tested.

This commit is contained in:
Aleksander Nowakowski
2018-05-04 16:32:39 +02:00
parent 1f2d78c8f5
commit 396983cb24
5 changed files with 49 additions and 36 deletions

View File

@@ -68,8 +68,9 @@ public abstract class BatteryManager<T extends BatteryManagerCallbacks> extends
public void enableBatteryLevelCharacteristicNotifications() { public void enableBatteryLevelCharacteristicNotifications() {
if (isConnected()) { if (isConnected()) {
// If the Battery Level characteristic is null, the request will be ignored // If the Battery Level characteristic is null, the request will be ignored
setNotificationCallback(mBatteryLevelCharacteristic)
.with(mBatteryLevelDataCallback);
enableNotifications(mBatteryLevelCharacteristic) enableNotifications(mBatteryLevelCharacteristic)
.with(mBatteryLevelDataCallback)
.done(device -> log(LogContract.Log.Level.INFO, "Battery Level notifications enabled")) .done(device -> log(LogContract.Log.Level.INFO, "Battery Level notifications enabled"))
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found")); .fail((device, status) -> log(LogContract.Log.Level.WARNING, "Battery Level characteristic not found"));
} }

View File

@@ -81,31 +81,30 @@ public class BPMManager extends BatteryManager<BPMManagerCallbacks> {
protected void initialize() { protected void initialize() {
super.initialize(); super.initialize();
enableNotifications(mICPCharacteristic) setNotificationCallback(mICPCharacteristic)
.with(new IntermediateCuffPressureDataCallback() { .with(new IntermediateCuffPressureDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
log(LogContract.Log.Level.APPLICATION, "\"" + IntermediateCuffPressureParser.parse(data) + "\" received"); log(LogContract.Log.Level.APPLICATION, "\"" + IntermediateCuffPressureParser.parse(data) + "\" received");
// Pass through received data // Pass through received data
super.onDataReceived(device, data); super.onDataReceived(device, data);
} }
@Override @Override
public void onIntermediateCuffPressureReceived(@NonNull final BluetoothDevice device, public void onIntermediateCuffPressureReceived(@NonNull final BluetoothDevice device,
final float cuffPressure, final int unit, final float cuffPressure, final int unit,
@Nullable final Float pulseRate, @Nullable final Integer userID, @Nullable final Float pulseRate, @Nullable final Integer userID,
@Nullable final BPMStatus status, @Nullable final Calendar calendar) { @Nullable final BPMStatus status, @Nullable final Calendar calendar) {
mCallbacks.onIntermediateCuffPressureReceived(device, cuffPressure, unit, pulseRate, userID, status, calendar); mCallbacks.onIntermediateCuffPressureReceived(device, cuffPressure, unit, pulseRate, userID, status, calendar);
} }
@Override @Override
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
log(LogContract.Log.Level.WARNING, "Invalid ICP data received: " + data); log(LogContract.Log.Level.WARNING, "Invalid ICP data received: " + data);
} }
}); });
setIndicationCallback(mBPMCharacteristic)
enableIndications(mBPMCharacteristic)
.with(new BloodPressureMeasurementDataCallback() { .with(new BloodPressureMeasurementDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -129,6 +128,9 @@ public class BPMManager extends BatteryManager<BPMManagerCallbacks> {
log(LogContract.Log.Level.WARNING, "Invalid BPM data received: " + data); log(LogContract.Log.Level.WARNING, "Invalid BPM data received: " + data);
} }
}); });
enableNotifications(mICPCharacteristic);
enableIndications(mBPMCharacteristic);
} }
@Override @Override

View File

@@ -124,8 +124,8 @@ public class CGMSManager extends BatteryManager<CGMSManagerCallbacks> {
} }
}).fail((device, status) -> log(LogContract.Log.Level.WARNING, "Could not read CGM Status characteristic")); }).fail((device, status) -> log(LogContract.Log.Level.WARNING, "Could not read CGM Status characteristic"));
// Enable Continuous Glucose Measurement notifications // Set notification and indication callbacks
enableNotifications(mCGMMeasurementCharacteristic) setNotificationCallback(mCGMMeasurementCharacteristic)
.with(new ContinuousGlucoseMeasurementDataCallback() { .with(new ContinuousGlucoseMeasurementDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -153,11 +153,9 @@ public class CGMSManager extends BatteryManager<CGMSManagerCallbacks> {
public void onContinuousGlucoseMeasurementReceivedWithCrcError(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onContinuousGlucoseMeasurementReceivedWithCrcError(@NonNull final BluetoothDevice device, @NonNull final Data data) {
log(LogContract.Log.Level.WARNING, "Continuous Glucose Measurement record received with CRC error"); log(LogContract.Log.Level.WARNING, "Continuous Glucose Measurement record received with CRC error");
} }
}) });
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enable Continuous Glucose Measurement notifications (" + status + ")"));
// Enable CGM Specific Ops indications setIndicationCallback(mCGMSpecificOpsControlPointCharacteristic)
enableIndications(mCGMSpecificOpsControlPointCharacteristic)
.with(new CGMSpecificOpsControlPointDataCallback() { .with(new CGMSpecificOpsControlPointDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -198,7 +196,7 @@ public class CGMSManager extends BatteryManager<CGMSManagerCallbacks> {
} }
}); });
enableIndications(mRecordAccessControlPointCharacteristic) setIndicationCallback(mRecordAccessControlPointCharacteristic)
.with(new RecordAccessControlPointDataCallback() { .with(new RecordAccessControlPointDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -250,7 +248,14 @@ public class CGMSManager extends BatteryManager<CGMSManagerCallbacks> {
mCallbacks.onOperationFailed(device); mCallbacks.onOperationFailed(device);
} }
} }
}) });
// Enable notifications and indications
enableNotifications(mCGMMeasurementCharacteristic)
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enable Continuous Glucose Measurement notifications (" + status + ")"));
enableIndications(mCGMSpecificOpsControlPointCharacteristic)
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enable CGM Specific Ops Control Point indications notifications (" + status + ")"));
enableIndications(mRecordAccessControlPointCharacteristic)
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enabled Record Access Control Point indications (error " + status + ")")); .fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enabled Record Access Control Point indications (error " + status + ")"));
// Start Continuous Glucose session if hasn't been started before // Start Continuous Glucose session if hasn't been started before

View File

@@ -69,7 +69,7 @@ public class CSCManager extends BatteryManager<CSCManagerCallbacks> {
super.initialize(); super.initialize();
// CSC characteristic is required // CSC characteristic is required
enableNotifications(mCSCMeasurementCharacteristic) setNotificationCallback(mCSCMeasurementCharacteristic)
.with(new CyclingSpeedAndCadenceDataCallback() { .with(new CyclingSpeedAndCadenceDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, final @NonNull Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, final @NonNull Data data) {
@@ -99,6 +99,7 @@ public class CSCManager extends BatteryManager<CSCManagerCallbacks> {
log(LogContract.Log.Level.WARNING, "Invalid CSC Measurement data received: " + data); log(LogContract.Log.Level.WARNING, "Invalid CSC Measurement data received: " + data);
} }
}); });
enableNotifications(mCSCMeasurementCharacteristic);
} }
@Override @Override

View File

@@ -112,8 +112,7 @@ public class GlucoseManager extends BatteryManager<GlucoseManagerCallbacks> {
} }
device.setCharacteristicNotification(mRecordAccessControlPointCharacteristic, true); device.setCharacteristicNotification(mRecordAccessControlPointCharacteristic, true);
*/ */
setNotificationCallback(mGlucoseMeasurementCharacteristic)
enableNotifications(mGlucoseMeasurementCharacteristic)
.with(new GlucoseMeasurementDataCallback() { .with(new GlucoseMeasurementDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -148,7 +147,8 @@ public class GlucoseManager extends BatteryManager<GlucoseManagerCallbacks> {
}); });
} }
}); });
enableNotifications(mGlucoseMeasurementContextCharacteristic)
setNotificationCallback(mGlucoseMeasurementContextCharacteristic)
.with(new GlucoseMeasurementContextDataCallback() { .with(new GlucoseMeasurementContextDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -190,7 +190,7 @@ public class GlucoseManager extends BatteryManager<GlucoseManagerCallbacks> {
} }
}); });
enableIndications(mRecordAccessControlPointCharacteristic) setIndicationCallback(mRecordAccessControlPointCharacteristic)
.with(new RecordAccessControlPointDataCallback() { .with(new RecordAccessControlPointDataCallback() {
@Override @Override
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) { public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
@@ -239,7 +239,11 @@ public class GlucoseManager extends BatteryManager<GlucoseManagerCallbacks> {
mCallbacks.onOperationFailed(device); mCallbacks.onOperationFailed(device);
} }
} }
}) });
enableNotifications(mGlucoseMeasurementCharacteristic);
enableNotifications(mGlucoseMeasurementContextCharacteristic);
enableIndications(mRecordAccessControlPointCharacteristic)
.fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enabled Record Access Control Point indications (error " + status + ")")); .fail((device, status) -> log(LogContract.Log.Level.WARNING, "Failed to enabled Record Access Control Point indications (error " + status + ")"));
} }