Skip to content

Instantly share code, notes, and snippets.

@RobLewis
Last active April 12, 2023 06:41

Revisions

  1. RobLewis revised this gist Jun 26, 2018. No changes.
  2. RobLewis created this gist Jun 26, 2018.
    48 changes: 48 additions & 0 deletions CustomOperationExample.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    package net.grlewis.cufftest;

    import android.bluetooth.BluetoothGatt;

    import com.polidea.rxandroidble2.RxBleCustomOperation;
    import com.polidea.rxandroidble2.internal.connection.RxBleGattCallback;

    import io.reactivex.Observable;
    import io.reactivex.Scheduler;
    import io.reactivex.annotations.NonNull;

    public class GetGattOperation implements RxBleCustomOperation<BluetoothGatt> {

    private BluetoothGatt gatt;

    // How this works:

    // You call rxBleConnection.queue( <instance of this class> )
    // It returns an Observable<T>--call it Observable A

    // The queue manager calls the .asObservable() method below,
    // which returns another Observable<T>--call it Observable B
    // It is placed in the queue for execution

    // When it's time to run this operation, ConnectionOperationQueue will
    // subscribe to Observable B (here, Observable.just( bluetoothGatt ))

    // Emissions from this Observable B (here, the bluetoothGatt) are forwarded to the Observable A returned by the .queue() method

    // Instances can be queued and received via a subscription to Observable A:
    // rxBleConnection.queue( new GetGattOperation() ).subscribe( gatt -> {} );


    @Override // returns "Observable B" (see above)
    public @NonNull Observable<BluetoothGatt> asObservable( BluetoothGatt bluetoothGatt,
    RxBleGattCallback rxBleGattCallback,
    Scheduler scheduler) throws Throwable {

    gatt = bluetoothGatt;
    return Observable.just( bluetoothGatt ); // return Observable B (emits Gatt then completes)
    }


    public BluetoothGatt getGatt( ) {
    return gatt;
    }

    }