如何从RxAndroidBle获取BluetoothGatt对象
如前所述,我有一个遗留应用程序拒绝连接到我试图支持的某个外围设备(与其他设备配合正常)如何从RxAndroidBle获取BluetoothGatt对象,android,bluetooth-lowenergy,rxandroidble,Android,Bluetooth Lowenergy,Rxandroidble,如前所述,我有一个遗留应用程序拒绝连接到我试图支持的某个外围设备(与其他设备配合正常)RxAndroidBle也成功连接,我正在考虑使用它来建立连接并将其交给应用程序的其余部分。我需要从rxbeconnection获取BluetoothGatt对象;我该怎么做 目前获取BluetoothGatt实例的唯一选项是实现RXbeCustomOperation接口,并在RXbeConnection.queue(RXbeCustomOperation) 公共接口RxBleCustomOperation{
RxAndroidBle
也成功连接,我正在考虑使用它来建立连接并将其交给应用程序的其余部分。我需要从rxbeconnection
获取BluetoothGatt
对象;我该怎么做 目前获取BluetoothGatt
实例的唯一选项是实现RXbeCustomOperation
接口,并在RXbeConnection.queue(RXbeCustomOperation)
公共接口RxBleCustomOperation{
/**
* (...)
*/
@非空
可观察的;可观察的,
RXblettCallback RXblettCallback,
调度程序)抛出可丢弃的;
}
请注意
RxBleCustomOperation
界面和RxBleConnection.queue(RxBleCustomOperation)
函数上的Javadoc。仔细研究了这些文档后,我希望我的理解是正确的。这些广泛的评论是为了我自己的利益,但也许它们会帮助其他试图理解这一点的人。欢迎评论和更正
public class GetGattOperation implements RxBleCustomOperation<BluetoothGatt> {
private BluetoothGatt gatt;
// How this may work:
// 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>--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 .queue()
// Instances can be queued and received via a subscription to Observable A:
// rxBleConnection.queue( new GetGattOperation() ).subscribe( gatt -> {} );
@Override
public @NonNull Observable<BluetoothGatt> asObservable( BluetoothGatt bluetoothGatt,
RxBleGattCallback rxBleGattCallback,
Scheduler scheduler) throws Throwable {
gatt = bluetoothGatt;
return Observable.just( bluetoothGatt ); // return Observable B
}
public BluetoothGatt getGatt( ) {
return gatt;
}
}
这也是一个要点:嗨@Robert Lewis,我正在尝试使用
RxBleCustomOperation
设置本机回调RXblegtCallback.setNativeCallback
但是我不太明白,你能给我举个例子吗?首先,你看到RXblegtCallback
类的Javadoc了吗?它建议不要在性能关键型应用程序中使用本机回调。
public class GetGattOperation implements RxBleCustomOperation<BluetoothGatt> {
private BluetoothGatt gatt;
// How this may work:
// 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>--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 .queue()
// Instances can be queued and received via a subscription to Observable A:
// rxBleConnection.queue( new GetGattOperation() ).subscribe( gatt -> {} );
@Override
public @NonNull Observable<BluetoothGatt> asObservable( BluetoothGatt bluetoothGatt,
RxBleGattCallback rxBleGattCallback,
Scheduler scheduler) throws Throwable {
gatt = bluetoothGatt;
return Observable.just( bluetoothGatt ); // return Observable B
}
public BluetoothGatt getGatt( ) {
return gatt;
}
}
.doOnNext( connection -> {
rxBleConnection = connection;
connection.queue( new GetGattOperation() ) // queue() returns Observable A
.subscribe( gatt -> { // receives events forwarded from Observable B
Log.i( "Main", "BluetoothGatt received: " + gatt.toString() );
} );
}
)