@@ -43,12 +43,14 @@ import kotlinx.coroutines.flow.catch
4343import kotlinx.coroutines.flow.combine
4444import kotlinx.coroutines.flow.filter
4545import kotlinx.coroutines.flow.launchIn
46+ import kotlinx.coroutines.flow.map
4647import kotlinx.coroutines.flow.onEach
4748import kotlinx.coroutines.flow.onStart
4849import no.nordicsemi.android.common.ui.scanner.repository.DevicesScanFilter
4950import no.nordicsemi.android.common.ui.scanner.repository.ScannerRepository
5051import no.nordicsemi.android.common.ui.scanner.repository.ScanningState
51- import no.nordicsemi.android.kotlin.ble.core.ServerDevice
52+ import no.nordicsemi.android.kotlin.ble.scanner.aggregator.BleScanResultAggregator
53+ import no.nordicsemi.android.kotlin.ble.scanner.data.AggregatedBleScanItemWithRecord
5254import no.nordicsemi.android.kotlin.ble.scanner.errors.ScanFailedError
5355import no.nordicsemi.android.kotlin.ble.scanner.errors.ScanningFailedException
5456import javax.inject.Inject
@@ -80,15 +82,17 @@ internal class ScannerViewModel @Inject constructor(
8082
8183 private fun relaunchScanning () {
8284 currentJob?.cancel()
85+ val aggregator = BleScanResultAggregator ()
8386 currentJob = scannerRepository.getScannerState()
87+ .map { aggregator.aggregate(it) }
8488 .filter { it.isNotEmpty() }
8589 .combine(filterConfig) { result, config ->
8690 result.applyFilters(config)
8791 }
8892 .onStart { _state .value = ScanningState .Loading }
8993 .cancellable()
9094 .onEach {
91- _state .value = ScanningState .DevicesDiscovered (it)
95+ _state .value = ScanningState .DevicesDiscovered (it.map { it.device } )
9296 }
9397 .catch {
9498 _state .value = (it as ? ScanningFailedException )?.let {
@@ -101,11 +105,10 @@ internal class ScannerViewModel @Inject constructor(
101105 // This can't be observed in View Model Scope, as it can exist even when the
102106 // scanner is not visible. Scanner state stops scanning when it is not observed.
103107 // .stateIn(viewModelScope, SharingStarted.Lazily, ScanningState.Loading)
104- private fun List<ServerDevice >.applyFilters (config : DevicesScanFilter ) =
105- filter { ! config.filterUuidRequired || it.serviceUuids.contains(uuid) }
108+ private fun List<AggregatedBleScanItemWithRecord >.applyFilters (config : DevicesScanFilter ) =
109+ filter { ! config.filterUuidRequired || it.lastScanResult?.scanRecord?. serviceUuids? .contains(uuid) == true }
106110 .filter { ! config.filterNearbyOnly || it.highestRssi >= FILTER_RSSI }
107- .filter { ! config.filterWithNames || it.hasName }
108-
111+ .filter { ! config.filterWithNames || it.device.name.isNotEmpty() }
109112
110113 fun setFilterUuid (uuid : ParcelUuid ? ) {
111114 this .uuid = uuid
0 commit comments