use of com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException in project xDrip by NightscoutFoundation.
the class MiBandService method enableHeartRateNotification.
private void enableHeartRateNotification() {
if (MiBandEntry.isNeedToCollectHR()) {
if (notifSubscriptionHeartRateMeasurement != null)
return;
} else {
if (notifSubscriptionHeartRateMeasurement != null) {
notifSubscriptionHeartRateMeasurement.unsubscribe();
notifSubscriptionHeartRateMeasurement = null;
return;
}
}
if (d)
UserError.Log.d(TAG, "Requesting to enable HR notifications");
notifSubscriptionHeartRateMeasurement = new Subscription(I.connection.setupNotification(Const.UUID_CHAR_HEART_RATE_MEASUREMENT).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received HR notification bytes: " + bytesToHex(bytes));
handleHeartrate(bytes);
}, throwable -> {
notifSubscriptionHeartRateMeasurement.unsubscribe();
notifSubscriptionHeartRateMeasurement = null;
UserError.Log.d(TAG, "HR Throwable in Record Notification: " + throwable);
if (throwable instanceof BleCharacteristicNotFoundException) {
UserError.Log.d(TAG, "HR Characteristic not found for notification");
} else {
UserError.Log.d(TAG, "HR Disconnected exception");
}
}));
}
use of com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException in project xDrip by NightscoutFoundation.
the class MiBandService method enableNotification.
@SuppressLint("CheckResult")
private void enableNotification() {
if (d)
UserError.Log.d(TAG, "enableNotifications called");
if (I.connection == null) {
if (d)
UserError.Log.d(TAG, "Cannot enable as connection is null!");
return;
}
enableHeartRateNotification();
if (I.isNotificationEnabled) {
if (d)
UserError.Log.d(TAG, "Notifications already enabled");
changeNextState();
return;
}
if (notifSubscriptionDeviceEvent != null) {
notifSubscriptionDeviceEvent.unsubscribe();
}
if (notifSubscriptionHeartRateMeasurement != null) {
notifSubscriptionHeartRateMeasurement.unsubscribe();
}
if (d)
UserError.Log.d(TAG, "Requesting to enable device event notifications");
I.connection.requestMtu(PREFERRED_MTU_SIZE).subscribe();
notifSubscriptionDeviceEvent = new Subscription(I.connection.setupNotification(Const.UUID_CHARACTERISTIC_DEVICEEVENT).doOnNext(notificationObservable -> {
I.isNotificationEnabled = true;
changeNextState();
}).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received device notification bytes: " + bytesToHex(bytes));
handleDeviceEvent(bytes);
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
I.isNotificationEnabled = false;
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
changeNextState();
} else {
UserError.Log.d(TAG, "Disconnected exception");
isNeedToAuthenticate = true;
messageQueue.clear();
changeState(CLOSE);
}
}));
}
use of com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException in project xDrip by NightscoutFoundation.
the class BlueJayService method sendTime.
// Not using packet queue due to reactive time sensitive nature
private void sendTime() {
final String func = "SetTime";
final SetTimeTx outbound = new SetTimeTx();
UserError.Log.d(TAG, "Outbound: " + bytesToHex(outbound.getBytes()));
I.connection.writeCharacteristic(THINJAM_WRITE, outbound.getBytes()).subscribe(response -> {
SetTimeTx reply = new SetTimeTx(response);
if (D)
UserError.Log.d(TAG, func + " response: " + bytesToHex(response) + " " + reply.toS());
UserError.Log.e(TAG, "Time difference with watch: " + ((outbound.getTimestamp() - reply.getTimestamp()) / 1000d));
changeNextState();
}, throwable -> {
UserError.Log.e(TAG, "Failed to write " + func + " request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
} else {
UserError.Log.d(TAG, "Throwable in " + func + " " + throwable);
if (throwable instanceof BleCharacteristicNotFoundException) {
UserError.Log.d(TAG, "Assuming wrong firmware version");
changeNextState();
} else {
changeState(CLOSE);
}
}
});
}
use of com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException in project xDrip-plus by jamorham.
the class MiBandService method enableNotification.
@SuppressLint("CheckResult")
private void enableNotification() {
if (d)
UserError.Log.d(TAG, "enableNotifications called");
if (I.connection == null) {
if (d)
UserError.Log.d(TAG, "Cannot enable as connection is null!");
return;
}
enableHeartRateNotification();
if (I.isNotificationEnabled) {
if (d)
UserError.Log.d(TAG, "Notifications already enabled");
changeNextState();
return;
}
if (notifSubscriptionDeviceEvent != null) {
notifSubscriptionDeviceEvent.unsubscribe();
}
if (notifSubscriptionHeartRateMeasurement != null) {
notifSubscriptionHeartRateMeasurement.unsubscribe();
}
if (d)
UserError.Log.d(TAG, "Requesting to enable device event notifications");
I.connection.requestMtu(PREFERRED_MTU_SIZE).subscribe();
notifSubscriptionDeviceEvent = new Subscription(I.connection.setupNotification(Const.UUID_CHARACTERISTIC_DEVICEEVENT).doOnNext(notificationObservable -> {
I.isNotificationEnabled = true;
changeNextState();
}).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received device notification bytes: " + bytesToHex(bytes));
handleDeviceEvent(bytes);
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Notification: " + throwable);
I.isNotificationEnabled = false;
if (throwable instanceof BleCharacteristicNotFoundException) {
// maybe legacy - ignore for now but needs better handling
UserError.Log.d(TAG, "Characteristic not found for notification");
changeNextState();
} else {
UserError.Log.d(TAG, "Disconnected exception");
isNeedToAuthenticate = true;
messageQueue.clear();
changeState(CLOSE);
}
}));
}
use of com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException 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);
}));
}
Aggregations