Comments (7)
By the way,if device is try to connect,
at the same time to start scan(scanBleDevices),the scan will return nothing until device gatt close,please help me.
from rxandroidble.
Hello @kexuejin
I am currently working on the fix to close the connection asap. For the second issue it is by design that only one operation is being performed at the time and establishing the connection is an atomic operation if autoConnect == false
. Once I will finish the fix scan should happen earlier.
Best Regards
from rxandroidble.
Thank you very much!
from rxandroidble.
Fix is ready. It's waiting for our internal code review but unfortunately it is our holiday season and it may take up to two weeks. If you need it quicker you can always checkout the latest master
branch and exchange RxBleRadioOperationConnect.java
file code to:
package com.polidea.rxandroidble.internal.operations;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.support.annotation.NonNull;
import com.polidea.rxandroidble.RxBleConnection;
import com.polidea.rxandroidble.internal.RxBleLog;
import com.polidea.rxandroidble.internal.RxBleRadioOperation;
import com.polidea.rxandroidble.internal.connection.RxBleGattCallback;
import com.polidea.rxandroidble.internal.util.BleConnectionCompat;
import rx.Observable;
import rx.subjects.BehaviorSubject;
import static com.polidea.rxandroidble.RxBleConnection.RxBleConnectionState.CONNECTED;
public class RxBleRadioOperationConnect extends RxBleRadioOperation<BluetoothGatt> {
private final BluetoothDevice bluetoothDevice;
private final RxBleGattCallback rxBleGattCallback;
private final BleConnectionCompat connectionCompat;
private final boolean autoConnect;
private BehaviorSubject<BluetoothGatt> bluetoothGattBehaviorSubject = BehaviorSubject.create();
@SuppressWarnings("Convert2MethodRef")
private final Runnable releaseRadioRunnable = () -> releaseRadio();
private final Runnable emptyRunnable = () -> {
};
private final BehaviorSubject<Boolean> isSubscribed = BehaviorSubject.create(false);
private final Observable<BluetoothGatt> asObservable = super.asObservable()
.doOnSubscribe(() -> isSubscribed.onNext(true))
.doOnUnsubscribe(() -> isSubscribed.onNext(false))
.share();
public RxBleRadioOperationConnect(BluetoothDevice bluetoothDevice, RxBleGattCallback rxBleGattCallback,
BleConnectionCompat connectionCompat, boolean autoConnect) {
this.bluetoothDevice = bluetoothDevice;
this.rxBleGattCallback = rxBleGattCallback;
this.connectionCompat = connectionCompat;
this.autoConnect = autoConnect;
}
@Override
public Observable<BluetoothGatt> asObservable() {
return asObservable;
}
@Override
protected void protectedRun() {
final Runnable onConnectionEstablishedRunnable = autoConnect ? emptyRunnable : releaseRadioRunnable;
final Runnable onConnectCalledRunnable = autoConnect ? releaseRadioRunnable : emptyRunnable;
getConnectedBluetoothGatt()
// when there are no subscribers there is no point of continuing work -> next will be disconnect operation
.takeUntil(hasNoSubscribers().doOnNext(noSubscribers -> RxBleLog.d("No subscribers, finishing operation")))
.doOnCompleted(onConnectionEstablishedRunnable::run)
.subscribe(getSubscriber());
onConnectCalledRunnable.run();
}
@NonNull
private Observable<Boolean> hasNoSubscribers() {
return isSubscribed.filter(aBoolean -> !aBoolean);
}
/**
* Emits BluetoothGatt and completes after connection is established.
*
* @return BluetoothGatt after connection reaches {@link RxBleConnection.RxBleConnectionState#CONNECTED} state.
* @throws com.polidea.rxandroidble.exceptions.BleDisconnectedException if connection was disconnected/failed before it was established.
*/
@NonNull
private Observable<BluetoothGatt> getConnectedBluetoothGatt() {
// start connecting the BluetoothGatt
// note: Due to different Android BLE stack implementations it is not certain whether `connectGatt()` or `BluetoothGattCallback`
// will emit BluetoothGatt first
return Observable.fromCallable(() ->
connectionCompat.connectGatt(bluetoothDevice, autoConnect, rxBleGattCallback.getBluetoothGattCallback())
)
.mergeWith(rxBleGattCallback.getBluetoothGatt())
// relay BluetoothGatt instance updates
.doOnNext(bluetoothGattBehaviorSubject::onNext)
// capture BluetoothGatt when connected
.sample(rxBleGattCallback
.getOnConnectionStateChange()
.filter(rxBleConnectionState -> rxBleConnectionState == CONNECTED))
// disconnect may happen even if the connection was not established yet
.mergeWith(rxBleGattCallback.observeDisconnect())
.take(1)
// finish relaying if there won't be more updates
.doOnTerminate(bluetoothGattBehaviorSubject::onCompleted);
}
/**
* Obtain observable emitting most recent {@link BluetoothGatt instance}.
* NOTE: Connection may be released and/or GATT may be closed in any point of time.
*
* @return Observable with BluetoothGatt. Most recent GATT will be emitted instantly after subscription if it is available.
*/
public Observable<BluetoothGatt> getBluetoothGatt() {
return bluetoothGattBehaviorSubject;
}
}
Best Regards
from rxandroidble.
Hey guys, I suddenly found that reverting the RxJava version to 1.1.7 (previously was 1.1.9) seems to help with this problem. I'm gonna assume there were some changes in the way Subjects work in the later versions that made the library behave strangely.
from rxandroidble.
Maybe, but i don't want to reverting to rxjava version...
from rxandroidble.
Fixed with 604853c
Should be available in the SNAPSHOT shortly.
from rxandroidble.
Related Issues (20)
- It is not possible to disconnect the Bluetooth while it is in the process of connecting. HOT 1
- BleGattCharacteristicException while readCharacteristic HOT 12
- scanResult.bleDevice.name not showing Ble updated name HOT 1
- BLUETOOTH_SCAN set usesPermissionFlags="neverForLocation",Is there a problem? HOT 2
- GenericFailure<CharacteristicValueUpdateError> HOT 11
- Send long bytes twice HOT 2
- Write request is not processed in the random order HOT 2
- Long Bytes Send HOT 8
- Creating a Fork HOT 2
- Set Preferred PHY / Read PHY HOT 2
- RxBleClient mulit connections HOT 4
- Is the library still maintained? HOT 3
- Android 14 breaking change HOT 15
- Google Pixel 5 loses bonding info after BT adapter cycle HOT 4
- Metode readcharacteristic not stream HOT 2
- Unable to readCharacteristic after writeCharacteristic HOT 5
- In the case of unsuccessful Bluetooth connection, actively disconnect without executing context. unregisterReceiver (receiver); At this point, there may be multiple Bluetooth broadcast registrarReceivers, which may crash on some Huawei phones due to a broadcast limit of approximately 1000. Suggest fixing this issue. HOT 2
- Can't scan after connect dispose/clear HOT 1
- Add `advertisingSid` property to `ScanResult` and `RxBleInternalScanResult` HOT 3
- BleGattCallbackTimeoutException when writing a characteristic with different data payload many times in a short period of time. HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxandroidble.