Search in sources :

Example 1 with Subscription

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");
    // }
    }
}
Also used : Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Example 2 with Subscription

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");
    }
}
Also used : Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Example 3 with Subscription

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");
        }
    }));
}
Also used : BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Example 4 with Subscription

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);
        }
    }));
}
Also used : AUTHORIZE_FAILED(com.eveningoutpost.dexdrip.watch.miband.MiBandService.MiBandState.AUTHORIZE_FAILED) AUTH_REQUEST_RANDOM_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_REQUEST_RANDOM_AUTH_NUMBER) AuthMessages(com.eveningoutpost.dexdrip.watch.miband.message.AuthMessages) Arrays(java.util.Arrays) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) PendingIntent(android.app.PendingIntent) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) PowerManager(android.os.PowerManager) HeartRate(com.eveningoutpost.dexdrip.Models.HeartRate) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) COMMAND_ACK_FIND_PHONE_IN_PROGRESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_ACK_FIND_PHONE_IN_PROGRESS) AUTH_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_FAIL) Schedulers(io.reactivex.schedulers.Schedulers) AUTH_MIBAND4_CODE_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_CODE_FAIL) AUTH_RESPONSE(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_RESPONSE) AUTH_SEND_KEY(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_KEY) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert) FeaturesControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.FeaturesControllMessage) MIBAND_NOTIFY_TYPE_ALARM(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_ALARM) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) PrefBindingFactory(com.eveningoutpost.dexdrip.watch.PrefBindingFactory) UUID(java.util.UUID) JoH.getResourceURI(com.eveningoutpost.dexdrip.Models.JoH.getResourceURI) UNKNOWN(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.UNKNOWN) JoH(com.eveningoutpost.dexdrip.Models.JoH) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) MIBAND_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CANCEL) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) PREFERRED_MTU_SIZE(com.eveningoutpost.dexdrip.watch.miband.Const.PREFERRED_MTU_SIZE) AlertLevelMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertLevelMessage) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) AUTH_SUCCESS(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SUCCESS) Getter(lombok.Getter) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) AUTH_SEND_ENCRYPTED_AUTH_NUMBER(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_SEND_ENCRYPTED_AUTH_NUMBER) Pair(android.util.Pair) FirmwareOperations(com.eveningoutpost.dexdrip.watch.miband.Firmware.FirmwareOperations) DisplayControllMessageMiband3_4(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4) RxBleConnection(com.polidea.rxandroidble2.RxBleConnection) Intent(android.content.Intent) MediaPlayer(android.media.MediaPlayer) ArrayList(java.util.ArrayList) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) SuppressLint(android.annotation.SuppressLint) Calendar(java.util.Calendar) DeviceEvent(com.eveningoutpost.dexdrip.watch.miband.message.DeviceEvent) DisplayControllMessageMiBand2(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiBand2) DisplayControllMessage(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessage) COMMAND_DISABLE_CALL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.COMMAND_DISABLE_CALL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) AlertMessage(com.eveningoutpost.dexdrip.watch.miband.message.AlertMessage) MI_BAND4(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND4) Sheduled(com.eveningoutpost.dexdrip.watch.miband.message.DisplayControllMessageMiband3_4.NightMode.Sheduled) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) MI_BAND2(com.eveningoutpost.dexdrip.watch.miband.MiBand.MiBandType.MI_BAND2) JoH.emptyString(com.eveningoutpost.dexdrip.Models.JoH.emptyString) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) AUTH_MIBAND4_FAIL(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes.AUTH_MIBAND4_FAIL) WatchFaceGenerator(com.eveningoutpost.dexdrip.watch.miband.Firmware.WatchFaceGenerator) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) PoorMansConcurrentLinkedDeque(com.eveningoutpost.dexdrip.utils.framework.PoorMansConcurrentLinkedDeque) TimeUnit(java.util.concurrent.TimeUnit) MIBAND_NOTIFY_TYPE_MESSAGE(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_MESSAGE) OperationCodes(com.eveningoutpost.dexdrip.watch.miband.message.OperationCodes) UserError(com.eveningoutpost.dexdrip.Models.UserError) MIBAND_NOTIFY_TYPE_CALL(com.eveningoutpost.dexdrip.watch.miband.Const.MIBAND_NOTIFY_TYPE_CALL) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) SuppressLint(android.annotation.SuppressLint)

Example 5 with Subscription

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");
    }
}
Also used : Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Aggregations

Subscription (com.eveningoutpost.dexdrip.utils.bt.Subscription)34 SuppressLint (android.annotation.SuppressLint)10 Intent (android.content.Intent)10 PowerManager (android.os.PowerManager)10 Pair (android.util.Pair)10 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)10 JoH (com.eveningoutpost.dexdrip.Models.JoH)10 JoH.msTill (com.eveningoutpost.dexdrip.Models.JoH.msTill)10 UserError (com.eveningoutpost.dexdrip.Models.UserError)10 R (com.eveningoutpost.dexdrip.R)10 Constants (com.eveningoutpost.dexdrip.UtilityModels.Constants)10 Inevitable (com.eveningoutpost.dexdrip.UtilityModels.Inevitable)10 StatusItem (com.eveningoutpost.dexdrip.UtilityModels.StatusItem)10 WakeLockTrampoline (com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline)10 com.eveningoutpost.dexdrip.xdrip (com.eveningoutpost.dexdrip.xdrip)10 com.eveningoutpost.dexdrip.xdrip.gs (com.eveningoutpost.dexdrip.xdrip.gs)10 BleCharacteristicNotFoundException (com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException)10 Schedulers (io.reactivex.schedulers.Schedulers)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10