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 no.nordicsemi.android.ble.data.Data;
public class AlertLevelParser {
public static String parse(final BluetoothGattCharacteristic characteristic) {
return parse(Data.from(characteristic));
}
/**
* Parses the alert level.
*
* @param characteristic
* @param data
* @return alert level in human readable format
*/
public static String parse(final BluetoothGattCharacteristic characteristic) {
final int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
public static String parse(final Data data) {
final int value = data.getIntValue(Data.FORMAT_UINT8, 0);
switch (value) {
case 0:

View File

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