From 7c4dbb7254008284c4ed226a97ff96cf1d67e0fa Mon Sep 17 00:00:00 2001 From: "ITHQ2084\\anju.mg" Date: Mon, 18 Dec 2023 18:54:04 +0530 Subject: [PATCH] Bug fix on bluetooth scanning --- .../lamp/lamp_kotlin/sensor_core/WiFi.kt | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lamp_kotlin/src/main/java/digital/lamp/lamp_kotlin/sensor_core/WiFi.kt b/lamp_kotlin/src/main/java/digital/lamp/lamp_kotlin/sensor_core/WiFi.kt index 7d300f1..531cca5 100644 --- a/lamp_kotlin/src/main/java/digital/lamp/lamp_kotlin/sensor_core/WiFi.kt +++ b/lamp_kotlin/src/main/java/digital/lamp/lamp_kotlin/sensor_core/WiFi.kt @@ -13,9 +13,12 @@ import android.content.pm.PackageManager import android.net.wifi.ScanResult import android.net.wifi.WifiInfo import android.net.wifi.WifiManager +import android.os.Handler import android.os.IBinder +import android.os.Looper import android.util.Log import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import digital.lamp.lamp_kotlin.sensor_core.utils.LampConstants import java.util.concurrent.Callable import java.util.concurrent.Executors @@ -189,6 +192,7 @@ class WiFi : Service() { if (bluetoothAdapter != null && bluetoothAdapter.isEnabled) { when (intent?.action) { BluetoothDevice.ACTION_FOUND -> { + val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) device?.let { @@ -210,6 +214,12 @@ class WiFi : Service() { class BluetoothBackgroundService : IntentService(TAG) { private val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter() + override fun onCreate() { + super.onCreate() + val filter = IntentFilter(BluetoothDevice.ACTION_FOUND) + registerReceiver(bluetoothReceiver, filter) + } + private val bluetoothReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val action = intent?.action @@ -226,6 +236,7 @@ class WiFi : Service() { val device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) device?.let { + val rowData = ContentValues() rowData.put(TIMESTAMP, System.currentTimeMillis()) rowData.put(BLUETOOTH_ADDRESS, device.address) @@ -234,7 +245,6 @@ class WiFi : Service() { ) } } - } } } @@ -246,27 +256,31 @@ class WiFi : Service() { return } + // Register for broadcasts when a device is discovered + if (ActivityCompat.checkSelfPermission( this, Manifest.permission.BLUETOOTH_SCAN ) != PackageManager.PERMISSION_GRANTED ) { - // Request Bluetooth permissions - // Handle the permission request in onRequestPermissionsResult - } else { - // Start Bluetooth discovery - val filter = IntentFilter(BluetoothDevice.ACTION_FOUND) - registerReceiver(bluetoothReceiver, filter) + + }else{ + // Start discovery bluetoothAdapter.startDiscovery() + + // The discovery will continue for 12 seconds (adjust as needed) Thread.sleep(12000) - // Stop Bluetooth discovery - bluetoothAdapter.cancelDiscovery() - // Unregister the BroadcastReceiver - unregisterReceiver(bluetoothReceiver) + // Stop discovery + bluetoothAdapter.cancelDiscovery() } + } + override fun onDestroy() { + super.onDestroy() + unregisterReceiver(bluetoothReceiver) + } } /** @@ -420,7 +434,6 @@ class WiFi : Service() { fun setInterval(frequency: Long) { this.frequency = frequency } - } } \ No newline at end of file