use of com.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
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 by NightscoutFoundation.
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 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.eveningoutpost.dexdrip.utils.bt.Subscription 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.eveningoutpost.dexdrip.utils.bt.Subscription in project xDrip by NightscoutFoundation.
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");
}
}
Aggregations