use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class PendiqService 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");
changeState(INIT);
}
} 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 realEstablishConnection.
// also called from callback
private void realEstablishConnection(final String address, final boolean autoConnect) {
UserError.Log.d(TAG, "Trying connect: " + address + " autoconnect: " + autoConnect);
// Attempt to establish a connection
I.connectionSubscription = new Subscription(I.bleDevice.establishConnection(autoConnect).timeout(I.connectTimeoutMinutes, TimeUnit.MINUTES).subscribeOn(Schedulers.io()).doFinally(this::establishConnectionFinally).subscribe(this::onConnectionReceived, this::onConnectionFailure));
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip-plus by jamorham.
the class JamBaseBluetoothSequencer method watchConnection.
protected synchronized void watchConnection(final String address) {
UserError.Log.d(TAG, "Starting to watch connection with: " + address);
// / / Listen for connection state changes
I.stateSubscription = new Subscription(I.bleDevice.observeConnectionStateChanges().subscribeOn(Schedulers.io()).subscribe(this::onConnectionStateChange, throwable -> {
UserError.Log.wtf(TAG, "Got Error from state subscription: " + throwable);
}));
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
the class BlueJayService method enableNotifications.
private void enableNotifications() {
UserError.Log.d(TAG, "enableNotifications called()");
if (I.isNotificationEnabled) {
UserError.Log.d(TAG, "Notifications already enabled");
changeNextState();
return;
}
if (notificationSubscription != null) {
notificationSubscription.unsubscribe();
}
JoH.threadSleep(500);
UserError.Log.d(TAG, "Requesting to enable notifications");
if (I.connection == null) {
UserError.Log.d(TAG, "Connection went away before we could enable notifications");
return;
}
notificationSubscription = new Subscription(I.connection.setupNotification(THINJAM_WRITE).observeOn(// needed?
Schedulers.newThread()).doOnNext(notificationObservable -> {
UserError.Log.d(TAG, "Notifications enabled");
I.connection.writeCharacteristic(THINJAM_BULK, new byte[] { 0x55 });
// Debug sleep to make sure notifications are actually enabled???
JoH.threadSleep(500);
I.isNotificationEnabled = true;
changeNextState();
}).flatMap(notificationObservable -> notificationObservable).observeOn(Schedulers.newThread()).subscribe(bytes -> {
// incoming notifications
UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes));
val pushRx = PushRx.parse(bytes);
if (pushRx != null) {
UserError.Log.d(TAG, "Received PushRX: " + pushRx.toS());
getInfo().processPushRx(pushRx);
processPushRxActions(pushRx);
} else if (bytes[0] == 0x06) {
// TODO move this to parsed response
final int replyParam = ((int) bytes[1]) & 0xff;
if (replyParam == 0) {
UserError.Log.d(TAG, "Bulk up success reply marker received! - removing queue head item");
// removes first item from the queue which should be the one we just processed!
commandQueue.poll();
UserError.Log.d(TAG, "Scheduling immediate run of queue");
// remove timeout retry task
Inevitable.kill("tj-next-queue");
Inevitable.task("tj-next-queue", 0, this::processQueue);
} else {
revisedOffset = replyParam;
// race condition on display
UserError.Log.d(TAG, "Bulk up failure at: " + revisedOffset);
}
} else if (bytes[0] == (byte) 0xFE) {
audioStreamCallBack(bytes);
} else {
if (ThinJamActivity.isD()) {
notificationString.append(new String(bytes));
Inevitable.task("tj update notifi", 250, new Runnable() {
@Override
public void run() {
stringObservableField.set(notificationString.toString());
}
});
}
}
}, 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");
debug.processTestSuite("logcharerror");
tryGattRefresh(getI().connection);
}
if (throwable instanceof BleCannotSetCharacteristicNotificationException) {
UserError.Log.e(TAG, "Problems setting notifications - disconnecting");
changeState(CLOSE);
}
if (throwable instanceof BleDisconnectedException) {
UserError.Log.d(TAG, "Disconnected while enabling notifications");
changeState(CLOSE);
}
}));
}
use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
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);
}
Aggregations