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

Commit

Permalink
Merge pull request #39 from XavierDupuis/dupuis-refactor-main-activity
Browse files Browse the repository at this point in the history
Refactor - Extract bluetooth receiver
  • Loading branch information
XavierDupuis committed Apr 8, 2023
2 parents bce864d + db3d900 commit c74b29c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
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 @@ -49,85 +45,46 @@ class MainActivity : AppCompatActivity() {
@Inject
lateinit var bluetoothRepository: BluetoothRepository

private var bluetoothStarted = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainContent(themeRepository, languageRepository)
}
bluetoothStarted = false
bluetoothRepository.bluetoothStarted = false
startBTScan()
languageRepository.getLocale = { AppCompatDelegate.getApplicationLocales() }
languageRepository.changeLocale =
{ AppCompatDelegate.setApplicationLocales(
LocaleListCompat.forLanguageTags(it.toLanguageTag())) }
}

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()
// languageRepository.updateCurrentLanguage()
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 @@ -164,16 +121,17 @@ class MainActivity : AppCompatActivity() {
}

// 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()
}
}
}
}
}

0 comments on commit c74b29c

Please sign in to comment.