Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit f4269a4

Browse files
committed
Alert Level callbacks
1 parent ba0610d commit f4269a4

File tree

5 files changed

+240
-0
lines changed

5 files changed

+240
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package no.nordicsemi.android.ble.common.callback.alert;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
import android.os.Parcel;
5+
6+
import androidx.annotation.NonNull;
7+
import no.nordicsemi.android.ble.callback.profile.ProfileReadResponse;
8+
import no.nordicsemi.android.ble.common.profile.alert.AlertLevelCallback;
9+
import no.nordicsemi.android.ble.data.Data;
10+
11+
public abstract class AlertLevelDataCallback extends ProfileReadResponse implements AlertLevelCallback {
12+
13+
public AlertLevelDataCallback() {
14+
// empty
15+
}
16+
17+
protected AlertLevelDataCallback(final Parcel in) {
18+
super(in);
19+
}
20+
21+
@Override
22+
public void onDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
23+
super.onDataReceived(device, data);
24+
25+
if (data.size() == 1) {
26+
final Integer level = data.getIntValue(Data.FORMAT_UINT8, 0);
27+
if (level != null && level <= AlertLevelCallback.ALERT_HIGH) {
28+
onAlertLevelChanged(device, level);
29+
return;
30+
}
31+
}
32+
onInvalidDataReceived(device, data);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package no.nordicsemi.android.ble.common.callback.alert;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
import android.os.Parcel;
5+
import android.os.Parcelable;
6+
7+
import androidx.annotation.NonNull;
8+
import no.nordicsemi.android.ble.common.profile.alert.AlertLevel;
9+
import no.nordicsemi.android.ble.exception.InvalidDataException;
10+
import no.nordicsemi.android.ble.exception.RequestFailedException;
11+
12+
/**
13+
* Response class that could be used as a result of a synchronous request.
14+
* The data received are available through getters, instead of a callback.
15+
* <p>
16+
* Usage example:
17+
* <pre>
18+
* try {
19+
* AlertLevelResponse response = waitForWrite(characteristic)
20+
* .awaitValid(AlertLevelResponse.class);
21+
* int alertLevel = response.getAlertLevel();
22+
* ...
23+
* } catch ({@link RequestFailedException} e) {
24+
* Log.w(TAG, "Request failed with status " + e.getStatus(), e);
25+
* } catch ({@link InvalidDataException} e) {
26+
* Log.w(TAG, "Invalid data received: " + e.getResponse().getRawData());
27+
* }
28+
* </pre>
29+
* </p>
30+
*/
31+
public final class AlertLevelResponse extends AlertLevelDataCallback implements Parcelable {
32+
private int level;
33+
34+
public AlertLevelResponse() {
35+
// empty
36+
}
37+
38+
@Override
39+
public void onAlertLevelChanged(@NonNull final BluetoothDevice device, final int level) {
40+
this.level = level;
41+
}
42+
43+
/**
44+
* Returns the Alert Level value.
45+
*
46+
* @return the received alert level value.
47+
*/
48+
@AlertLevel
49+
public int getAlertLevel() {
50+
return level;
51+
}
52+
53+
// Parcelable
54+
private AlertLevelResponse(final Parcel in) {
55+
super(in);
56+
level = in.readInt();
57+
}
58+
59+
@Override
60+
public void writeToParcel(final Parcel dest, final int flags) {
61+
super.writeToParcel(dest, flags);
62+
dest.writeInt(level);
63+
}
64+
65+
public static final Creator<AlertLevelResponse> CREATOR = new Creator<AlertLevelResponse>() {
66+
@Override
67+
public AlertLevelResponse createFromParcel(final Parcel in) {
68+
return new AlertLevelResponse(in);
69+
}
70+
71+
@Override
72+
public AlertLevelResponse[] newArray(final int size) {
73+
return new AlertLevelResponse[size];
74+
}
75+
};
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package no.nordicsemi.android.ble.common.profile.alert;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
6+
import androidx.annotation.IntDef;
7+
8+
@SuppressWarnings("WeakerAccess")
9+
@Retention(RetentionPolicy.SOURCE)
10+
@IntDef(value = {
11+
AlertLevelCallback.ALERT_NONE,
12+
AlertLevelCallback.ALERT_MILD,
13+
AlertLevelCallback.ALERT_HIGH
14+
})
15+
public @interface AlertLevel {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package no.nordicsemi.android.ble.common.profile.alert;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
5+
import androidx.annotation.NonNull;
6+
7+
public interface AlertLevelCallback {
8+
int ALERT_NONE = 0x00;
9+
int ALERT_MILD = 0x01;
10+
int ALERT_HIGH = 0x02;
11+
12+
/**
13+
* Method called when Alert Level characteristic value has changed.
14+
*
15+
* @param device the target device.
16+
* @param level the new alert level.
17+
*/
18+
void onAlertLevelChanged(@NonNull final BluetoothDevice device, @AlertLevel final int level);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package no.nordicsemi.android.ble.common.callback.alert;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
5+
import org.junit.Test;
6+
7+
import androidx.annotation.NonNull;
8+
import no.nordicsemi.android.ble.callback.DataReceivedCallback;
9+
import no.nordicsemi.android.ble.common.data.alert.AlertLevelData;
10+
import no.nordicsemi.android.ble.common.profile.alert.AlertLevelCallback;
11+
import no.nordicsemi.android.ble.data.Data;
12+
13+
import static org.junit.Assert.*;
14+
15+
public class AlertLevelDataCallbackTest {
16+
17+
@Test
18+
public void onAlertLevelChanged_high() {
19+
final DataReceivedCallback callback = new AlertLevelDataCallback() {
20+
@Override
21+
public void onAlertLevelChanged(@NonNull final BluetoothDevice device, final int level) {
22+
assertEquals("High alert received", AlertLevelCallback.ALERT_HIGH, level);
23+
}
24+
25+
@Override
26+
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
27+
super.onInvalidDataReceived(device, data);
28+
fail("Correct data reported as invalid");
29+
}
30+
};
31+
final Data data = AlertLevelData.highAlert();
32+
callback.onDataReceived(null, data);
33+
34+
assertEquals("Correct value", 0x02, AlertLevelCallback.ALERT_HIGH);
35+
}
36+
37+
@Test
38+
public void onAlertLevelChanged_mild() {
39+
final DataReceivedCallback callback = new AlertLevelDataCallback() {
40+
@Override
41+
public void onAlertLevelChanged(@NonNull final BluetoothDevice device, final int level) {
42+
assertEquals("Mild alert received", AlertLevelCallback.ALERT_MILD, level);
43+
}
44+
45+
@Override
46+
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
47+
super.onInvalidDataReceived(device, data);
48+
fail("Correct data reported as invalid");
49+
}
50+
};
51+
final Data data = AlertLevelData.mildAlert();
52+
callback.onDataReceived(null, data);
53+
54+
assertEquals("Correct value", 0x01, AlertLevelCallback.ALERT_MILD);
55+
}
56+
57+
@Test
58+
public void onAlertLevelChanged_none() {
59+
final DataReceivedCallback callback = new AlertLevelDataCallback() {
60+
@Override
61+
public void onAlertLevelChanged(@NonNull final BluetoothDevice device, final int level) {
62+
assertEquals("No alert received", AlertLevelCallback.ALERT_NONE, level);
63+
}
64+
65+
@Override
66+
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
67+
super.onInvalidDataReceived(device, data);
68+
fail("Correct data reported as invalid");
69+
}
70+
};
71+
final Data data = AlertLevelData.noAlert();
72+
callback.onDataReceived(null, data);
73+
74+
assertEquals("Correct value", 0x00, AlertLevelCallback.ALERT_NONE);
75+
}
76+
77+
@Test
78+
public void onAlertLevelChanged_invalid() {
79+
final DataReceivedCallback callback = new AlertLevelDataCallback() {
80+
@Override
81+
public void onAlertLevelChanged(@NonNull final BluetoothDevice device, final int level) {
82+
fail("Invalid data reported as valid");
83+
}
84+
85+
@Override
86+
public void onInvalidDataReceived(@NonNull final BluetoothDevice device, @NonNull final Data data) {
87+
super.onInvalidDataReceived(device, data);
88+
assertEquals("Invalid data", 2, data.size());
89+
}
90+
};
91+
final Data data = Data.opCode((byte) 0x01, (byte) 0x02);
92+
callback.onDataReceived(null, data);
93+
94+
assertEquals("Correct value", 0x00, AlertLevelCallback.ALERT_NONE);
95+
}
96+
}

0 commit comments

Comments
 (0)