use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
the class MedtrumCollectionService method enable_features_and_listen.
private synchronized void enable_features_and_listen() {
UserError.Log.d(TAG, "enable features - enter");
stopListening();
if (connection != null) {
notificationSubscription = new Subscription(connection.setupNotification(Const.CGM_CHARACTERISTIC_NOTIFY).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-n", 60000);
try {
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
lastInteractionTime = JoH.tsl();
setFailOverTimer();
lastAnnex = new AnnexARx(bytes);
UserError.Log.d(TAG, "Notification: " + lastAnnex.toS());
createRecordFromAnnexData(lastAnnex);
backFillIfNeeded(lastAnnex);
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "notification throwable: " + throwable);
}));
final InboundStream inboundStream = new InboundStream();
indicationSubscription = new Subscription(connection.setupIndication(CGM_CHARACTERISTIC_INDICATE).timeout(LISTEN_STASIS_SECONDS, // WARN
TimeUnit.SECONDS).observeOn(Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Indications enabled");
sendTx(new AuthTx(serial));
}).flatMap(notificationObservable -> notificationObservable).subscribe(bytes -> {
final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-receive-i", 60000);
try {
UserError.Log.d(TAG, "Received indication bytes: " + JoH.bytesToHex(bytes));
if (inboundStream.hasSomeData() && msSince(lastInteractionTime) > Constants.SECOND_IN_MS * 10) {
UserError.Log.d(TAG, "Resetting stream due to earlier timeout");
}
lastInteractionTime = JoH.tsl();
inboundStream.push(bytes);
if (!checkAndProcessInboundStream(inboundStream)) {
Inevitable.task("mt-reset-stream-no-data", 3000, () -> {
if (inboundStream.hasSomeData()) {
UserError.Log.d(TAG, "Resetting stream as incomplete after 3s");
inboundStream.reset();
}
});
}
} finally {
JoH.releaseWakeLock(wl);
}
}, throwable -> {
UserError.Log.d(TAG, "indication throwable: " + throwable);
}));
} else {
UserError.Log.e(TAG, "Connection null when trying to set notifications");
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
the class Ob1G5CollectionService method connect_to_device.
private synchronized void connect_to_device(boolean auto) {
if ((state == CONNECT) || (state == CONNECT_NOW)) {
// TODO check mac
if (transmitterMAC == null) {
tryLoadingSavedMAC();
}
final String localTransmitterMAC = transmitterMAC;
if (localTransmitterMAC != null) {
msg("Connect request");
if (state == CONNECT_NOW) {
if (connection_linger != null)
JoH.releaseWakeLock(connection_linger);
connection_linger = JoH.getWakeLock("jam-g5-pconnect", 60000);
}
if (d)
UserError.Log.d(TAG, "Local bonding state: " + (isDeviceLocallyBonded() ? "BONDED" : "NOT Bonded"));
stopConnect();
try {
bleDevice = rxBleClient.getBleDevice(localTransmitterMAC);
// / / Listen for connection state changes
stateSubscription = new Subscription(bleDevice.observeConnectionStateChanges().subscribeOn(Schedulers.io()).subscribe(this::onConnectionStateChange, throwable -> {
UserError.Log.wtf(TAG, "Got Error from state subscription: " + throwable);
}));
last_connect_started = JoH.tsl();
// Attempt to establish a connection // TODO does this need different connection timeout for auto vs normal?
connectionSubscription = new Subscription(bleDevice.establishConnection(auto).timeout(7, TimeUnit.MINUTES).subscribeOn(Schedulers.io()).subscribe(this::onConnectionReceived, this::onConnectionFailure));
} catch (IllegalArgumentException e) {
UserError.Log.e(TAG, "Caught IllegalArgument Exception: " + e + " retry on next run");
// TODO if this is due to concurrent access then changing state again may be a bad idea
state = SCAN;
// note backoff
backoff_automata();
}
} else {
UserError.Log.wtf(TAG, "No transmitter mac address!");
state = SCAN;
// note backoff
backoff_automata();
}
} else {
UserError.Log.wtf(TAG, "Attempt to connect when not in CONNECT state");
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
the class Ob1G5CollectionService method discover_services.
private synchronized void discover_services() {
if (state == DISCOVER) {
if (connection != null) {
if (d)
UserError.Log.d(TAG, "Local bonding state: " + (isDeviceLocallyBonded() ? "BONDED" : "NOT Bonded"));
stopDiscover();
discoverSubscription = new Subscription(connection.discoverServices(10, TimeUnit.SECONDS).subscribe(this::onServicesDiscovered, this::onDiscoverFailed));
} else {
UserError.Log.e(TAG, "No connection when in DISCOVER state - reset");
state = INIT;
background_automata();
}
} else {
UserError.Log.wtf(TAG, "Attempt to discover when not in DISCOVER state");
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class JamBaseBluetoothSequencer method discover_services.
// service discovery
public synchronized void discover_services() {
// if (state == DISCOVER) {
if (I.discoverOnce && I.isDiscoveryComplete) {
UserError.Log.d(TAG, "Skipping service discovery as already completed");
changeNextState();
} else {
if (I.connection != null) {
UserError.Log.d(TAG, "Discovering services");
stopDiscover();
I.discoverSubscription = new Subscription(I.connection.discoverServices(10, TimeUnit.SECONDS).subscribe(this::onServicesDiscovered, this::onDiscoverFailed));
} else {
UserError.Log.e(TAG, "No connection when in DISCOVER state - reset");
// These are normally just ghosts that get here, not really connected
if (I.resetWhenAlreadyConnected) {
if (JoH.ratelimit("jam-sequencer-reset", 10)) {
changeState(CLOSE);
}
}
}
// } else {
// UserError.Log.wtf(TAG, "Attempt to discover when not in DISCOVER state");
// }
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
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);
}));
}
Aggregations