Small refactoring in Proximity: using AlertLevelData

This commit is contained in:
Aleksander Nowakowski
2018-05-22 17:36:43 +02:00
parent 1caa9ba38d
commit 16f5832a31
2 changed files with 23 additions and 20 deletions

View File

@@ -23,15 +23,21 @@ package no.nordicsemi.android.nrftoolbox.parser;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import no.nordicsemi.android.ble.data.Data;
public class AlertLevelParser { public class AlertLevelParser {
public static String parse(final BluetoothGattCharacteristic characteristic) {
return parse(Data.from(characteristic));
}
/** /**
* Parses the alert level. * Parses the alert level.
* *
* @param characteristic * @param data
* @return alert level in human readable format * @return alert level in human readable format
*/ */
public static String parse(final BluetoothGattCharacteristic characteristic) { public static String parse(final Data data) {
final int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); final int value = data.getIntValue(Data.FORMAT_UINT8, 0);
switch (value) { switch (value) {
case 0: case 0:

View File

@@ -29,25 +29,23 @@ import android.support.annotation.NonNull;
import java.util.UUID; import java.util.UUID;
import no.nordicsemi.android.ble.common.data.alert.AlertLevelData;
import no.nordicsemi.android.log.LogContract; import no.nordicsemi.android.log.LogContract;
import no.nordicsemi.android.nrftoolbox.battery.BatteryManager; import no.nordicsemi.android.nrftoolbox.battery.BatteryManager;
import no.nordicsemi.android.nrftoolbox.parser.AlertLevelParser; import no.nordicsemi.android.nrftoolbox.parser.AlertLevelParser;
@SuppressWarnings("WeakerAccess")
class ProximityManager extends BatteryManager<ProximityManagerCallbacks> { class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
private final String TAG = "ProximityManager"; private final String TAG = "ProximityManager";
/** Link Loss service UUID */
final static UUID LINKLOSS_SERVICE_UUID = UUID.fromString("00001803-0000-1000-8000-00805f9b34fb");
/** Immediate Alert service UUID */ /** Immediate Alert service UUID */
private final static UUID IMMEDIATE_ALERT_SERVICE_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb"); private final static UUID IMMEDIATE_ALERT_SERVICE_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");
/** Linkloss service UUID */
final static UUID LINKLOSS_SERVICE_UUID = UUID.fromString("00001803-0000-1000-8000-00805f9b34fb");
/** Alert Level characteristic UUID */ /** Alert Level characteristic UUID */
private static final UUID ALERT_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A06-0000-1000-8000-00805f9b34fb"); private static final UUID ALERT_LEVEL_CHARACTERISTIC_UUID = UUID.fromString("00002A06-0000-1000-8000-00805f9b34fb");
private final static byte[] HIGH_ALERT = { 0x02 }; private BluetoothGattCharacteristic mAlertLevelCharacteristic, mLinkLossCharacteristic;
private final static byte[] MILD_ALERT = { 0x01 };
private final static byte[] NO_ALERT = { 0x00 };
private BluetoothGattCharacteristic mAlertLevelCharacteristic, mLinklossCharacteristic;
private boolean mAlertOn; private boolean mAlertOn;
ProximityManager(final Context context) { ProximityManager(final Context context) {
@@ -59,6 +57,7 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
return true; return true;
} }
@NonNull
@Override @Override
protected BatteryManagerGattCallback getGattCallback() { protected BatteryManagerGattCallback getGattCallback() {
return mGattCallback; return mGattCallback;
@@ -72,16 +71,16 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
@Override @Override
protected void initialize() { protected void initialize() {
super.initialize(); super.initialize();
writeCharacteristic(mLinklossCharacteristic, HIGH_ALERT); writeCharacteristic(mLinkLossCharacteristic, AlertLevelData.highAlert());
} }
@Override @Override
protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) { protected boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
final BluetoothGattService llService = gatt.getService(LINKLOSS_SERVICE_UUID); final BluetoothGattService llService = gatt.getService(LINKLOSS_SERVICE_UUID);
if (llService != null) { if (llService != null) {
mLinklossCharacteristic = llService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID); mLinkLossCharacteristic = llService.getCharacteristic(ALERT_LEVEL_CHARACTERISTIC_UUID);
} }
return mLinklossCharacteristic != null; return mLinkLossCharacteristic != null;
} }
@Override @Override
@@ -98,7 +97,7 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
protected void onDeviceDisconnected() { protected void onDeviceDisconnected() {
super.onDeviceDisconnected(); super.onDeviceDisconnected();
mAlertLevelCharacteristic = null; mAlertLevelCharacteristic = null;
mLinklossCharacteristic = null; mLinkLossCharacteristic = null;
// Reset the alert flag // Reset the alert flag
mAlertOn = false; mAlertOn = false;
} }
@@ -122,11 +121,9 @@ class ProximityManager extends BatteryManager<ProximityManagerCallbacks> {
return; return;
log(LogContract.Log.Level.VERBOSE, on ? "Setting alarm to HIGH..." : "Disabling alarm..."); log(LogContract.Log.Level.VERBOSE, on ? "Setting alarm to HIGH..." : "Disabling alarm...");
writeCharacteristic(mAlertLevelCharacteristic, on ? HIGH_ALERT : NO_ALERT) writeCharacteristic(mAlertLevelCharacteristic, on ? AlertLevelData.highAlert() : AlertLevelData.noAlert())
.done(device -> { .with((device, data) -> log(LogContract.Log.Level.APPLICATION, "\"" + AlertLevelParser.parse(data) + "\" sent"))
mAlertOn = on; .done(device -> mAlertOn = on)
log(LogContract.Log.Level.APPLICATION, "\"" + AlertLevelParser.parse(mAlertLevelCharacteristic) + "\" sent");
})
.fail((device, status) -> log(LogContract.Log.Level.APPLICATION, "Alert Level characteristic not found")); .fail((device, status) -> log(LogContract.Log.Level.APPLICATION, "Alert Level characteristic not found"));
} }