Search in sources :

Example 36 with TransactionBuilder

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

the class HPlusSupport method onSetTime.

@Override
public void onSetTime() {
    TransactionBuilder builder = new TransactionBuilder("time");
    setCurrentDate(builder);
    setCurrentTime(builder);
    builder.queue(getQueue());
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Example 37 with TransactionBuilder

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

the class UpdateFirmwareOperation method sendFwInfo.

public boolean sendFwInfo() {
    try {
        TransactionBuilder builder = performInitialized("send firmware info");
        //                getSupport().setLowLatency(builder);
        builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.updating_firmware), getContext()));
        int fwSize = getFirmwareInfo().getSize();
        byte[] sizeBytes = BLETypeConversions.fromUint24(fwSize);
        int arraySize = 4;
        boolean isFirmwareCode = getFirmwareInfo().getFirmwareType() == FirmwareType.FIRMWARE;
        if (!isFirmwareCode) {
            arraySize++;
        }
        byte[] bytes = new byte[arraySize];
        int i = 0;
        bytes[i++] = MiBand2Service.COMMAND_FIRMWARE_INIT;
        bytes[i++] = sizeBytes[0];
        bytes[i++] = sizeBytes[1];
        bytes[i++] = sizeBytes[2];
        if (!isFirmwareCode) {
            bytes[i++] = getFirmwareInfo().getFirmwareType().getValue();
        }
        builder.write(fwCControlChar, bytes);
        builder.queue(getQueue());
        return true;
    } catch (IOException e) {
        LOG.error("Error sending firmware info: " + e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) SetDeviceBusyAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction) IOException(java.io.IOException)

Example 38 with TransactionBuilder

use of nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder 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)

Example 39 with TransactionBuilder

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

the class MiBandSupport method doInitialize.

private void doInitialize() {
    try {
        TransactionBuilder builder = performInitialized("just initializing after authentication");
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to initialize device after authentication", ex);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 40 with TransactionBuilder

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

the class MiBandSupport method onReboot.

@Override
public void onReboot() {
    try {
        TransactionBuilder builder = performInitialized("Reboot");
        builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), reboot);
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to reboot MI", ex);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Aggregations

TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)50 IOException (java.io.IOException)34 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)7 GregorianCalendar (java.util.GregorianCalendar)4 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)4 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)4 Calendar (java.util.Calendar)3 GBAlarm (nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm)3 SetDeviceBusyAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction)3 VibrationProfile (nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile)2 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)2 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UUID (java.util.UUID)1 BadPaddingException (javax.crypto.BadPaddingException)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 CalendarEvents (nodomain.freeyourgadget.gadgetbridge.model.CalendarEvents)1 SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)1 WaitAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction)1