1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-29 05:16:51 +01:00

Remove the timeout and handle the messages that the band sends back to acknowledge its status.

This should make the firmware transfer much more reliable and hopefully release-ready.
This commit is contained in:
Daniele Gobbetti 2015-07-31 12:30:42 +02:00
parent fbbc2afda4
commit 2208d5088b

View File

@ -84,6 +84,10 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
private DeviceInfo mDeviceInfo; private DeviceInfo mDeviceInfo;
private boolean firmwareInfoSent = false;
private byte[] newFirmware;
private boolean rebootWhenBandReady = false;
public MiBandSupport() { public MiBandSupport() {
addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE); addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE);
} }
@ -567,13 +571,9 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
int checksum = (Integer.decode("0x" + mMacOctets[4]) << 8 | Integer.decode("0x" + mMacOctets[5])) ^ mFwHelper.getCRC16(mFwHelper.getFw()); int checksum = (Integer.decode("0x" + mMacOctets[4]) << 8 | Integer.decode("0x" + mMacOctets[5])) ^ mFwHelper.getCRC16(mFwHelper.getFw());
if (sendFirmwareInfo(oldFwVersion, newFwVersion, mFwHelper.getFw().length, checksum)) { if (sendFirmwareInfo(oldFwVersion, newFwVersion, mFwHelper.getFw().length, checksum)) {
//metadata were sent correctly, send the actual firmware firmwareInfoSent = true;
if(sendFirmwareData(mFwHelper.getFw())) { newFirmware = mFwHelper.getFw();
//firmware was sent correctly, reboot //the firmware will be sent by the notification listener if the band confirms that the metadata are ok.
onReboot();
} else {
//TODO: the firmware transfer failed, but the miband should be still functional with the old firmware. What should we do?
}
} }
return; return;
@ -615,7 +615,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) { } else if (MiBandService.UUID_CHARACTERISTIC_BATTERY.equals(characteristicUUID)) {
handleBatteryInfo(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS); handleBatteryInfo(characteristic.getValue(), BluetoothGatt.GATT_SUCCESS);
} else if (MiBandService.UUID_CHARACTERISTIC_NOTIFICATION.equals(characteristicUUID)) { } else if (MiBandService.UUID_CHARACTERISTIC_NOTIFICATION.equals(characteristicUUID)) {
// device somehow changed, should we update e.g. battery level? handleNotificationNotif(characteristic.getValue());
} }
} }
@ -645,6 +645,58 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
} }
} }
private void logMessageContent(byte[] value) {
LOG.info("RECEIVED DATA WITH LENGTH: " + value.length);
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
}
private void handleNotificationNotif(byte[] value) {
if(value.length != 1) {
LOG.error("Notifications should be 1 byte long.");
LOG.info("RECEIVED DATA WITH LENGTH: " + value.length);
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
return;
}
switch (value[0]) {
case MiBandService.NOTIFY_FW_CHECK_SUCCESS:
if(firmwareInfoSent && newFirmware != null) {
if(sendFirmwareData(newFirmware)) {
rebootWhenBandReady = true;
} else {
//TODO: the firmware transfer failed, but the miband should be still functional with the old firmware. What should we do?
GB.toast("Problem with the firmware transfer. DO NOT REBOOT YOUR MIBAND!!!", Toast.LENGTH_LONG, GB.ERROR);
}
firmwareInfoSent = false;
newFirmware = null;
}
break;
case MiBandService.NOTIFY_FW_CHECK_FAILED:
GB.toast("Problem with the firmware metadata transfer", Toast.LENGTH_LONG, GB.ERROR);
firmwareInfoSent = false;
newFirmware = null;
break;
case MiBandService.NOTIFY_FIRMWARE_UPDATE_SUCCESS:
if (rebootWhenBandReady) {
onReboot();
}
rebootWhenBandReady = false;
break;
case MiBandService.NOTIFY_FIRMWARE_UPDATE_FAILED:
//TODO: the firmware transfer failed, but the miband should be still functional with the old firmware. What should we do?
GB.toast("Problem with the firmware transfer. DO NOT REBOOT YOUR MIBAND!!!", Toast.LENGTH_LONG, GB.ERROR);
rebootWhenBandReady = false;
break;
default:
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
}
}
private void handleDeviceInfo(byte[] value, int status) { private void handleDeviceInfo(byte[] value, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
mDeviceInfo = new DeviceInfo(value); mDeviceInfo = new DeviceInfo(value);
@ -946,7 +998,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
LOG.info("Firmware update progress:" + firmwareProgress + " total len:" + len + " progress:" + (firmwareProgress / len)); LOG.info("Firmware update progress:" + firmwareProgress + " total len:" + len + " progress:" + (firmwareProgress / len));
if (firmwareProgress >= len) { if (firmwareProgress >= len) {
builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC}); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[]{MiBandService.COMMAND_SYNC});
builder.wait(1000);
builder.add(new SetProgressAction("Firmware installation complete", false, 100, getContext())); builder.add(new SetProgressAction("Firmware installation complete", false, 100, getContext()));
} else { } else {
GB.updateInstallNotification("Firmware write failed", false, 0, getContext()); GB.updateInstallNotification("Firmware write failed", false, 0, getContext());