@@ -3,6 +3,7 @@ package com.beepiz.bluetooth.gattcoroutines
3
3
import android.bluetooth.*
4
4
import android.os.Build.VERSION.SDK_INT
5
5
import androidx.annotation.RequiresApi
6
+ import com.beepiz.bluetooth.gattcoroutines.extensions.offerCatching
6
7
import kotlinx.coroutines.*
7
8
import kotlinx.coroutines.channels.*
8
9
import kotlinx.coroutines.sync.Mutex
@@ -48,8 +49,8 @@ internal class GattConnectionImpl(
48
49
private val isConnectedBroadcastChannel = ConflatedBroadcastChannel (false )
49
50
private val isConnectedChannel get() = isConnectedBroadcastChannel.openSubscription()
50
51
override var isConnected: Boolean
51
- get() = ! isClosed && isConnectedBroadcastChannel.value
52
- private set(value) = isConnectedBroadcastChannel.offer (value).let { Unit }
52
+ get() = runCatching { ! isClosed && isConnectedBroadcastChannel.value }.getOrDefault( false )
53
+ private set(value) = isConnectedBroadcastChannel.offerCatching (value).let { Unit }
53
54
private var isClosed = false
54
55
private var closedException: ConnectionClosedException ? = null
55
56
private val stateChangeBroadcastChannel =
@@ -102,30 +103,20 @@ internal class GattConnectionImpl(
102
103
}
103
104
104
105
override fun close (notifyStateChangeChannel : Boolean ) {
105
- closeInternal(notifyStateChangeChannel,
106
- ConnectionClosedException ()
107
- )
106
+ closeInternal(notifyStateChangeChannel, ConnectionClosedException ())
108
107
}
109
108
110
109
private fun closeInternal (notifyStateChangeChannel : Boolean , cause : ConnectionClosedException ) {
111
110
closedException = cause
112
111
bluetoothGatt?.close()
113
112
isClosed = true
114
- try {
115
- isConnected = false
116
- if (notifyStateChangeChannel) {
117
- stateChangeBroadcastChannel.offer(
118
- GattConnection .StateChange (
119
- STATUS_SUCCESS ,
120
- BluetoothProfile .STATE_DISCONNECTED
121
- )
122
- )
123
- }
124
- } catch (ignored: ConnectionClosedException ) {
125
- // isConnected property is delegated by a channel that throws if already closed,
126
- // but we don't need the exception to be thrown again here, so we ignore it.
127
- // We do the same for stateChangeBroadcastChannel.offer(…) call.
128
- }
113
+ isConnected = false
114
+ if (notifyStateChangeChannel) stateChangeBroadcastChannel.offerCatching(
115
+ element = GattConnection .StateChange (
116
+ status = STATUS_SUCCESS ,
117
+ newState = BluetoothProfile .STATE_DISCONNECTED
118
+ )
119
+ )
129
120
isConnectedBroadcastChannel.close(cause)
130
121
rssiChannel.close(cause)
131
122
servicesDiscoveryChannel.close(cause)
@@ -202,7 +193,7 @@ internal class GattConnectionImpl(
202
193
when (status) {
203
194
STATUS_SUCCESS -> isConnected = newState == BluetoothProfile .STATE_CONNECTED
204
195
}
205
- stateChangeBroadcastChannel.offer (
196
+ stateChangeBroadcastChannel.offerCatching (
206
197
GattConnection .StateChange (
207
198
status,
208
199
newState
@@ -251,7 +242,8 @@ internal class GattConnectionImpl(
251
242
GattConnection .Phy (
252
243
txPhy,
253
244
rxPhy
254
- ), status)
245
+ ), status
246
+ )
255
247
}
256
248
}
257
249
@@ -305,9 +297,7 @@ internal class GattConnectionImpl(
305
297
306
298
@Suppress(" NOTHING_TO_INLINE" )
307
299
private inline fun checkNotClosed () {
308
- if (isClosed) throw ConnectionClosedException (
309
- closedException
310
- )
300
+ if (isClosed) throw ConnectionClosedException (closedException)
311
301
}
312
302
313
303
private class GattResponse <out E >(val e : E , val status : Int ) {
0 commit comments