Search in sources :

Example 1 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 2 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 3 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)

Example 4 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 5 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)

Aggregations

TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)279 IOException (java.io.IOException)218 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)17 Alarm (nodomain.freeyourgadget.gadgetbridge.model.Alarm)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 GregorianCalendar (java.util.GregorianCalendar)11 UUID (java.util.UUID)11 Calendar (java.util.Calendar)10 SetDeviceBusyAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceBusyAction)10 ByteBuffer (java.nio.ByteBuffer)8 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)8 ServerTransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.ServerTransactionBuilder)7 SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)7 GBException (nodomain.freeyourgadget.gadgetbridge.GBException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 SetProgressAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction)4 AlertNotificationProfile (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.AlertNotificationProfile)4 NewAlert (nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.alertnotification.NewAlert)4 GBPrefs (nodomain.freeyourgadget.gadgetbridge.util.GBPrefs)4 InvalidKeyException (java.security.InvalidKeyException)3