@@ -210,17 +210,29 @@ class Peripheral(
210
210
gatt : BluetoothGatt ? ,
211
211
status : Int ,
212
212
) {
213
- if (status == BluetoothGatt .GATT_SUCCESS ) {
213
+ if (status == BluetoothGatt .GATT_SUCCESS && gatt != null ) {
214
214
val charsToRead =
215
- gatt?.services?.flatMap { svc ->
216
- svc.characteristics.filter { char ->
217
- char.properties and BluetoothGattCharacteristic .PROPERTY_READ != 0
215
+ gatt.services?.flatMap { svc ->
216
+ svc.characteristics.filter filter@{ char ->
217
+ if (char.properties and BluetoothGattCharacteristic .PROPERTY_READ != 0 ) {
218
+ return @filter true
219
+ }
220
+ val charUUID = char.uuid.toString().lowercase()
221
+ val charSvcUUID =
222
+ char.service.uuid
223
+ .toString()
224
+ .lowercase()
225
+ if (! discoveredCharacteristics.containsKey(charSvcUUID)) {
226
+ discoveredCharacteristics[charSvcUUID] = mutableSetOf ()
227
+ }
228
+ discoveredCharacteristics[charSvcUUID]?.add(charUUID)
229
+
230
+ return @filter false
218
231
}
219
232
}
220
233
neededCharDoneCount = charsToRead?.size ? : 0
221
234
if (neededCharDoneCount == 0 ) {
222
- connectedContinuation?.resume(Unit )
223
- connectedContinuation = null
235
+ serviceDiscoveryDone(gatt)
224
236
return
225
237
}
226
238
CoroutineScope (Dispatchers .IO ).launch {
@@ -238,6 +250,34 @@ class Peripheral(
238
250
}
239
251
}
240
252
253
+ fun serviceDiscoveryDone (gatt : BluetoothGatt ) {
254
+ _discoveredServices =
255
+ gatt.services
256
+ .filter { discoveredCharacteristics.containsKey(it.uuid.toString().lowercase()) }
257
+ .map { svc ->
258
+ hashMapOf(
259
+ " id" to svc.uuid.toString().lowercase(),
260
+ " characteristics" to
261
+ svc.characteristics
262
+ .filter {
263
+ discoveredCharacteristics[
264
+ it.service.uuid
265
+ .toString()
266
+ .lowercase(),
267
+ ]!! .contains(
268
+ it.uuid.toString().lowercase(),
269
+ )
270
+ }.map { char ->
271
+ hashMapOf(
272
+ " id" to char.uuid.toString().lowercase(),
273
+ )
274
+ },
275
+ )
276
+ }
277
+ connectedContinuation?.resume(Unit )
278
+ connectedContinuation = null
279
+ }
280
+
241
281
@Deprecated(" Deprecated in Java" )
242
282
override fun onCharacteristicRead (
243
283
gatt : BluetoothGatt ,
@@ -255,37 +295,13 @@ class Peripheral(
255
295
if (status != BluetoothGatt .GATT_SUCCESS ) {
256
296
Log .d(TAG , " error getting characteristic $charUUID : $status " )
257
297
} else {
258
- if (! discoveredCharacteristics.containsKey(characteristic.service.uuid.toString() )) {
298
+ if (! discoveredCharacteristics.containsKey(charSvcUUID )) {
259
299
discoveredCharacteristics[charSvcUUID] = mutableSetOf ()
260
300
}
261
301
discoveredCharacteristics[charSvcUUID]?.add(charUUID)
262
302
}
263
303
if (discoveredSvcDoneCount == neededCharDoneCount) {
264
- _discoveredServices =
265
- gatt.services
266
- .filter { discoveredCharacteristics.containsKey(it.uuid.toString().lowercase()) }
267
- .map { svc ->
268
- hashMapOf(
269
- " id" to svc.uuid.toString().lowercase(),
270
- " characteristics" to
271
- svc.characteristics
272
- .filter {
273
- discoveredCharacteristics[
274
- it.service.uuid
275
- .toString()
276
- .lowercase(),
277
- ]!! .contains(
278
- it.uuid.toString().lowercase(),
279
- )
280
- }.map { char ->
281
- hashMapOf(
282
- " id" to char.uuid.toString().lowercase(),
283
- )
284
- },
285
- )
286
- }
287
- connectedContinuation?.resume(Unit )
288
- connectedContinuation = null
304
+ serviceDiscoveryDone(gatt)
289
305
}
290
306
}
291
307
}
0 commit comments