Skip to content

Commit f35e8c5

Browse files
author
Vincent Masselis
committed
Updated to AS 3.5
1 parent 8731e89 commit f35e8c5

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99

1010
}
1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:3.4.2'
12+
classpath 'com.android.tools.build:gradle:3.5.0'
1313
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414
// NOTE: Do not place your application dependencies here; they belong
1515
// in the individual module build.gradle files
@@ -36,5 +36,5 @@ task clean(type: Delete) {
3636
ext {
3737
versionCode = "$System.env.BITRISE_BUILD_NUMBER"
3838
groupId = 'com.vincentmasselis.rxbluetoothkotlin'
39-
libVersion = "1.1.8"
39+
libVersion = "1.1.9"
4040
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Apr 24 14:46:24 CEST 2019
1+
#Thu Aug 22 18:41:29 CEST 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

rxbluetoothkotlin-core/src/main/kotlin/com/vincentmasselis/rxbluetoothkotlin/RxBluetoothGattImpl.kt

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.vincentmasselis.rxbluetoothkotlin
33
import android.annotation.SuppressLint
44
import android.bluetooth.*
55
import android.bluetooth.BluetoothAdapter.*
6+
import android.bluetooth.BluetoothGatt.GATT_SUCCESS
67
import android.content.Context
78
import android.content.IntentFilter
89
import android.os.Build
@@ -17,6 +18,7 @@ import io.reactivex.functions.Consumer
1718
import io.reactivex.functions.Function
1819
import io.reactivex.subjects.PublishSubject
1920
import io.reactivex.subjects.SingleSubject
21+
import io.reactivex.subjects.UnicastSubject
2022
import java.util.*
2123
import java.util.concurrent.CancellationException
2224
import java.util.concurrent.TimeUnit
@@ -107,16 +109,16 @@ class RxBluetoothGattImpl(
107109
callback.onConnectionState
108110
.subscribe { (newState, status) ->
109111
when {
110-
newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS ->
112+
newState == BluetoothProfile.STATE_CONNECTED && status == GATT_SUCCESS ->
111113
downStream.onNext(Unit)
112114

113-
newState == BluetoothProfile.STATE_DISCONNECTED && status == BluetoothGatt.GATT_SUCCESS ->
115+
newState == BluetoothProfile.STATE_DISCONNECTED && status == GATT_SUCCESS ->
114116
downStream.onComplete()
115117

116-
newState == BluetoothProfile.STATE_DISCONNECTED && status != BluetoothGatt.GATT_SUCCESS ->
118+
newState == BluetoothProfile.STATE_DISCONNECTED && status != GATT_SUCCESS ->
117119
downStream.tryOnError(exceptionConverter(source.device, status))
118120

119-
status != BluetoothGatt.GATT_SUCCESS ->
121+
status != GATT_SUCCESS ->
120122
downStream.tryOnError(exceptionConverter(source.device, status))
121123
}
122124
}
@@ -143,12 +145,12 @@ class RxBluetoothGattImpl(
143145

144146
// -------------------- I/O Tools
145147

146-
private val operationQueue = PublishSubject.create<Maybe<Any>>()
148+
private val operationQueue = UnicastSubject.create<Maybe<Any>>()
147149

148150
init {
149151
operationQueue
150152
.takeUntil(closeSubject.toObservable()) // Disposes when the connection is closed. Keep this takeUntil BEFORE the concatMapMaybe. If put after, every pending I/O into concatMapMaybe is disposed and the pending I/O messages will never trigger which causes dead chains.
151-
.concatMapMaybe { it.onErrorReturnItem(Unit) /* To avoid disposing which make the queue unavailable */ }
153+
.concatMapMaybe({ it.onErrorReturnItem(Unit) /* To avoid disposing which make the queue unavailable */ }, 1)
152154
.subscribe()
153155
}
154156

@@ -161,11 +163,19 @@ class RxBluetoothGattImpl(
161163
* @param [this] a single which contains a [BluetoothGatt] I/O operation to do.
162164
*/
163165
private fun <T> Single<T>.enqueue(exceptionWrapper: (device: BluetoothDevice, status: Int) -> DeviceDisconnected) = Maybe.create<T> { downstream ->
166+
167+
//logger?.v(TAG, "Enqueue Maybe create")
168+
164169
val livingConnection = Observable.defer { livingConnection(exceptionWrapper) }.replay(1).refCount()
165170

166171
livingConnection
172+
/*.doOnSubscribe { logger?.v(TAG, "Enqueue livingConnection subscription") }
173+
.doOnNext { logger?.v(TAG, "Enqueue livingConnection next") }
174+
.doOnError { logger?.v(TAG, "Enqueue livingConnection error $it") }
175+
.doOnComplete { logger?.v(TAG, "Enqueue livingConnection completion") }*/
167176
.flatMapSingle {
168177
this // this is the single to enqueue
178+
//.doOnSubscribe { logger?.v(TAG, "Enqueue source maybe subscription") }
169179
.subscribeOn(AndroidSchedulers.mainThread())
170180
// Value is set to 1 minute because some devices take a long time to detect when the connection is lost. For example, we saw up to 16 seconds on a Nexus 4
171181
// between the last call to write and the moment when the system fallback the disconnection.
@@ -180,6 +190,9 @@ class RxBluetoothGattImpl(
180190
else Observable.error(it)
181191
})
182192
.firstElement()
193+
/*.doOnSuccess { logger?.v(TAG, "Enqueue Maybe success with value $it") }
194+
.doOnError { logger?.v(TAG, "Enqueue Maybe error $it") }
195+
.doOnComplete { logger?.v(TAG, "Enqueue Maybe completed") }*/
183196
// You don't have to subscribe to this chain, operationQueue will do it for you
184197
// It's impossible to cancel a bluetooth I/O operation, so, even if the downstream is not listening for values, you must continue to listen until the end of the
185198
// operation, if not, you could start a new operation before the current has finished which fires exceptions.
@@ -191,7 +204,9 @@ class RxBluetoothGattImpl(
191204
operationQueue.onNext(it as Maybe<Any>)
192205
}
193206

194-
}
207+
//downstream.setCancellable { logger?.v(TAG, "Enqueue Maybe cancelled") }
208+
209+
}//.also { logger?.v(TAG, "Enqueue Maybe method called") }
195210

196211
// -------------------- I/O Operations
197212

@@ -218,7 +233,7 @@ class RxBluetoothGattImpl(
218233
}
219234
.enqueue(::RssiDeviceDisconnected)
220235
.flatMap {
221-
if (it.status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.RssiReadingFailed(it.status, source.device))
236+
if (it.status != GATT_SUCCESS) Maybe.error(IOFailed.RssiReadingFailed(it.status, source.device))
222237
else Maybe.just(it.rssi)
223238
}
224239

@@ -245,7 +260,7 @@ class RxBluetoothGattImpl(
245260
}
246261
.enqueue(::DiscoverServicesDeviceDisconnected)
247262
.flatMap { status ->
248-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.ServiceDiscoveringFailed(status, source.device))
263+
if (status != GATT_SUCCESS) Maybe.error(IOFailed.ServiceDiscoveringFailed(status, source.device))
249264
else Maybe.just(source.services)
250265
}
251266

@@ -273,7 +288,7 @@ class RxBluetoothGattImpl(
273288
}
274289
.enqueue(::MtuDeviceDisconnected)
275290
.flatMap {
276-
if (it.status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.MtuRequestingFailed(it.status, source.device))
291+
if (it.status != GATT_SUCCESS) Maybe.error(IOFailed.MtuRequestingFailed(it.status, source.device))
277292
else Maybe.just(it.mtu)
278293
}
279294

@@ -303,7 +318,7 @@ class RxBluetoothGattImpl(
303318
}
304319
.enqueue(::ReadPhyDeviceDisconnected)
305320
.flatMap {
306-
if (it.status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.PhyReadFailed(it.connectionPHY, it.status, source.device))
321+
if (it.status != GATT_SUCCESS) Maybe.error(IOFailed.PhyReadFailed(it.connectionPHY, it.status, source.device))
307322
else Maybe.just(it.connectionPHY)
308323
}
309324

@@ -333,7 +348,7 @@ class RxBluetoothGattImpl(
333348
}
334349
.enqueue { device, status -> SetPreferredPhyDeviceDisconnected(connectionPhy, phyOptions, device, status) }
335350
.flatMap { (connectionPhy, status) ->
336-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.SetPreferredPhyFailed(connectionPhy, phyOptions, status, source.device))
351+
if (status != GATT_SUCCESS) Maybe.error(IOFailed.SetPreferredPhyFailed(connectionPhy, phyOptions, status, source.device))
337352
else Maybe.just(connectionPhy)
338353
}
339354

@@ -372,7 +387,7 @@ class RxBluetoothGattImpl(
372387
}
373388
.enqueue { device, status -> CharacteristicReadDeviceDisconnected(device, status, characteristic.service, characteristic) }
374389
.flatMap { (readCharacteristic, status) ->
375-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(IOFailed.CharacteristicReadingFailed(status, source.device, readCharacteristic.service, readCharacteristic))
390+
if (status != GATT_SUCCESS) Maybe.error(IOFailed.CharacteristicReadingFailed(status, source.device, readCharacteristic.service, readCharacteristic))
376391
else Maybe.just(readCharacteristic.value)
377392
}
378393

@@ -413,7 +428,7 @@ class RxBluetoothGattImpl(
413428
}
414429
.enqueue { device, status -> CharacteristicWriteDeviceDisconnected(device, status, characteristic.service, characteristic, value) }
415430
.flatMap { (wroteCharacteristic, status) ->
416-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(
431+
if (status != GATT_SUCCESS) Maybe.error(
417432
IOFailed.CharacteristicWriteFailed(
418433
status,
419434
source.device,
@@ -561,7 +576,7 @@ class RxBluetoothGattImpl(
561576
}
562577
.enqueue { device, status -> DescriptorReadDeviceDisconnected(device, status, descriptor.characteristic.service, descriptor.characteristic, descriptor) }
563578
.flatMap { (readDescriptor, status) ->
564-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(
579+
if (status != GATT_SUCCESS) Maybe.error(
565580
IOFailed.DescriptorReadingFailed(
566581
status,
567582
source.device,
@@ -591,7 +606,7 @@ class RxBluetoothGattImpl(
591606
override fun write(descriptor: BluetoothGattDescriptor, value: ByteArray, checkIfAlreadyWritten: Boolean): Maybe<BluetoothGattDescriptor> = Single
592607
.create<Pair<BluetoothGattDescriptor, Int>> { downStream ->
593608
if (checkIfAlreadyWritten && Arrays.equals(descriptor.value, value)) {
594-
downStream.onSuccess(descriptor to BluetoothGatt.GATT_SUCCESS)
609+
downStream.onSuccess(descriptor to GATT_SUCCESS)
595610
return@create
596611
}
597612

@@ -615,7 +630,7 @@ class RxBluetoothGattImpl(
615630
}
616631
.enqueue { device, status -> DescriptorWriteDeviceDisconnected(device, status, descriptor.characteristic.service, descriptor.characteristic, descriptor, value) }
617632
.flatMap { (wroteDescriptor, status) ->
618-
if (status != BluetoothGatt.GATT_SUCCESS) Maybe.error(
633+
if (status != GATT_SUCCESS) Maybe.error(
619634
IOFailed.DescriptorWriteFailed(
620635
status,
621636
source.device,

0 commit comments

Comments
 (0)