NO.1 F1: Check CRC before saving data.
parent
32e1dec0f8
commit
bf34586a22
|
@ -68,6 +68,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
public BluetoothGattCharacteristic ctrlCharacteristic = null;
|
public BluetoothGattCharacteristic ctrlCharacteristic = null;
|
||||||
public BluetoothGattCharacteristic measureCharacteristic = null;
|
public BluetoothGattCharacteristic measureCharacteristic = null;
|
||||||
private List<No1F1ActivitySample> samples = new ArrayList<>();
|
private List<No1F1ActivitySample> samples = new ArrayList<>();
|
||||||
|
private byte crc = 0;
|
||||||
|
|
||||||
public No1F1Support() {
|
public No1F1Support() {
|
||||||
super(LOG);
|
super(LOG);
|
||||||
|
@ -246,6 +247,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
public void onFetchActivityData() {
|
public void onFetchActivityData() {
|
||||||
try {
|
try {
|
||||||
samples.clear();
|
samples.clear();
|
||||||
|
crc = 0;
|
||||||
TransactionBuilder builder = performInitialized("fetchSteps");
|
TransactionBuilder builder = performInitialized("fetchSteps");
|
||||||
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
|
builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
|
||||||
byte[] msg = new byte[]{
|
byte[] msg = new byte[]{
|
||||||
|
@ -526,8 +528,14 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
private void handleStepData(byte[] data) {
|
private void handleStepData(byte[] data) {
|
||||||
if (data[1] == (byte) 0xfd) {
|
if (data[1] == (byte) 0xfd) {
|
||||||
// TODO Check CRC
|
LOG.info("CRC received: " + (data[2] & 0xff) + ", calculated: " + (crc & 0xff));
|
||||||
if (samples.size() > 0) {
|
if (data[2] != crc) {
|
||||||
|
GB.toast(getContext(), "Incorrect CRC. Try fetching data again.", Toast.LENGTH_LONG, GB.ERROR);
|
||||||
|
if (getDevice().isBusy()) {
|
||||||
|
getDevice().unsetBusyTask();
|
||||||
|
getDevice().sendDeviceUpdateIntent(getContext());
|
||||||
|
}
|
||||||
|
} else if (samples.size() > 0) {
|
||||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||||
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
||||||
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
||||||
|
@ -540,6 +548,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
provider.addGBActivitySample(samples.get(i));
|
provider.addGBActivitySample(samples.get(i));
|
||||||
}
|
}
|
||||||
samples.clear();
|
samples.clear();
|
||||||
|
crc = 0;
|
||||||
LOG.info("Steps data saved");
|
LOG.info("Steps data saved");
|
||||||
try {
|
try {
|
||||||
TransactionBuilder builder = performInitialized("fetchSleep");
|
TransactionBuilder builder = performInitialized("fetchSleep");
|
||||||
|
@ -572,6 +581,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
sample.setSteps(data[6] * 256 + (data[7] & 0xff));
|
sample.setSteps(data[6] * 256 + (data[7] & 0xff));
|
||||||
|
|
||||||
samples.add(sample);
|
samples.add(sample);
|
||||||
|
crc ^= (data[6] ^ data[7]);
|
||||||
LOG.info("Received steps data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
LOG.info("Received steps data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
||||||
sample.getSteps() + " steps"
|
sample.getSteps() + " steps"
|
||||||
);
|
);
|
||||||
|
@ -580,8 +590,14 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
private void handleSleepData(byte[] data) {
|
private void handleSleepData(byte[] data) {
|
||||||
if (data[1] == (byte) 0xfd) {
|
if (data[1] == (byte) 0xfd) {
|
||||||
// TODO Check CRC
|
LOG.info("CRC received: " + (data[2] & 0xff) + ", calculated: " + (crc & 0xff));
|
||||||
if (samples.size() > 0) {
|
if (data[2] != crc) {
|
||||||
|
GB.toast(getContext(), "Incorrect CRC. Try fetching data again.", Toast.LENGTH_LONG, GB.ERROR);
|
||||||
|
if (getDevice().isBusy()) {
|
||||||
|
getDevice().unsetBusyTask();
|
||||||
|
getDevice().sendDeviceUpdateIntent(getContext());
|
||||||
|
}
|
||||||
|
} else if (samples.size() > 0) {
|
||||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||||
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
||||||
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
||||||
|
@ -596,6 +612,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
provider.addGBActivitySample(samples.get(i));
|
provider.addGBActivitySample(samples.get(i));
|
||||||
}
|
}
|
||||||
samples.clear();
|
samples.clear();
|
||||||
|
crc = 0;
|
||||||
LOG.info("Sleep data saved");
|
LOG.info("Sleep data saved");
|
||||||
try {
|
try {
|
||||||
TransactionBuilder builder = performInitialized("fetchHeartRate");
|
TransactionBuilder builder = performInitialized("fetchHeartRate");
|
||||||
|
@ -628,6 +645,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
sample.setRawIntensity(data[7] * 256 + (data[8] & 0xff));
|
sample.setRawIntensity(data[7] * 256 + (data[8] & 0xff));
|
||||||
|
|
||||||
samples.add(sample);
|
samples.add(sample);
|
||||||
|
crc ^= (data[7] ^ data[8]);
|
||||||
LOG.info("Received sleep data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
LOG.info("Received sleep data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
||||||
sample.getRawIntensity() + " rolls"
|
sample.getRawIntensity() + " rolls"
|
||||||
);
|
);
|
||||||
|
@ -636,8 +654,14 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
private void handleHeartRateData(byte[] data) {
|
private void handleHeartRateData(byte[] data) {
|
||||||
if (data[1] == (byte) 0xfd) {
|
if (data[1] == (byte) 0xfd) {
|
||||||
// TODO Check CRC
|
LOG.info("CRC received: " + (data[2] & 0xff) + ", calculated: " + (crc & 0xff));
|
||||||
if (samples.size() > 0) {
|
if (data[2] != crc) {
|
||||||
|
GB.toast(getContext(), "Incorrect CRC. Try fetching data again.", Toast.LENGTH_LONG, GB.ERROR);
|
||||||
|
if (getDevice().isBusy()) {
|
||||||
|
getDevice().unsetBusyTask();
|
||||||
|
getDevice().sendDeviceUpdateIntent(getContext());
|
||||||
|
}
|
||||||
|
} else if (samples.size() > 0) {
|
||||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||||
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
Long userId = DBHelper.getUser(dbHandler.getDaoSession()).getId();
|
||||||
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
Long deviceId = DBHelper.getDevice(getDevice(), dbHandler.getDaoSession()).getId();
|
||||||
|
@ -648,6 +672,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
provider.addGBActivitySample(samples.get(i));
|
provider.addGBActivitySample(samples.get(i));
|
||||||
}
|
}
|
||||||
samples.clear();
|
samples.clear();
|
||||||
|
crc = 0;
|
||||||
LOG.info("Heart rate data saved");
|
LOG.info("Heart rate data saved");
|
||||||
if (getDevice().isBusy()) {
|
if (getDevice().isBusy()) {
|
||||||
getDevice().unsetBusyTask();
|
getDevice().unsetBusyTask();
|
||||||
|
@ -672,6 +697,7 @@ public class No1F1Support extends AbstractBTLEDeviceSupport {
|
||||||
sample.setHeartRate(data[7] & 0xff);
|
sample.setHeartRate(data[7] & 0xff);
|
||||||
|
|
||||||
samples.add(sample);
|
samples.add(sample);
|
||||||
|
crc ^= (data[6] ^ data[7]);
|
||||||
LOG.info("Received heart rate data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
LOG.info("Received heart rate data for " + String.format("%1$TD %1$TT", timestamp) + ": " +
|
||||||
sample.getHeartRate() + " BPM"
|
sample.getHeartRate() + " BPM"
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue