Search in sources :

Example 11 with TransactionBuilder

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

the class UpdateFirmwareOperation method sendChecksum.

private void sendChecksum(Mi2FirmwareInfo firmwareInfo) throws IOException {
    TransactionBuilder builder = performInitialized("send firmware checksum");
    int crc16 = firmwareInfo.getCrc16();
    byte[] bytes = BLETypeConversions.fromUint16(crc16);
    builder.write(fwCControlChar, new byte[] { MiBand2Service.COMMAND_FIRMWARE_CHECKSUM, bytes[0], bytes[1] });
    builder.queue(getQueue());
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

Example 12 with TransactionBuilder

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

the class FetchActivityOperation method bufferActivityData.

/**
     * Method to store temporarily the activity data values got from the Mi Band.
     * <p/>
     * Since we expect chunks of 20 bytes each, we do not store the received bytes it the length is different.
     *
     * @param value
     */
private void bufferActivityData(byte[] value) {
    /*
        if (scheduledTask != null) {
            scheduledTask.cancel(true);
        }
*/
    if (activityStruct.hasRoomFor(value)) {
        if (activityStruct.isValidData(value)) {
            activityStruct.buffer(value);
            /*                scheduledTask = scheduleTaskExecutor.schedule(new Runnable() {
                    @Override
                    public void run() {
                        GB.toast(getContext(), "chiederei " + activityStruct.activityDataTimestampToAck + "   "+ activityStruct.activityDataUntilNextHeader, Toast.LENGTH_LONG, GB.ERROR);
                        //sendAckDataTransfer(activityStruct.activityDataTimestampToAck, activityStruct.activityDataUntilNextHeader);
                        LOG.debug("runnable called");
                    }
                }, 10l, TimeUnit.SECONDS);
*/
            if (activityStruct.isBufferFull()) {
                flushActivityDataHolder();
            }
        } else {
            // the length of the chunk is not what we expect. We need to make sense of this data
            LOG.warn("GOT UNEXPECTED ACTIVITY DATA WITH LENGTH: " + value.length + ", EXPECTED LENGTH: " + activityStruct.activityDataRemainingBytes);
            getSupport().logMessageContent(value);
        }
    } else {
        GB.toast(getContext(), "error buffering activity data: remaining bytes: " + activityStruct.activityDataRemainingBytes + ", received: " + value.length, Toast.LENGTH_LONG, GB.ERROR);
        try {
            TransactionBuilder builder = performInitialized("send stop sync data");
            builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), new byte[] { MiBandService.COMMAND_STOP_SYNC_DATA });
            builder.queue(getQueue());
            GB.updateTransferNotification("Data transfer failed", false, 0, getContext());
            handleActivityFetchFinish();
        } catch (IOException e) {
            LOG.error("error stopping activity sync", e);
        }
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 13 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 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 14 with TransactionBuilder

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

the class MiBand2Support method onSendConfiguration.

@Override
public void onSendConfiguration(String config) {
    TransactionBuilder builder;
    try {
        builder = performInitialized("Sending configuration for option: " + config);
        switch(config) {
            case MiBandConst.PREF_MI2_DATEFORMAT:
                setDateDisplay(builder);
                break;
            case MiBandConst.PREF_MI2_ACTIVATE_DISPLAY_ON_LIFT:
                setActivateDisplayOnLiftWrist(builder);
                break;
            case ActivityUser.PREF_USER_STEPS_GOAL:
                setFitnessGoal(builder);
                break;
        }
        builder.queue(getQueue());
    } catch (IOException e) {
        GB.toast("Error setting configuration", Toast.LENGTH_LONG, GB.ERROR, e);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 15 with TransactionBuilder

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

the class MiBand2Support method onSetAlarms.

@Override
public void onSetAlarms(ArrayList<? extends Alarm> alarms) {
    try {
        BluetoothGattCharacteristic characteristic = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_3_CONFIGURATION);
        TransactionBuilder builder = performInitialized("Set alarm");
        boolean anyAlarmEnabled = false;
        for (Alarm alarm : alarms) {
            anyAlarmEnabled |= alarm.isEnabled();
            queueAlarm(alarm, builder, characteristic);
        }
        builder.queue(getQueue());
        if (anyAlarmEnabled) {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_ok), Toast.LENGTH_SHORT, GB.INFO);
        } else {
            GB.toast(getContext(), getContext().getString(R.string.user_feedback_all_alarms_disabled), Toast.LENGTH_SHORT, GB.INFO);
        }
    } catch (IOException ex) {
        GB.toast(getContext(), getContext().getString(R.string.user_feedback_miband_set_alarms_failed), Toast.LENGTH_LONG, GB.ERROR, ex);
    }
}
Also used : Alarm(nodomain.freeyourgadget.gadgetbridge.model.Alarm) GBAlarm(nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

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