Search in sources :

Example 16 with Subscription

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

Example 17 with Subscription

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

Example 18 with Subscription

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

Example 19 with Subscription

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);
        }
    }));
}
Also used : Arrays(java.util.Arrays) CLOSED(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSED) Inevitable(com.eveningoutpost.dexdrip.UtilityModels.Inevitable) SET_TIME(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.SET_TIME) Pref(com.eveningoutpost.dexdrip.UtilityModels.Pref) OPCODE_BULK_R_XFER_0(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_BULK_R_XFER_0) BleGattCharacteristicException(com.polidea.rxandroidble2.exceptions.BleGattCharacteristicException) BroadcastSnooze(com.eveningoutpost.dexdrip.UtilityModels.BroadcastSnooze) SetTimeTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTimeTx) BaseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BaseTx) ERROR_INVALID(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_INVALID) OPCODE_IDENTIFY(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_IDENTIFY) ReplyProcessor(com.eveningoutpost.dexdrip.utils.bt.ReplyProcessor) ThinJamAudioStream(com.eveningoutpost.dexdrip.watch.thinjam.io.ThinJamAudioStream) FirmwareDownload(com.eveningoutpost.dexdrip.watch.thinjam.firmware.FirmwareDownload) OPCODE_SHOW_QRCODE(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_SHOW_QRCODE) OPCODE_RESET_ALL(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_RESET_ALL) JoH(com.eveningoutpost.dexdrip.Models.JoH) GlucoseTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.GlucoseTx) THINJAM_NOTIFY_TYPE_LOW_ALERT(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_LOW_ALERT) OPCODE_EASY_AUTH(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_EASY_AUTH) JoH.bytesToHex(com.eveningoutpost.dexdrip.Models.JoH.bytesToHex) PROTOTYPE(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.PROTOTYPE) CalibrationState(com.eveningoutpost.dexdrip.G5Model.CalibrationState) Home(com.eveningoutpost.dexdrip.Home) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic) WakeLockTrampoline(com.eveningoutpost.dexdrip.utils.framework.WakeLockTrampoline) Unitized(com.eveningoutpost.dexdrip.UtilityModels.Unitized) THINJAM_NOTIFY_TYPE_TEXTBOX1(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_TEXTBOX1) BulkUpRequestTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpRequestTx) CRITICAL(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.CRITICAL) GOOD(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.GOOD) SCHEDULE_ITEMS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.SCHEDULE_ITEMS) HexDump(com.eveningoutpost.dexdrip.ImportedLibraries.usbserial.util.HexDump) ArrayList(java.util.ArrayList) BlueJay.versionSufficient(com.eveningoutpost.dexdrip.watch.thinjam.BlueJay.versionSufficient) JoH.tsl(com.eveningoutpost.dexdrip.Models.JoH.tsl) Helper(com.eveningoutpost.dexdrip.utils.bt.Helper) SetTxIdTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetTxIdTx) DefineWindowTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.DefineWindowTx) THINJAM_NOTIFY_TYPE_CANCEL(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_CANCEL) JamBaseBluetoothSequencer(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer) ERROR_MISC(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_MISC) ObservableField(android.databinding.ObservableField) RxBleDeviceServices(com.polidea.rxandroidble2.RxBleDeviceServices) THINJAM_BULK(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_BULK) lombok.val(lombok.val) THINJAM_NOTIFY_TYPE_DIALOG(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_DIALOG) AlertPlayer(com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer) OPCODE_GET_STATUS_3(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_3) UserError(com.eveningoutpost.dexdrip.Models.UserError) OPCODE_GET_STATUS_1(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_1) OPCODE_GET_STATUS_2(com.eveningoutpost.dexdrip.watch.thinjam.Const.OPCODE_GET_STATUS_2) PersistentStore(com.eveningoutpost.dexdrip.UtilityModels.PersistentStore) Treatments(com.eveningoutpost.dexdrip.Models.Treatments) BitmapTools(com.eveningoutpost.dexdrip.watch.thinjam.utils.BitmapTools) com.eveningoutpost.dexdrip.xdrip(com.eveningoutpost.dexdrip.xdrip) FirmwareInfo(com.eveningoutpost.dexdrip.watch.thinjam.firmware.FirmwareInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) INIT(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.INIT) FEATURE_TJ_DISP_A(com.eveningoutpost.dexdrip.watch.thinjam.Const.FEATURE_TJ_DISP_A) IBinder(android.os.IBinder) BytesGenerator(com.eveningoutpost.dexdrip.utils.BytesGenerator) ERROR_OUT_OF_RANGE(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_OUT_OF_RANGE) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) NotifyTx.getPacketStreamForNotification(com.eveningoutpost.dexdrip.watch.thinjam.messages.NotifyTx.getPacketStreamForNotification) PowerManager(android.os.PowerManager) StatusItem(com.eveningoutpost.dexdrip.UtilityModels.StatusItem) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BlueJay.shouldSendReadings(com.eveningoutpost.dexdrip.watch.thinjam.BlueJay.shouldSendReadings) Locale(java.util.Locale) Schedulers(io.reactivex.schedulers.Schedulers) DEXCOM_PERIOD(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder.DEXCOM_PERIOD) DISCOVER(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.DISCOVER) BlueJayFirmware.parse(com.eveningoutpost.dexdrip.watch.thinjam.firmware.BlueJayFirmware.parse) BAD(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.BAD) THINJAM_OTA(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_OTA) CLOSE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.CLOSE) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) NORMAL(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.NORMAL) Expose(com.google.gson.annotations.Expose) UUID(java.util.UUID) ENABLE_NOTIFICATIONS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.ENABLE_NOTIFICATIONS) THINJAM_NOTIFY_TYPE_HIGH_ALERT(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_NOTIFY_TYPE_HIGH_ALERT) List(java.util.List) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) NOTICE(com.eveningoutpost.dexdrip.UtilityModels.StatusItem.Highlight.NOTICE) R(com.eveningoutpost.dexdrip.R) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription) Ob1G5CollectionService(com.eveningoutpost.dexdrip.Services.Ob1G5CollectionService) ERROR_OK(com.eveningoutpost.dexdrip.watch.thinjam.Const.ERROR_OK) BulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BulkUpTx) ThinJamActivity(com.eveningoutpost.dexdrip.ui.activities.ThinJamActivity) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) JoH.msTill(com.eveningoutpost.dexdrip.Models.JoH.msTill) Setter(lombok.Setter) LOG_TESTS(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.LOG_TESTS) Getter(lombok.Getter) AuthReqTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.AuthReqTx) Constants(com.eveningoutpost.dexdrip.UtilityModels.Constants) PushRx(com.eveningoutpost.dexdrip.watch.thinjam.messages.PushRx) Pair(android.util.Pair) Intent(android.content.Intent) HOUR_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.HOUR_IN_MS) RBulkUpTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.RBulkUpTx) StandbyTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.StandbyTx) JoH.niceTimeScalar(com.eveningoutpost.dexdrip.Models.JoH.niceTimeScalar) Binder(android.os.Binder) SuppressLint(android.annotation.SuppressLint) OTAUI(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.OTAUI) SlidingWindowConstraint(com.eveningoutpost.dexdrip.utils.time.SlidingWindowConstraint) TJ_BitmapType(com.eveningoutpost.dexdrip.watch.thinjam.utils.BitmapTools.TJ_BitmapType) SLEEP(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SLEEP) BluetoothGattService(android.bluetooth.BluetoothGattService) com.eveningoutpost.dexdrip.xdrip.gs(com.eveningoutpost.dexdrip.xdrip.gs) BlueJayFirmware.split(com.eveningoutpost.dexdrip.watch.thinjam.firmware.BlueJayFirmware.split) JoH.msSince(com.eveningoutpost.dexdrip.Models.JoH.msSince) JoH.threadSleep(com.eveningoutpost.dexdrip.Models.JoH.threadSleep) BackFillTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.BackFillTx) TimeUnit(java.util.concurrent.TimeUnit) SECOND_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.SECOND_IN_MS) THINJAM_WRITE(com.eveningoutpost.dexdrip.watch.thinjam.Const.THINJAM_WRITE) ResetPersistTx(com.eveningoutpost.dexdrip.watch.thinjam.messages.ResetPersistTx) RUN_QUEUE(com.eveningoutpost.dexdrip.watch.thinjam.BlueJayService.ThinJamState.RUN_QUEUE) SEND_QUEUE(com.eveningoutpost.dexdrip.Services.JamBaseBluetoothSequencer.BaseState.SEND_QUEUE) SetGammaEtc(com.eveningoutpost.dexdrip.watch.thinjam.messages.SetGammaEtc) MINUTE_IN_MS(com.eveningoutpost.dexdrip.UtilityModels.Constants.MINUTE_IN_MS) lombok.val(lombok.val) BleCannotSetCharacteristicNotificationException(com.polidea.rxandroidble2.exceptions.BleCannotSetCharacteristicNotificationException) BleCharacteristicNotFoundException(com.polidea.rxandroidble2.exceptions.BleCharacteristicNotFoundException) BleDisconnectedException(com.polidea.rxandroidble2.exceptions.BleDisconnectedException) Subscription(com.eveningoutpost.dexdrip.utils.bt.Subscription)

Example 20 with Subscription

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);
}
Also used : ScanSettings(com.polidea.rxandroidble2.scan.ScanSettings) 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