Skip to content

Instantly share code, notes, and snippets.

@RobLewis
Last active April 12, 2023 06:41
Show Gist options
  • Save RobLewis/fc20bde8ce71c7da2040d04b2ba3c156 to your computer and use it in GitHub Desktop.
Save RobLewis/fc20bde8ce71c7da2040d04b2ba3c156 to your computer and use it in GitHub Desktop.
Simple example of usage of RxBleCustomOperation — create an Observable that emits the Bluetooth GATT object
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;
}
}
@moisesaq
Copy link

moisesaq commented Jul 25, 2019

In line 39, could I set native callback? something like this rxBleGattCallback.setNativeCallback(bluetoothGattCallback)

I have this
private val bluetoothGattCallback = object : BluetoothGattCallback() {
override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) {
super.onCharacteristicChanged(gatt, characteristic)
Timber.e("CHARACTERISTIC CHANGED")
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment