Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Refactor - Extract bluetooth receiver #39

Merged
merged 3 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.example.bluetoothdetector

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.os.Bundle
Expand Down Expand Up @@ -37,80 +33,41 @@ class MainActivity : ComponentActivity() {
@Inject
lateinit var bluetoothRepository: BluetoothRepository

private var bluetoothStarted = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent(themeRepository)
}
bluetoothStarted = false
bluetoothRepository.bluetoothStarted = false
startBTScan()
}

private val btReceiver = object : BroadcastReceiver() {
@SuppressLint("MissingPermission")
override fun onReceive(context: Context, intent: Intent) {
// Permission check
if (ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.BLUETOOTH_ADMIN
) != PackageManager.PERMISSION_GRANTED
) {
return
}
if (android.os.Build.VERSION.SDK_INT >= 31 && ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.BLUETOOTH_SCAN
) != PackageManager.PERMISSION_GRANTED
) {
return
}
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
val device: BluetoothDevice? =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)

bluetoothRepository.addDeviceToList(device)
}
ACTION_DISCOVERY_FINISHED -> {
// When bluetooth scan ends, restart it
if (bluetoothStarted) {
bluetoothRepository.startDiscovery()
}
}
}
}
}

override fun onResume() {
// Start bluetooth scan when app is resumed
super.onResume()
locationRepository.resumeLocationUpdatesAsync()
// Start bluetooth scan when app is resumed
startBTScan()
}

override fun onPause() {
// Stop bluetooth scan when app is paused
super.onPause()
locationRepository.pauseLocationUpdatesAsync()
if (bluetoothStarted) {
// Stop bluetooth scan when app is paused
if (bluetoothRepository.bluetoothStarted) {
bluetoothRepository.stopDiscovery()
bluetoothStarted = false
bluetoothRepository.bluetoothStarted = false
}
}

override fun onDestroy() {
// Stop bluetooth scan and unregister the intent receiver when app is destroyed
super.onDestroy()
if (bluetoothStarted) {
// Stop bluetooth scan and unregister the intent receiver when app is destroyed
if (bluetoothRepository.bluetoothStarted) {
bluetoothRepository.stopDiscovery()
bluetoothStarted = false
bluetoothRepository.bluetoothStarted = false
}
if (btReceiver != null) {
unregisterReceiver(btReceiver)
if (bluetoothRepository.bluetoothReceiver != null) {
unregisterReceiver(bluetoothRepository.bluetoothReceiver)
}
}

Expand Down Expand Up @@ -147,16 +104,17 @@ class MainActivity : ComponentActivity() {
}

// Only starts Bluetooth scan once
if (!bluetoothStarted) {
bluetoothStarted = true
if (!bluetoothRepository.bluetoothStarted) {
bluetoothRepository.bluetoothStarted = true
} else {
return
}
// Register for broadcasts when a device is discovered.
val filter = IntentFilter()
filter.addAction(BluetoothDevice.ACTION_FOUND)
filter.addAction(ACTION_DISCOVERY_FINISHED)
registerReceiver(btReceiver, filter)
val filter = IntentFilter().apply {
addAction(BluetoothDevice.ACTION_FOUND)
addAction(ACTION_DISCOVERY_FINISHED)
}
registerReceiver(bluetoothRepository.bluetoothReceiver, filter)
bluetoothRepository.startDiscovery()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.pm.PackageManager
import android.location.Location
import androidx.core.app.ActivityCompat
import com.example.bluetoothdetector.main.model.Device
import com.example.bluetoothdetector.main.sources.BluetoothReceiver
import java.util.*
import kotlin.math.cos
import kotlin.math.sin
Expand All @@ -18,9 +19,14 @@ class BluetoothRepository(
private val deviceRepository: DeviceRepository,
private val locationRepository: LocationRepository
) {
var bluetoothStarted = false

private val bluetoothManager: BluetoothManager =
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager

val bluetoothReceiver =
BluetoothReceiver(context, this)

// Starts a bluetooth discovery scan
fun startDiscovery() {
// Permission check
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.bluetoothdetector.main.sources

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.example.bluetoothdetector.main.repository.BluetoothRepository

open class BluetoothReceiver(
private val applicationContext: Context,
private val bluetoothRepository: BluetoothRepository
) : BroadcastReceiver() {
@SuppressLint("MissingPermission")
override fun onReceive(context: Context, intent: Intent) {
// Permission check
if (ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.BLUETOOTH_ADMIN
) != PackageManager.PERMISSION_GRANTED
) {
return
}
if (android.os.Build.VERSION.SDK_INT >= 31 && ActivityCompat.checkSelfPermission(
applicationContext,
Manifest.permission.BLUETOOTH_SCAN
) != PackageManager.PERMISSION_GRANTED
) {
return
}
when (intent.action) {
BluetoothDevice.ACTION_FOUND -> {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
val device: BluetoothDevice? =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)

bluetoothRepository.addDeviceToList(device)
}
BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
// When bluetooth scan ends, restart it
if (bluetoothRepository.bluetoothStarted) {
bluetoothRepository.startDiscovery()
}
}
}
}
}