use of com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER in project xDrip by NightscoutFoundation.
the class MiBandService method ProcessAuthCommands.
@SuppressLint("CheckResult")
private void ProcessAuthCommands(RxBleConnection connection, byte[] value) {
if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_KEY && (value[2] & 0x0f) == AUTH_SUCCESS) {
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ1: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ1: " + throwable);
});
} else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
byte[] tmpValue = Arrays.copyOfRange(value, 3, 19);
try {
byte[] authReply = authorisation.calculateAuthReply(tmpValue);
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authReply).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ2: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ2: " + throwable);
});
} catch (Exception e) {
JoH.static_toast_long(e.getMessage());
UserError.Log.e(TAG, (e.getMessage()));
changeState(AUTHORIZE_FAILED);
}
} else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
isNeedToAuthenticate = false;
if (MiBand.getPersistentAuthMac().isEmpty()) {
MiBand.setPersistentAuthMac(MiBand.getMac());
MiBand.setPersistentAuthKey(JoH.bytesToHex(authorisation.getLocalKey()), MiBand.getPersistentAuthMac());
String msg = "MiBand was successfully authenticated";
JoH.static_toast_long(msg);
UserError.Log.e(TAG, msg);
}
if (authSubscription != null) {
authSubscription.unsubscribe();
}
changeNextState();
} else if (value[0] == AUTH_RESPONSE && (((value[2] & 0x0f) == AUTH_FAIL) || (value[2] == AUTH_MIBAND4_FAIL) || (value[2] == AUTH_MIBAND4_CODE_FAIL))) {
MiBand.setPersistentAuthKey("", MiBand.getPersistentAuthMac());
if (authSubscription != null) {
authSubscription.unsubscribe();
}
String msg = "Cannot authorize miband, please recheck Auth code";
JoH.static_toast_long(msg);
UserError.Log.e(TAG, msg);
changeState(AUTHORIZE_FAILED);
}
}
use of com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER in project xDrip-plus by jamorham.
the class MiBandService method ProcessAuthCommands.
@SuppressLint("CheckResult")
private void ProcessAuthCommands(RxBleConnection connection, byte[] value) {
if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_KEY && (value[2] & 0x0f) == AUTH_SUCCESS) {
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ1: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ1: " + throwable);
});
} else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
byte[] tmpValue = Arrays.copyOfRange(value, 3, 19);
try {
byte[] authReply = authorisation.calculateAuthReply(tmpValue);
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authReply).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote OPCODE_AUTH_REQ2: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not write OPCODE_AUTH_REQ2: " + throwable);
});
} catch (Exception e) {
JoH.static_toast_long(e.getMessage());
UserError.Log.e(TAG, (e.getMessage()));
changeState(AUTHORIZE_FAILED);
}
} else if (value[0] == AUTH_RESPONSE && (value[1] & 0x0f) == AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
isNeedToAuthenticate = false;
if (MiBand.getPersistentAuthMac().isEmpty()) {
MiBand.setPersistentAuthMac(MiBand.getMac());
MiBand.setPersistentAuthKey(JoH.bytesToHex(authorisation.getLocalKey()), MiBand.getPersistentAuthMac());
String msg = "MiBand was successfully authenticated";
JoH.static_toast_long(msg);
UserError.Log.e(TAG, msg);
}
if (authSubscription != null) {
authSubscription.unsubscribe();
}
changeNextState();
} else if (value[0] == AUTH_RESPONSE && (((value[2] & 0x0f) == AUTH_FAIL) || (value[2] == AUTH_MIBAND4_FAIL) || (value[2] == AUTH_MIBAND4_CODE_FAIL))) {
MiBand.setPersistentAuthKey("", MiBand.getPersistentAuthMac());
if (authSubscription != null) {
authSubscription.unsubscribe();
}
String msg = "Cannot authorize miband, please recheck Auth code";
JoH.static_toast_long(msg);
UserError.Log.e(TAG, msg);
changeState(AUTHORIZE_FAILED);
}
}
Aggregations