use of com.polidea.rxandroidble2.exceptions.BleDisconnectedException in project xDrip by NightscoutFoundation.
the class BlueJayService method sendOtaChunk.
boolean sendOtaChunk(final UUID uuid, final byte[] bytes) {
if (I.connection == null || !I.isConnected)
return false;
I.connection.writeCharacteristic(uuid, bytes).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).subscribe(characteristicValue -> {
if (D)
UserError.Log.d(TAG, "Wrote record request request: " + bytesToHex(characteristicValue));
busy = false;
}, throwable -> {
UserError.Log.e(TAG, "Failed to write record request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
} else {
if (throwable instanceof BleDisconnectedException) {
changeState(CLOSE);
}
UserError.Log.d(TAG, "Throwable in Record End write: " + throwable);
}
});
// only that we didn't fail in setup
return true;
}
use of com.polidea.rxandroidble2.exceptions.BleDisconnectedException in project xDrip by NightscoutFoundation.
the class InPenService method getRecords.
private void getRecords(final int firstIndex, final int lastIndex) {
final int numberOfRecords = lastIndex - firstIndex;
if (numberOfRecords > 30) {
I.connection.writeCharacteristic(KEEPALIVE, new KeepAliveTx().getBytes()).subscribe(value -> {
UserError.Log.d(TAG, "Wrote keep alive for " + numberOfRecords);
}, throwable -> {
UserError.Log.d(TAG, "Got exception in keep alive" + throwable);
});
}
final RecordTx packet = new RecordTx(firstIndex, lastIndex);
UserError.Log.d(TAG, "getRecords called, loading: " + firstIndex + " to " + lastIndex);
I.connection.setupIndication(RECORD_INDICATE).doOnNext(notificationObservable -> {
I.connection.writeCharacteristic(RECORD_START, packet.startBytes()).subscribe(valueS -> {
UserError.Log.d(TAG, "Wrote record start: " + bytesToHex(valueS));
I.connection.writeCharacteristic(RECORD_END, packet.endBytes()).subscribe(valueE -> {
UserError.Log.d(TAG, "Wrote record end: " + bytesToHex(valueE));
I.connection.writeCharacteristic(RECORD_REQUEST, packet.triggerBytes()).subscribe(characteristicValue -> {
if (D)
UserError.Log.d(TAG, "Wrote record request request: " + bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Failed to write record request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
}
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record End write: " + throwable);
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Start write: " + throwable);
// throws BleGattCharacteristicException status = 128 for "no resources" eg nothing matches
});
}).flatMap(notificationObservable -> notificationObservable).timeout(120, TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(bytes -> {
records.add(bytes);
UserError.Log.d(TAG, "INDICATE INDICATE: " + HexDump.dumpHexString(bytes));
}, throwable -> {
if (!(throwable instanceof OperationSuccess)) {
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected when waiting to receive indication: " + throwable);
} else {
UserError.Log.e(TAG, "Error receiving indication: " + throwable);
}
Inevitable.task("check-records-queue", 100, this::processRecordsQueue);
}
});
}
use of com.polidea.rxandroidble2.exceptions.BleDisconnectedException 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);
}));
}
use of com.polidea.rxandroidble2.exceptions.BleDisconnectedException in project xDrip by NightscoutFoundation.
the class MiBandService method installWatchface.
@SuppressLint("CheckResult")
private void installWatchface() {
// TODO decrease display brightness before uploading watchface to minimize battery consumption
RxBleConnection connection = I.connection;
if (d)
UserError.Log.d(TAG, "Install WatchFace");
if (I.connection == null) {
if (d)
UserError.Log.d(TAG, "Cannot enable as connection is null!");
return;
}
try {
WatchFaceGenerator wfGen = new WatchFaceGenerator(getBaseContext().getAssets());
byte[] fwArray = wfGen.genWatchFace();
if (fwArray == null || fwArray.length == 0) {
resetFirmwareState(false, "Empty image");
return;
}
firmware = new FirmwareOperations(fwArray);
} catch (Exception e) {
resetFirmwareState(false, "FirmwareOperations error " + e.getMessage());
return;
}
if (d)
UserError.Log.d(TAG, "Begin uploading Watchface, lenght: " + firmware.getSize());
if (d)
UserError.Log.d(TAG, "Requesting to enable notifications for installWatchface");
watchfaceSubscription = new Subscription(connection.setupNotification(firmware.getFirmwareCharacteristicUUID()).timeout(400, // WARN
TimeUnit.SECONDS).doOnNext(notificationObservable -> {
if (d)
UserError.Log.d(TAG, "Notification for firmware enabled");
firmware.nextSequence();
processFirmwareCommands(null, true);
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
// incoming notifications
if (d)
UserError.Log.d(TAG, "Received firmware notification bytes: " + bytesToHex(bytes));
processFirmwareCommands(bytes, false);
}, throwable -> {
UserError.Log.d(TAG, "Throwable in firmware 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) {
UserError.Log.d(TAG, "Timeout");
}
resetFirmwareState(false);
}));
}
use of com.polidea.rxandroidble2.exceptions.BleDisconnectedException in project xDrip-plus by jamorham.
the class InPenService method getRecords.
private void getRecords(final int firstIndex, final int lastIndex) {
final int numberOfRecords = lastIndex - firstIndex;
if (numberOfRecords > 30) {
I.connection.writeCharacteristic(KEEPALIVE, new KeepAliveTx().getBytes()).subscribe(value -> {
UserError.Log.d(TAG, "Wrote keep alive for " + numberOfRecords);
}, throwable -> {
UserError.Log.d(TAG, "Got exception in keep alive" + throwable);
});
}
final RecordTx packet = new RecordTx(firstIndex, lastIndex);
UserError.Log.d(TAG, "getRecords called, loading: " + firstIndex + " to " + lastIndex);
I.connection.setupIndication(RECORD_INDICATE).doOnNext(notificationObservable -> {
I.connection.writeCharacteristic(RECORD_START, packet.startBytes()).subscribe(valueS -> {
UserError.Log.d(TAG, "Wrote record start: " + bytesToHex(valueS));
I.connection.writeCharacteristic(RECORD_END, packet.endBytes()).subscribe(valueE -> {
UserError.Log.d(TAG, "Wrote record end: " + bytesToHex(valueE));
I.connection.writeCharacteristic(RECORD_REQUEST, packet.triggerBytes()).subscribe(characteristicValue -> {
if (D)
UserError.Log.d(TAG, "Wrote record request request: " + bytesToHex(characteristicValue));
}, throwable -> {
UserError.Log.e(TAG, "Failed to write record request: " + throwable);
if (throwable instanceof BleGattCharacteristicException) {
final int status = ((BleGattCharacteristicException) throwable).getStatus();
UserError.Log.e(TAG, "Got status message: " + Helper.getStatusName(status));
}
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record End write: " + throwable);
});
}, throwable -> {
UserError.Log.d(TAG, "Throwable in Record Start write: " + throwable);
// throws BleGattCharacteristicException status = 128 for "no resources" eg nothing matches
});
}).flatMap(notificationObservable -> notificationObservable).timeout(120, TimeUnit.SECONDS).observeOn(Schedulers.newThread()).subscribe(bytes -> {
records.add(bytes);
UserError.Log.d(TAG, "INDICATE INDICATE: " + HexDump.dumpHexString(bytes));
}, throwable -> {
if (!(throwable instanceof OperationSuccess)) {
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected when waiting to receive indication: " + throwable);
} else {
UserError.Log.e(TAG, "Error receiving indication: " + throwable);
}
Inevitable.task("check-records-queue", 100, this::processRecordsQueue);
}
});
}
Aggregations