feat: start and stop scanning

This commit is contained in:
Fabian Christoffel
2023-06-14 17:19:40 +02:00
parent 6752e79515
commit aea69eb4d7
2 changed files with 49 additions and 3 deletions

View File

@@ -6,12 +6,16 @@ import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings
import android.content.Context import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@@ -47,11 +51,27 @@ class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding private lateinit var binding: ActivityMainBinding
private val scanSettings = ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.build()
private val bluetoothAdapter: BluetoothAdapter by lazy { private val bluetoothAdapter: BluetoothAdapter by lazy {
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter bluetoothManager.adapter
} }
private val bleScanner by lazy {
bluetoothAdapter.bluetoothLeScanner
}
private var isScanning = false
set(value) {
field = value
runOnUiThread {
binding.fab.setText(if (value) "Stop Scan" else "Start Scan")
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@@ -66,7 +86,11 @@ class MainActivity : AppCompatActivity() {
setupActionBarWithNavController(navController, appBarConfiguration) setupActionBarWithNavController(navController, appBarConfiguration)
binding.fab.setOnClickListener { view -> binding.fab.setOnClickListener { view ->
if (!isScanning) {
startBleScan() startBleScan()
} else {
stopBleScan()
}
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAnchorView(R.id.fab) // .setAnchorView(R.id.fab)
// .setAction("Action", null).show() // .setAction("Action", null).show()
@@ -159,13 +183,34 @@ class MainActivity : AppCompatActivity() {
} }
} }
@SuppressLint("MissingPermission")
val scanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
with(result.device) {
Log.i(
"ScanCallback",
"Found BLE device! Name: ${name ?: "Unnamed"}, address: $address"
)
}
}
}
@SuppressLint("MissingPermission")
private fun startBleScan() { private fun startBleScan() {
if (!hasRequiredRuntimePermissions()) { if (!hasRequiredRuntimePermissions()) {
requestRelevantRuntimePermissions() requestRelevantRuntimePermissions()
} else { /* TODO: Actually perform scan */ } else {
bleScanner.startScan(null, scanSettings, scanCallback)
isScanning = true
} }
} }
@SuppressLint("MissingPermission")
private fun stopBleScan() {
bleScanner.stopScan(scanCallback)
isScanning = false
}
private fun Activity.requestRelevantRuntimePermissions() { private fun Activity.requestRelevantRuntimePermissions() {
if (hasRequiredRuntimePermissions()) { if (hasRequiredRuntimePermissions()) {
return return

View File

@@ -21,13 +21,14 @@
<include layout="@layout/content_main" /> <include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin" android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:text='Scan'
app:srcCompat="@android:drawable/stat_sys_data_bluetooth" /> app:srcCompat="@android:drawable/stat_sys_data_bluetooth" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>