use of com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED 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.MiBandService.MiBandState.AUTHORIZE_FAILED 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);
}
}
use of com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED in project xDrip-plus by jamorham.
the class MiBandService method authPhase.
@SuppressLint("CheckResult")
private void authPhase() {
extendWakeLock(30000);
RxBleConnection connection = I.connection;
if (d)
UserError.Log.d(TAG, "Authorizing");
if (I.connection == null) {
if (d)
UserError.Log.d(TAG, "Cannot enable as connection is null!");
return;
}
String authKey = MiBand.getPersistentAuthKey();
if (MiBand.getMibandType() == MI_BAND4) {
if (authKey.isEmpty()) {
authKey = MiBand.getAuthKey();
if (authKey.isEmpty()) {
authKey = AuthMessages.getAuthCodeFromFilesSystem(MiBand.getMac());
}
if (!AuthMessages.isValidAuthKey(authKey)) {
JoH.static_toast_long("Wrong miband authorization key, please recheck a key and try to reconnect again");
changeState(AUTHORIZE_FAILED);
return;
} else {
MiBand.setAuthKey(authKey);
}
}
}
if (!AuthMessages.isValidAuthKey(authKey)) {
authKey = "";
}
if (d)
UserError.Log.d(TAG, "authKey: " + authKey);
authorisation = new AuthMessages(MiBand.getMibandType(), authKey);
if (d)
UserError.Log.d(TAG, "localKey: " + JoH.bytesToHex(authorisation.getLocalKey()));
authSubscription = new Subscription(connection.setupNotification(authorisation.getCharacteristicUUID()).timeout(20, // WARN
TimeUnit.SECONDS).doOnNext(notificationObservable -> {
if (d)
UserError.Log.d(TAG, "Notification for auth enabled");
if (MiBand.isAuthenticated()) {
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote getAuthKeyRequest: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not getAuthKeyRequest: " + throwable);
});
} else {
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthCommand()).subscribe(characteristicValue -> {
UserError.Log.d(TAG, "Wrote getAuthCommand, got: " + JoH.bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Could not write getAuthCommand: " + throwable);
});
}
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received auth notification bytes: " + bytesToHex(bytes));
ProcessAuthCommands(connection, bytes);
// changeNextState();
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
} else if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
} else if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected while enabling notifications");
} else if (throwable instanceof TimeoutException) {
// check if it is normal timeout
if (!MiBand.isAuthenticated()) {
String errorText = "MiBand authentication failed due to authentication timeout. When your Mi Band vibrates and blinks, tap it a few times in a row.";
UserError.Log.d(TAG, errorText);
JoH.static_toast_long(errorText);
}
}
if (authSubscription != null) {
authSubscription.unsubscribe();
}
changeState(CLOSE);
}));
}
use of com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED in project xDrip by NightscoutFoundation.
the class MiBandService method authPhase.
@SuppressLint("CheckResult")
private void authPhase() {
extendWakeLock(30000);
RxBleConnection connection = I.connection;
if (d)
UserError.Log.d(TAG, "Authorizing");
if (I.connection == null) {
if (d)
UserError.Log.d(TAG, "Cannot enable as connection is null!");
return;
}
String authKey = MiBand.getPersistentAuthKey();
if (MiBand.getMibandType() == MI_BAND4) {
if (authKey.isEmpty()) {
authKey = MiBand.getAuthKey();
if (authKey.isEmpty()) {
authKey = AuthMessages.getAuthCodeFromFilesSystem(MiBand.getMac());
}
if (!AuthMessages.isValidAuthKey(authKey)) {
JoH.static_toast_long("Wrong miband authorization key, please recheck a key and try to reconnect again");
changeState(AUTHORIZE_FAILED);
return;
} else {
MiBand.setAuthKey(authKey);
}
}
}
if (!AuthMessages.isValidAuthKey(authKey)) {
authKey = "";
}
if (d)
UserError.Log.d(TAG, "authKey: " + authKey);
authorisation = new AuthMessages(MiBand.getMibandType(), authKey);
if (d)
UserError.Log.d(TAG, "localKey: " + JoH.bytesToHex(authorisation.getLocalKey()));
authSubscription = new Subscription(connection.setupNotification(authorisation.getCharacteristicUUID()).timeout(20, // WARN
TimeUnit.SECONDS).doOnNext(notificationObservable -> {
if (d)
UserError.Log.d(TAG, "Notification for auth enabled");
if (MiBand.isAuthenticated()) {
// get random key from band
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthKeyRequest()).subscribe(val -> {
if (d)
UserError.Log.d(TAG, "Wrote getAuthKeyRequest: " + JoH.bytesToHex(val));
}, throwable -> {
UserError.Log.e(TAG, "Could not getAuthKeyRequest: " + throwable);
});
} else {
connection.writeCharacteristic(authorisation.getCharacteristicUUID(), authorisation.getAuthCommand()).subscribe(characteristicValue -> {
UserError.Log.d(TAG, "Wrote getAuthCommand, got: " + JoH.bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Could not write getAuthCommand: " + throwable);
});
}
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received auth notification bytes: " + bytesToHex(bytes));
ProcessAuthCommands(connection, bytes);
// changeNextState();
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
} else if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
} else if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected while enabling notifications");
} else if (throwable instanceof TimeoutException) {
// check if it is normal timeout
if (!MiBand.isAuthenticated()) {
String errorText = "MiBand authentication failed due to authentication timeout. When your Mi Band vibrates and blinks, tap it a few times in a row.";
UserError.Log.d(TAG, errorText);
JoH.static_toast_long(errorText);
}
}
if (authSubscription != null) {
authSubscription.unsubscribe();
}
changeState(CLOSE);
}));
}
Aggregations