Fix a pointer aliasing issue, add some constants, remove invalid "checkpointing"
This commit is contained in:
parent
d967411d70
commit
5d121e6e8f
|
@ -59,6 +59,16 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(MiBandSupport.class);
|
private static final Logger LOG = LoggerFactory.getLogger(MiBandSupport.class);
|
||||||
|
|
||||||
|
public static final int MODE_CHARGING = 6;
|
||||||
|
public static final int MODE_NONWEAR = 3;
|
||||||
|
public static final int MODE_NREM = 5;
|
||||||
|
public static final int MODE_ONBED = 7;
|
||||||
|
public static final int MODE_REM = 4;
|
||||||
|
public static final int MODE_RUNNING = 2;
|
||||||
|
public static final int MODE_SLIENT = 0;
|
||||||
|
public static final int MODE_USER = 100;
|
||||||
|
public static final int MODE_WALKING = 1;
|
||||||
|
|
||||||
//temporary buffer, size is a multiple of 60 because we want to store complete minutes (1 minute = 3 bytes)
|
//temporary buffer, size is a multiple of 60 because we want to store complete minutes (1 minute = 3 bytes)
|
||||||
private static final int activityDataHolderSize = 3 * 60 * 4; // 8h
|
private static final int activityDataHolderSize = 3 * 60 * 4; // 8h
|
||||||
private byte[] activityDataHolder = new byte[activityDataHolderSize];
|
private byte[] activityDataHolder = new byte[activityDataHolderSize];
|
||||||
|
@ -75,7 +85,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
private volatile boolean telephoneRinging;
|
private volatile boolean telephoneRinging;
|
||||||
private volatile boolean isLocatingDevice;
|
private volatile boolean isLocatingDevice;
|
||||||
|
|
||||||
|
|
||||||
public MiBandSupport() {
|
public MiBandSupport() {
|
||||||
addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE);
|
addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE);
|
||||||
}
|
}
|
||||||
|
@ -478,6 +487,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
public void onFetchActivityData() {
|
public void onFetchActivityData() {
|
||||||
try {
|
try {
|
||||||
TransactionBuilder builder = performInitialized("fetch activity data");
|
TransactionBuilder builder = performInitialized("fetch activity data");
|
||||||
|
// builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_LE_PARAMS), getLowLatency());
|
||||||
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()));
|
||||||
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), fetch);
|
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), fetch);
|
||||||
builder.queue(getQueue());
|
builder.queue(getQueue());
|
||||||
|
@ -565,7 +575,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void queueAlarm(GBAlarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
|
private void queueAlarm(GBAlarm alarm, TransactionBuilder builder, BluetoothGattCharacteristic characteristic) {
|
||||||
Calendar alarmCal = alarm.getAlarmCal();
|
Calendar alarmCal = alarm.getAlarmCal();
|
||||||
byte[] alarmMessage = new byte[]{
|
byte[] alarmMessage = new byte[]{
|
||||||
|
@ -611,11 +620,14 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
LOG.info("TIMESTAMP: " + DateFormat.getDateTimeInstance().format(timestamp.getTime()).toString() + " magic byte: " + dataUntilNextHeader);
|
LOG.info("TIMESTAMP: " + DateFormat.getDateTimeInstance().format(timestamp.getTime()).toString() + " magic byte: " + dataUntilNextHeader);
|
||||||
|
|
||||||
this.activityDataRemainingBytes = this.activityDataUntilNextHeader = dataUntilNextHeader;
|
this.activityDataRemainingBytes = this.activityDataUntilNextHeader = dataUntilNextHeader;
|
||||||
this.activityDataTimestampProgress = this.activityDataTimestampToAck = timestamp;
|
this.activityDataTimestampToAck = (GregorianCalendar) timestamp.clone();
|
||||||
|
this.activityDataTimestampProgress = timestamp;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
bufferActivityData(value);
|
bufferActivityData(value);
|
||||||
}
|
}
|
||||||
|
LOG.debug("activity data: length: " + value.length + ", remaining bytes: " + activityDataRemainingBytes);
|
||||||
|
|
||||||
if (this.activityDataRemainingBytes == 0) {
|
if (this.activityDataRemainingBytes == 0) {
|
||||||
sendAckDataTransfer(this.activityDataTimestampToAck, this.activityDataUntilNextHeader);
|
sendAckDataTransfer(this.activityDataTimestampToAck, this.activityDataUntilNextHeader);
|
||||||
flushActivityDataHolder();
|
flushActivityDataHolder();
|
||||||
|
@ -658,7 +670,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void flushActivityDataHolder() {
|
private void flushActivityDataHolder() {
|
||||||
GregorianCalendar timestamp = this.activityDataTimestampProgress;
|
|
||||||
byte category, intensity, steps;
|
byte category, intensity, steps;
|
||||||
|
|
||||||
ActivityDatabaseHandler dbHandler = GBApplication.getActivityDatabaseHandler();
|
ActivityDatabaseHandler dbHandler = GBApplication.getActivityDatabaseHandler();
|
||||||
|
@ -669,16 +680,15 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
steps = this.activityDataHolder[i + 2];
|
steps = this.activityDataHolder[i + 2];
|
||||||
|
|
||||||
dbHandler.addGBActivitySample(
|
dbHandler.addGBActivitySample(
|
||||||
(int) (timestamp.getTimeInMillis() / 1000),
|
(int) (activityDataTimestampProgress.getTimeInMillis() / 1000),
|
||||||
GBActivitySample.PROVIDER_MIBAND,
|
GBActivitySample.PROVIDER_MIBAND,
|
||||||
intensity,
|
intensity,
|
||||||
steps,
|
steps,
|
||||||
category);
|
category);
|
||||||
timestamp.add(Calendar.MINUTE, 1);
|
activityDataTimestampProgress.add(Calendar.MINUTE, 1);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
this.activityDataHolderProgress = 0;
|
this.activityDataHolderProgress = 0;
|
||||||
this.activityDataTimestampProgress = timestamp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -689,8 +699,8 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
LOG.info("handleControlPoint got status:" + status);
|
LOG.info("handleControlPoint got status:" + status);
|
||||||
|
|
||||||
if (getDevice().isBusy()) {
|
if (getDevice().isBusy()) {
|
||||||
if (value != null && value.length == 9 && value[0] == 0xa) {
|
if (isActivityDataSyncFinished(value)) {
|
||||||
handleActivityCheckpoint(value);
|
unsetBusy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -702,20 +712,17 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// I have no idea if this is anything we could or should do.
|
private boolean isActivityDataSyncFinished(byte[] value) {
|
||||||
// For some reason, I got these bytes on the control point characteristic
|
// byte 0 is the kind of message
|
||||||
// while in the middle of a activity data transfer:
|
// byte 1 to 6 represent a timestamp
|
||||||
// { 0xa, 0xf, 0x6, 0x5, 0x14, 0xe, 0x15, 0x70, 0x8 }
|
// byte 7 to 8 represent the amount of data left (0 = done)
|
||||||
// 'a' = activity, then timestamp, then bytes to read
|
LOG.info("finished?: " + GB.hexdump(value, 0, -1));
|
||||||
// After this, the communication stops. And this happened again and again
|
if (value.length == 9) {
|
||||||
// so that I couldn't transfer the remaning data anymore.
|
if (value[0] == 0xa && value[7] == 0 && value[8] == 0) {
|
||||||
// My idea was that this might be some kind of checkpointing, asking for an ACK
|
return true;
|
||||||
// of the transfer till now.
|
}
|
||||||
// Commented out for now until it can be reproduced and tested again.
|
}
|
||||||
private void handleActivityCheckpoint(byte[] value) {
|
return false;
|
||||||
GregorianCalendar timestamp = parseTimestamp(value, 1);
|
|
||||||
sendAckDataTransfer(timestamp, activityDataHolderProgress);
|
|
||||||
flushActivityDataHolder();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unsetBusy() {
|
private void unsetBusy() {
|
||||||
|
|
Loading…
Reference in New Issue