Skip to content

Commit 3a74b34

Browse files
authored
Merge pull request #203 from NordicSemiconductor/fixes/sample
Sample app fixes
2 parents 36f1883 + 24a3d33 commit 3a74b34

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
<manifest xmlns:tools="http://schemas.android.com/tools"
33
xmlns:android="http://schemas.android.com/apk/res/android">
44

5-
<!--
65
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
7-
tools:remove="android:usesPermissionFlags"/>
6+
android:usesPermissionFlags="neverForLocation"
7+
tools:targetApi="31" />
88

9+
<!--
910
<uses-permission
1011
android:name="android.permission.ACCESS_COARSE_LOCATION"
1112
tools:remove="android:maxSdkVersion" />

sample/src/main/java/no/nordicsemi/kotlin/ble/android/sample/scanner/ScannerViewModel.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import kotlinx.coroutines.delay
4141
import kotlinx.coroutines.flow.MutableStateFlow
4242
import kotlinx.coroutines.flow.StateFlow
4343
import kotlinx.coroutines.flow.asStateFlow
44+
import kotlinx.coroutines.flow.buffer
4445
import kotlinx.coroutines.flow.catch
4546
import kotlinx.coroutines.flow.filterNot
4647
import kotlinx.coroutines.flow.filterNotNull
@@ -49,6 +50,7 @@ import kotlinx.coroutines.flow.map
4950
import kotlinx.coroutines.flow.onCompletion
5051
import kotlinx.coroutines.flow.onEach
5152
import kotlinx.coroutines.flow.onEmpty
53+
import kotlinx.coroutines.flow.onStart
5254
import kotlinx.coroutines.flow.update
5355
import kotlinx.coroutines.launch
5456
import kotlinx.coroutines.withTimeout
@@ -61,13 +63,11 @@ import no.nordicsemi.kotlin.ble.core.ConnectionState
6163
import no.nordicsemi.kotlin.ble.core.Phy
6264
import no.nordicsemi.kotlin.ble.core.PhyInUse
6365
import no.nordicsemi.kotlin.ble.core.WriteType
64-
import no.nordicsemi.kotlin.ble.core.util.fromShortUuid
6566
import timber.log.Timber
6667
import javax.inject.Inject
6768
import kotlin.time.Duration.Companion.milliseconds
6869
import kotlin.time.Duration.Companion.seconds
6970
import kotlin.uuid.ExperimentalUuidApi
70-
import kotlin.uuid.Uuid
7171

7272
@HiltViewModel
7373
class ScannerViewModel @Inject constructor(
@@ -102,29 +102,27 @@ class ScannerViewModel @Inject constructor(
102102

103103
@OptIn(ExperimentalUuidApi::class)
104104
fun onScanRequested() {
105-
_isScanning.update { true }
106105
scanningJob = centralManager
107106
.scan(5000.milliseconds) {
108-
Any {
109-
ManufacturerData(0x0059)
110-
ServiceUuid(Uuid.fromShortUuid(0x1809))
111-
}
107+
// Any {
108+
// ManufacturerData(0x0059)
109+
// ServiceUuid(Uuid.fromShortUuid(0x1809))
110+
// }
112111
Any {
113112
Name("Pixel 5")
114113
Name("Pixel 7")
115-
Name("Nordic_LBS")
116-
Name("Nordic_Buttonless")
117114
Name("DFU1A06")
118-
Name("Mesh Light")
119115
Name("nRFConnect")
120116
Name("HR Sensor")
121117
Name(Regex("Mesh.*"))
118+
Name(Regex("Nordic.*"))
122119
}
123120
}
124-
.distinctByPeripheral()
125-
.map {
126-
it.peripheral
121+
.onStart {
122+
_isScanning.update { true }
127123
}
124+
.distinctByPeripheral()
125+
.map { it.peripheral }
128126
.filterNot { _devices.value.contains(it) }
129127
//.distinct()
130128
.onEach { newPeripheral ->
@@ -185,6 +183,7 @@ class ScannerViewModel @Inject constructor(
185183
observerServices(peripheral, this)
186184
} catch (e: Exception) {
187185
Timber.e(e, "Connection attempt failed")
186+
connectionScopeMap.remove(peripheral)?.cancel()
188187
}
189188
}
190189
}
@@ -356,6 +355,7 @@ class ScannerViewModel @Inject constructor(
356355

357356
private fun observePeripheralState(peripheral: Peripheral, scope: CoroutineScope) {
358357
peripheral.state
358+
.buffer()
359359
.onEach {
360360
Timber.i("State of $peripheral: $it")
361361

0 commit comments

Comments
 (0)