Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit dde21ed

Browse files
committed
change handler to coroutine
1 parent 46b8d94 commit dde21ed

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

BluetoothLeChat/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ android {
5454
dependencies {
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'

BluetoothLeChat/app/src/main/java/com/example/bluetoothlechat/scan/DeviceScanViewModel.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ import android.app.Application
1919
import android.bluetooth.BluetoothAdapter
2020
import android.bluetooth.BluetoothDevice
2121
import android.bluetooth.le.*
22-
import android.os.Handler
23-
import android.os.Looper
2422
import android.os.ParcelUuid
2523
import android.util.Log
2624
import androidx.lifecycle.AndroidViewModel
2725
import androidx.lifecycle.LiveData
2826
import androidx.lifecycle.MutableLiveData
2927
import com.example.bluetoothlechat.bluetooth.SERVICE_UUID
3028
import com.example.bluetoothlechat.scan.DeviceScanViewState.*
29+
import kotlinx.coroutines.*
3130

3231

3332
private 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)

0 commit comments

Comments
 (0)