Skip to content

Commit

Permalink
fix(android): explicitly set looper for handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
pwespi committed May 23, 2021
1 parent 63237e8 commit 4ebe4fe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.os.Looper
import android.util.Log
import java.util.*
import kotlin.collections.HashMap
Expand Down Expand Up @@ -364,15 +365,15 @@ class Device(
}

private fun setTimeout(key: String, message: String, timeout: Long = DEFAULT_TIMEOUT) {
val handler = Handler()
val handler = Handler(Looper.getMainLooper())
timeoutMap[key] = handler
handler.postDelayed({
reject(key, message)
}, timeout)
}

private fun setConnectionTimeout(key: String, message: String, gatt: BluetoothGatt?, timeout: Long = CONNECTION_TIMEOUT) {
val handler = Handler()
val handler = Handler(Looper.getMainLooper())
timeoutMap[key] = handler
handler.postDelayed({
gatt?.disconnect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -95,7 +96,7 @@ class DeviceScanner(
isScanning = true
bluetoothLeScanner?.startScan(scanFilters, scanSettings, scanCallback)
if (showDialog) {
dialogHandler = Handler()
dialogHandler = Handler(Looper.getMainLooper())
showDeviceList()
} else {
savedCallback?.invoke(ScanResponse(
Expand Down Expand Up @@ -173,7 +174,7 @@ class DeviceScanner(

private fun setTimeoutForStopScanning() {
if (scanDuration != null) {
stopScanHandler = Handler()
stopScanHandler = Handler(Looper.getMainLooper())
stopScanHandler?.postDelayed(
{
stopScanning()
Expand Down

0 comments on commit 4ebe4fe

Please sign in to comment.