Search in sources :

Example 6 with TransactionBuilder

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

the class MiBandSupport method onSetTime.

@Override
public void onSetTime() {
    try {
        TransactionBuilder builder = performInitialized("Set date and time");
        setCurrentTime(builder);
        builder.queue(getQueue());
    } catch (IOException ex) {
        LOG.error("Unable to set time on MI device", ex);
    }
    //TODO: once we have a common strategy for sending events (e.g. EventHandler), remove this call from here. Meanwhile it does no harm.
    sendCalendarEvents();
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 7 with TransactionBuilder

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

the class MiBandSupport method onEnableRealtimeHeartRateMeasurement.

@Override
public void onEnableRealtimeHeartRateMeasurement(boolean enable) {
    if (supportsHeartRate()) {
        try {
            TransactionBuilder builder = performInitialized("EnableRealtimeHeartRateMeasurement");
            if (enable) {
                builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_HEART_RATE_CONTROL_POINT), stopHeartMeasurementManual);
                builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_HEART_RATE_CONTROL_POINT), startHeartMeasurementContinuous);
            } else {
                builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_HEART_RATE_CONTROL_POINT), stopHeartMeasurementContinuous);
            }
            builder.queue(getQueue());
            enableRealtimeSamplesTimer(enable);
        } catch (IOException ex) {
            LOG.error("Unable to enable realtime heart rate measurement in  MI1S", ex);
        }
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) IOException(java.io.IOException)

Example 8 with TransactionBuilder

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

the class FetchActivityOperation method doPerform.

@Override
protected void doPerform() throws IOException {
    TransactionBuilder builder = performInitialized("fetching activity data");
    getSupport().setLowLatency(builder);
    builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext()));
    BluetoothGattCharacteristic characteristicFetch = getCharacteristic(MiBand2Service.UUID_UNKNOWN_CHARACTERISTIC4);
    builder.notify(characteristicFetch, true);
    BluetoothGattCharacteristic characteristicActivityData = getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_5_ACTIVITY_DATA);
    GregorianCalendar sinceWhen = getLastSuccessfulSyncTime();
    builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE, 0x01 }, getSupport().getTimeBytes(sinceWhen, TimeUnit.MINUTES)));
    // TODO: actually wait for the success-reply
    builder.add(new WaitAction(1000));
    builder.notify(characteristicActivityData, true);
    builder.write(characteristicFetch, new byte[] { MiBand2Service.COMMAND_FETCH_ACTIVITY_DATA });
    builder.queue(getQueue());
}
Also used : GregorianCalendar(java.util.GregorianCalendar) TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) SetDeviceBusyAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WaitAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.WaitAction)

Example 9 with TransactionBuilder

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

the class InitOperation method onCharacteristicChanged.

@Override
public boolean onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    UUID characteristicUUID = characteristic.getUuid();
    if (MiBand2Service.UUID_CHARACTERISTIC_AUTH.equals(characteristicUUID)) {
        try {
            byte[] value = characteristic.getValue();
            getSupport().logMessageContent(value);
            if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_SEND_KEY && value[2] == MiBand2Service.AUTH_SUCCESS) {
                TransactionBuilder builder = createTransactionBuilder("Sending the secret key to the band");
                builder.write(characteristic, requestAuthNumber());
                getSupport().performImmediately(builder);
            } else if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == MiBand2Service.AUTH_SUCCESS) {
                // md5??
                byte[] eValue = handleAESAuth(value, getSecretKey());
                byte[] responseValue = org.apache.commons.lang3.ArrayUtils.addAll(new byte[] { MiBand2Service.AUTH_SEND_ENCRYPTED_AUTH_NUMBER, MiBand2Service.AUTH_BYTE }, eValue);
                TransactionBuilder builder = createTransactionBuilder("Sending the encrypted random key to the band");
                builder.write(characteristic, responseValue);
                getSupport().setCurrentTimeWithService(builder);
                getSupport().performImmediately(builder);
            } else if (value[0] == MiBand2Service.AUTH_RESPONSE && value[1] == MiBand2Service.AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == MiBand2Service.AUTH_SUCCESS) {
                TransactionBuilder builder = createTransactionBuilder("Authenticated, now initialize phase 2");
                builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
                getSupport().requestDeviceInfo(builder);
                getSupport().phase2Initialize(builder);
                getSupport().setInitialized(builder);
                getSupport().performImmediately(builder);
            } else {
                return super.onCharacteristicChanged(gatt, characteristic);
            }
        } catch (Exception e) {
            GB.toast(getContext(), "Error authenticating Mi Band 2", Toast.LENGTH_LONG, GB.ERROR, e);
        }
        return true;
    } else {
        LOG.info("Unhandled characteristic changed: " + characteristicUUID);
        return super.onCharacteristicChanged(gatt, characteristic);
    }
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder) UUID(java.util.UUID) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 10 with TransactionBuilder

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

the class InitOperation method createTransactionBuilder.

private TransactionBuilder createTransactionBuilder(String task) {
    TransactionBuilder builder = getSupport().createTransactionBuilder(task);
    builder.setGattCallback(this);
    return builder;
}
Also used : TransactionBuilder(nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)

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