@@ -9,6 +9,7 @@ import f.cking.software.domain.model.DeviceData
9
9
import f.cking.software.domain.model.DeviceMetadata
10
10
import f.cking.software.domain.model.DeviceMetadata.CharacteristicType
11
11
import f.cking.software.domain.model.DeviceMetadata.ServiceTypes
12
+ import f.cking.software.domain.model.isNullOrEmpty
12
13
import f.cking.software.fromBase64
13
14
import kotlinx.coroutines.Dispatchers
14
15
import kotlinx.coroutines.FlowPreview
@@ -72,6 +73,12 @@ class FetchDeviceServiceInfo(
72
73
}
73
74
}
74
75
76
+ fun throwIfMetadataNotUpdated (e : Exception ) {
77
+ if (metadata.isNullOrEmpty() || metadata == device.metadata) {
78
+ throw e
79
+ }
80
+ }
81
+
75
82
bleScannerHelper.connectToDevice(device.address)
76
83
.collect { event ->
77
84
when (event) {
@@ -161,38 +168,38 @@ class FetchDeviceServiceInfo(
161
168
// Error handling
162
169
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .UnspecifiedConnectionError -> {
163
170
Timber .tag(TAG ).e(" Unspecified connection error from ${device.address} ." )
164
- disconnect( event.gatt )
165
- throw BluetoothConnectionException . UnspecifiedConnectionError (event.errorCode )
171
+ throwIfMetadataNotUpdated( BluetoothConnectionException . UnspecifiedConnectionError ( event.errorCode) )
172
+ submitMetadata( )
166
173
}
167
174
168
175
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .ConnectionTimeout -> {
169
176
Timber .tag(TAG ).e(" Connection timeout error from ${device.address} " )
170
- disconnect( event.gatt )
171
- throw BluetoothConnectionException . ConnectionTimeoutException (event.errorCode )
177
+ throwIfMetadataNotUpdated( BluetoothConnectionException . ConnectionTimeoutException ( event.errorCode) )
178
+ submitMetadata( )
172
179
}
173
180
174
181
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .ConnectionFailedToEstablish -> {
175
182
Timber .tag(TAG ).e(" Connection failed to establish error from ${device.address} " )
176
- disconnect( event.gatt )
177
- throw BluetoothConnectionException . ConnectionFailedToEstablish (event.errorCode )
183
+ throwIfMetadataNotUpdated( BluetoothConnectionException . ConnectionFailedToEstablish ( event.errorCode) )
184
+ submitMetadata( )
178
185
}
179
186
180
187
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .ConnectionFailedBeforeInitializing -> {
181
188
Timber .tag(TAG ).e(" Connection initializing failed error from ${device.address} " )
182
- disconnect( event.gatt )
183
- throw BluetoothConnectionException . ConnectionInitializingFailed (event.errorCode )
189
+ throwIfMetadataNotUpdated( BluetoothConnectionException . ConnectionInitializingFailed ( event.errorCode) )
190
+ submitMetadata( )
184
191
}
185
192
186
193
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .ConnectionTerminated -> {
187
194
Timber .tag(TAG ).e(" Connection terminated error from ${device.address} . Probably max GATT connections reached" )
188
- disconnect( event.gatt )
189
- throw BluetoothConnectionException . ConnectionTerminated (event.errorCode )
195
+ throwIfMetadataNotUpdated( BluetoothConnectionException . ConnectionTerminated ( event.errorCode) )
196
+ submitMetadata( )
190
197
}
191
198
192
199
is BleScannerHelper .DeviceConnectResult .DisconnectedWithError .ConnectionFailedTooManyClients -> {
193
200
Timber .tag(TAG ).e(" Connection failed due to too many clients error from ${device.address} " )
194
- disconnect( event.gatt )
195
- throw BluetoothConnectionException . TooManyClients (event.errorCode )
201
+ throwIfMetadataNotUpdated( BluetoothConnectionException . TooManyClients ( event.errorCode) )
202
+ submitMetadata( )
196
203
}
197
204
198
205
else -> {
0 commit comments