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);
}
}
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;
}
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;
}
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());
}
}
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;
}
Aggregations