android


RxAndroidBle stopped when discovering another BLE device


I'm using RxAndroidBle to discovery surrounded devices' service and some characteristics. I met a problem that establishConnection with another device dosn't work after the connection with first device is unsubsribed. When it comes to another device, the state is always "Connection State: RxBleConnectionState{DISCONNECTED}".
Here is the codes, will anyone can help me to have a check!
private Subscription createDeviceConnectionSubscription(final RxBleDevice
rxBleDevice){
bInternalStatus = DISCOVERRING;
Log.d(LOG_TAG, "start to discover the services on: " + rxBleDevice.getMacAddress().toString());
Subscription subscription = rxBleDevice.establishConnection(false)
.timeout(10, TimeUnit.SECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.from(mWorker.getLooper()))
.flatMap(new Func1<RxBleConnection, Observable<gattConnectionResults>>() {
#Override
public Observable<gattConnectionResults> call(RxBleConnection rxBleConnection) {
return Observable.zip(
rxBleConnection.discoverServices(3, TimeUnit.SECONDS),
rxBleConnection.readCharacteristic(UUID.fromString("UUID1")),
rxBleConnection.readCharacteristic(UUID.fromString("UUID2")),
new Func3<RxBleDeviceServices, byte[], byte[], gattConnectionResults>() {
#Override
public gattConnectionResults call(RxBleDeviceServices rxBleDeviceServices, byte[] bytes, byte[] bytes2) {
//......
return results;
}
}
);
}
})
.subscribe(subscriber);
rxBleDevice.observeConnectionStateChanges()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<RxBleConnection.RxBleConnectionState>() {
#Override
public void call(RxBleConnection.RxBleConnectionState rxBleConnectionState) {
Log.d("Gatt Connection State", ""+rxBleConnectionState);
}
});
return subscription;
}
Subscriber<gattConnectionResults> subscriber = new
Subscriber<gattConnectionResults>() {
#Override
public void onCompleted() {
Log.d(LOG_TAG, "RXXX on completed called!!!");
if(mDeviceConnectionSubscription!=null){
handleConnectionOver();
}
}
#Override
public void onError(Throwable e) {
Log.d(LOG_TAG, "RXXX Connection error!!! " + e);
if(mDeviceConnectionSubscription!=null){
handleConnectionOver();
}
}
#Override
public void onNext(gattConnectionResults results) {
}
}
};
private void handleConnectionOver(){
bInternalStatus = STOPPED;
if(mDeviceConnectionSubscription!=null && !mDeviceConnectionSubscription.isUnsubscribed()){
mDeviceConnectionSubscription.unsubscribe();
mDeviceConnectionSubscription = null;
}
}
Thanks for #s-noopy, after move unsubcribe to the end of onNext(), the logs by RxBle are:
05-15 D/RxBle#Radio: QUEUED RxBleRadioOperationConnect(192750313)
05-15 D/RxBle#Radio: STARTED RxBleRadioOperationConnect(192750313)
05-15 V/RxBle#BleConnectionCompat: Connecting without reflection
05-15 D/RxBle#BluetoothGatt: onConnectionStateChange newState=2 status=0
05-15 D/RxBle#Radio: FINISHED RxBleRadioOperationConnect(192750313)
05-15 D/RxBle#Radio: QUEUED RxBleRadioOperationServicesDiscover(178598822)
05-15 D/RxBle#Radio: STARTED RxBleRadioOperationServicesDiscover(178598822)
05-15 D/RxBle#BluetoothGatt: onServicesDiscovered status=0
05-15 D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(241700341)
05-15 D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(24572440)
05-15 D/RxBle#Radio: FINISHED RxBleRadioOperationServicesDiscover(178598822)
05-15 D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(241700341)
05-15 D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=00002a00-0000-1000-8000-00805f9b34fb status=0
05-15 D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(241700341)
05-15 D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(24572440)
05-15 D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=00002a29-0000-1000-8000-00805f9b34fb status=0
05-15 D/RxBle#Radio: QUEUED RxBleRadioOperationDisconnect(204169407)
05-15 D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(24572440)
05-15 D/RxBle#Radio: STARTED RxBleRadioOperationDisconnect(204169407)
05-15 D/RxBle#BluetoothGatt: onConnectionStateChange newState=0 status=0
05-15 D/RxBle#Radio: FINISHED RxBleRadioOperationDisconnect(204169407)
You should create a new Subscriber for each Subscription. Subscriber is stateful and once it will get onCompleted or onError it will not subscribe again to any other Observable.
Alternatively you should use Observer instead of Subscriber. Observer is stateless.

Related Links

Retrieving images using its path from server to android app
RecyclerView notifyItemChanged Prevent Scroll [duplicate]
Google Analytics doesn't report Real-time Android
Edit text integer
Titanium Alloy crash on android, Cannot call method 'open' of undefined
Native components, armeabi-v7a, and thumb?
Checkbox getting deselected in arrayadapter in listview
Android: member variable becoming null
How to detect color of the center of camera streaming in android without OpenCV
Missing App Icon after modifying Manifest
Save Image from res/drawable to Image Gallery Android
SQL lite select for actual week (from monday till sunday)
how to get imageview height and width after pinch zoom?
How to properly create a customized animated drawable?
Android Backstack Management
how to get mobile number from my current Phone? it's display blank Mobile Number in Log. but when run on emulator its display emulator Number [duplicate]

Categories

HOME
jpa
delphi-7
github-for-windows
gpu
flex
access
bitbucket-api
rom
gembox-spreadsheet
regular-language
hpc
webix
recordrtc
teechart
rcloud
arangodb
xcodebuild
jsonpath
spring-social
modbus-tcp
nodemcu
gmm
boost-icl
systemtime
google-api-client
draft-js-plugins
expressionengine3
xcode-ui-testing
moses
jasmine-node
delimiter
ms-access-2003
handler
angular-translate
iis-express
sqsh
panoramas
linkedhashmap
fossil
hibernate-search
getlasterror
docfx
flatmap
recurrent-neural-network
visa
odoo-website
ssrs-2014
berkeley-db-je
apache-directory
magento-2.0.7
jquery-ui-draggable
podscms
filehandle
lsyncd
pybrain
choice
parfor
burrows-wheeler-transform
neon
nikeplus-api
clipboard.js
http-status-code-401
stream-processing
project-organization
cda
ikiwiki
design-principles
decimalformat
roadkill-wiki
visual-sourcesafe-2005
liquid-layout
execve
structuremap3
appserver
cbind
decoupling
git-repo
eclipse-kepler
ie8-compatibility-mode
tlf
scringo
cnf
composite-component
traceability
fb.ui
android-sdk-2.3
oracle-enterprise-linux
apache-commons-dbutils
convention
flex-mobile
winmain
collectionviewsource
qtabbar
duplex
data-mapping
xpsdocument
great-circle

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App