use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
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-plus by jamorham.
the class MedtrumCollectionService method connect_to_device.
private synchronized void connect_to_device(boolean auto) {
if (state == CONNECT) {
// UserError.Log.d(TAG, "Address length: " + address.length());
if (address != null && address.length() > 6) {
status("Connecting");
stopConnect();
bleDevice = rxBleClient.getBleDevice(address);
// 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);
}));
// Attempt to establish a connection
listen_connected = false;
// auto not allowed due to timeout
auto = false;
connectionSubscription = new Subscription(bleDevice.establishConnection(auto).timeout(LISTEN_STASIS_SECONDS, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).subscribe(this::onConnectionReceived, this::onConnectionFailure));
} else {
UserError.Log.wtf(TAG, "No transmitter mac address!");
changeState(SCAN);
}
} else {
UserError.Log.wtf(TAG, "Attempt to connect when not in CONNECT state");
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class InPenScanMeister method scan.
@Override
public synchronized void scan() {
extendWakeLock((scanSeconds + 1) * Constants.SECOND_IN_MS);
stopScan("Scan start");
UserError.Log.d(TAG, "startScan called: hunting: " + address + " " + name);
scanSubscription = new Subscription(rxBleClient.scanBleDevices(new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(), new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(SCAN_SERVICE_UUID)).build()).timeout(scanSeconds, // is unreliable
TimeUnit.SECONDS).subscribeOn(Schedulers.io()).subscribe(this::onScanResult, this::onScanFailure));
Inevitable.task(STOP_SCAN_TASK_ID, scanSeconds * Constants.SECOND_IN_MS, this::stopScanWithTimeoutCallback);
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class PendiqService method connect_to_device.
private synchronized void connect_to_device(boolean auto) {
if ((state == CONNECT) || (state == STATE.CONNECT_NOW)) {
// TODO check mac
if (address != null) {
// msg("Connect request");
if (state == STATE.CONNECT_NOW) {
if (connection_linger != null)
JoH.releaseWakeLock(connection_linger);
connection_linger = JoH.getWakeLock("jam-pendiq-pconnect", 60000);
}
// if (d)
// UserError.Log.d(TAG, "Local bonding state: " + (isDeviceLocallyBonded() ? "BONDED" : "NOT Bonded"));
stopConnect();
bleDevice = rxBleClient.getBleDevice(address);
// / / 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);
}));
// Attempt to establish a connection
connectionSubscription = new Subscription(bleDevice.establishConnection(auto).timeout(7, TimeUnit.MINUTES).subscribeOn(Schedulers.io()).subscribe(this::onConnectionReceived, this::onConnectionFailure));
} else {
UserError.Log.wtf(TAG, "No transmitter mac address!");
changeState(SCAN);
// state = STATE.SCAN;
// backoff_automata(); // note backoff
}
} else {
UserError.Log.wtf(TAG, "Attempt to connect when not in CONNECT state");
}
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class PendiqService method scan_for_device.
private synchronized void scan_for_device() {
extendWakeLock((SCAN_SECONDS + 1) * Constants.SECOND_IN_MS);
stopScan();
scanSubscription = new Subscription(rxBleClient.scanBleDevices(new ScanSettings.Builder().setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()).timeout(SCAN_SECONDS, // is unreliable
TimeUnit.SECONDS).subscribeOn(Schedulers.io()).subscribe(this::onScanResult, this::onScanFailure));
Inevitable.task("stop_pendiq_scan", SCAN_SECONDS * Constants.SECOND_IN_MS, this::stopScan);
}
Aggregations