Last active
April 12, 2023 06:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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")
}
}