Search in sources :

Example 1 with SetDeviceStateAction

use of nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction 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 2 with SetDeviceStateAction

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

the class VibratissimoSupport method initializeDevice.

@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
    builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
    requestDeviceInfo(builder);
    setInitialized(builder);
    batteryInfoProfile.requestBatteryInfo(builder);
    return builder;
}
Also used : SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 3 with SetDeviceStateAction

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

the class HPlusSupport method initializeDevice.

@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
    LOG.debug("Initializing");
    builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
    measureCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE);
    ctrlCharacteristic = getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_CONTROL);
    getDevice().setFirmwareVersion("N/A");
    getDevice().setFirmwareVersion2("N/A");
    syncHelper = new HPlusHandlerThread(getDevice(), getContext(), this);
    //Initialize device
    //Sync preferences
    sendUserInfo(builder);
    requestDeviceInfo(builder);
    setInitialized(builder);
    syncHelper.start();
    builder.notify(getCharacteristic(HPlusConstants.UUID_CHARACTERISTIC_MEASURE), true);
    builder.setGattCallback(this);
    builder.notify(measureCharacteristic, true);
    return builder;
}
Also used : SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 4 with SetDeviceStateAction

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

the class InitOperation method doPerform.

@Override
protected void doPerform() throws IOException {
    getSupport().enableNotifications(builder, true);
    if (needsAuth) {
        builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.AUTHENTICATING, getContext()));
        // write key to miband2
        byte[] sendKey = org.apache.commons.lang3.ArrayUtils.addAll(new byte[] { MiBand2Service.AUTH_SEND_KEY, MiBand2Service.AUTH_BYTE }, getSecretKey());
        builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_AUTH), sendKey);
    } else {
        builder.add(new SetDeviceStateAction(getDevice(), GBDevice.State.INITIALIZING, getContext()));
        // get random auth number
        builder.write(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_AUTH), requestAuthNumber());
    }
}
Also used : SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Example 5 with SetDeviceStateAction

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

the class MiBandSupport method initializeDevice.

@Override
protected TransactionBuilder initializeDevice(TransactionBuilder builder) {
    builder.add(new SetDeviceStateAction(getDevice(), State.INITIALIZING, getContext()));
    enableNotifications(builder, true).setLowLatency(builder).readDate(// without reading the data, we get sporadic connection problems, especially directly after turning on BT
    builder).pair(builder).requestDeviceInfo(builder).sendUserInfo(builder).checkAuthenticationNeeded(builder, getDevice()).setWearLocation(builder).setHeartrateSleepSupport(builder).setFitnessGoal(builder).enableFurtherNotifications(builder, true).setCurrentTime(builder).requestBatteryInfo(builder).setHighLatency(builder).setInitialized(builder);
    return builder;
}
Also used : SetDeviceStateAction(nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)

Aggregations

SetDeviceStateAction (nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction)5 IOException (java.io.IOException)1 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 TransactionBuilder (nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder)1