Search in sources :

Example 1 with SetProgressAction

use of nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation method sendFirmwareData.

/**
     * Method that uploads a firmware (fwbytes) to the Mi Band.
     * The firmware has to be split into chunks of 20 bytes each, and periodically a COMMAND_SYNC command has to be issued to the Mi Band.
     * <p/>
     * The Mi Band will send a notification after receiving this data to confirm if the firmware looks good to it.
     *
     * @param fwbytes
     * @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
     * @see MiBandSupport#handleNotificationNotif
     */
private boolean sendFirmwareData(byte[] fwbytes) {
    int len = fwbytes.length;
    final int packetLength = 20;
    int packets = len / packetLength;
    BluetoothGattCharacteristic characteristicControlPoint = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
    BluetoothGattCharacteristic characteristicFWData = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_FIRMWARE_DATA);
    try {
        // going from 0 to len
        int firmwareProgress = 0;
        TransactionBuilder builder = performInitialized("send firmware packet");
        if (prefs.getBoolean("mi_low_latency_fw_update", true)) {
            getSupport().setLowLatency(builder);
        }
        for (int i = 0; i < packets; i++) {
            byte[] fwChunk = Arrays.copyOfRange(fwbytes, i * packetLength, i * packetLength + packetLength);
            builder.write(characteristicFWData, fwChunk);
            firmwareProgress += packetLength;
            int progressPercent = (int) ((((float) firmwareProgress) / len) * 100);
            if ((i > 0) && (i % 50 == 0)) {
                builder.write(characteristicControlPoint, new byte[] { MiBandService.COMMAND_SYNC });
                builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, progressPercent, getContext()));
            }
        }
        if (firmwareProgress < len) {
            byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len);
            builder.write(characteristicFWData, lastChunk);
            firmwareProgress = len;
        }
        builder.write(characteristicControlPoint, new byte[] { MiBandService.COMMAND_SYNC });
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send fw to MI", ex);
        GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_firmware_not_sent), false, 0, getContext());
        return false;
    }
    return true;
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) SetProgressAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)

Example 2 with SetProgressAction

use of nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction in project Gadgetbridge by Freeyourgadget.

the class UpdateFirmwareOperation method sendFirmwareData.

/**
     * Method that uploads a firmware (fwbytes) to the Mi Band.
     * The firmware has to be split into chunks of 20 bytes each, and periodically a COMMAND_SYNC command has to be issued to the Mi Band.
     * <p/>
     * The Mi Band will send a notification after receiving this data to confirm if the firmware looks good to it.
     *
     * @param info
     * @return whether the transfer succeeded or not. Only a BT layer exception will cause the transmission to fail.
     * @see #handleNotificationNotif
     */
private boolean sendFirmwareData(Mi2FirmwareInfo info) {
    byte[] fwbytes = info.getBytes();
    int len = fwbytes.length;
    final int packetLength = 20;
    int packets = len / packetLength;
    try {
        // going from 0 to len
        int firmwareProgress = 0;
        TransactionBuilder builder = performInitialized("send firmware packet");
        if (prefs.getBoolean("mi_low_latency_fw_update", true)) {
            getSupport().setLowLatency(builder);
        }
        builder.write(fwCControlChar, new byte[] { MiBand2Service.COMMAND_FIRMWARE_START_DATA });
        for (int i = 0; i < packets; i++) {
            byte[] fwChunk = Arrays.copyOfRange(fwbytes, i * packetLength, i * packetLength + packetLength);
            builder.write(fwCDataChar, fwChunk);
            firmwareProgress += packetLength;
            int progressPercent = (int) ((((float) firmwareProgress) / len) * 100);
            if ((i > 0) && (i % 100 == 0)) {
                builder.write(fwCControlChar, new byte[] { MiBand2Service.COMMAND_FIRMWARE_UPDATE_SYNC });
                builder.add(new SetProgressAction(getContext().getString(R.string.updatefirmwareoperation_update_in_progress), true, progressPercent, getContext()));
            }
        }
        if (firmwareProgress < len) {
            byte[] lastChunk = Arrays.copyOfRange(fwbytes, packets * packetLength, len);
            builder.write(fwCDataChar, lastChunk);
            firmwareProgress = len;
        }
        builder.write(fwCControlChar, new byte[] { MiBand2Service.COMMAND_FIRMWARE_UPDATE_SYNC });
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to send fw to MI 2", ex);
        GB.updateInstallNotification(getContext().getString(R.string.updatefirmwareoperation_firmware_not_sent), false, 0, getContext());
        return false;
    }
    return true;
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) SetProgressAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)

Aggregations

IOException (java.io.IOException)2 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)2 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)2 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)1