This repository was archived by the owner on Jul 10, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
src/main/java/com/example/bluetoothlechat/scan Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ android {
5454dependencies {
5555 implementation fileTree(dir : " libs" , include : [" *.jar" ])
5656 implementation " org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version "
57+ implementation ' org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
5758 implementation ' androidx.core:core-ktx:1.3.2'
5859 implementation ' androidx.fragment:fragment-ktx:1.3.4'
5960 implementation ' androidx.activity:activity-ktx:1.3.0-alpha08'
Original file line number Diff line number Diff line change @@ -19,15 +19,14 @@ import android.app.Application
1919import android.bluetooth.BluetoothAdapter
2020import android.bluetooth.BluetoothDevice
2121import android.bluetooth.le.*
22- import android.os.Handler
23- import android.os.Looper
2422import android.os.ParcelUuid
2523import android.util.Log
2624import androidx.lifecycle.AndroidViewModel
2725import androidx.lifecycle.LiveData
2826import androidx.lifecycle.MutableLiveData
2927import com.example.bluetoothlechat.bluetooth.SERVICE_UUID
3028import com.example.bluetoothlechat.scan.DeviceScanViewState.*
29+ import kotlinx.coroutines.*
3130
3231
3332private const val TAG = " DeviceScanViewModel"
@@ -84,8 +83,10 @@ class DeviceScanViewModel(app: Application) : AndroidViewModel(app) {
8483 _viewState .value = ActiveScan
8584
8685 // Stop scanning after the scan period
87- Handler (Looper .myLooper()!! ).postDelayed({ stopScanning() }, SCAN_PERIOD )
88-
86+ CoroutineScope (Dispatchers .Main ).launch {
87+ delay(SCAN_PERIOD )
88+ stopScanning()
89+ }
8990 // Kick off a new scan
9091 scanCallback = DeviceScanCallback ()
9192 scanner?.startScan(scanFilters, scanSettings, scanCallback)
You can’t perform that action at this time.
0 commit comments