fix: request for Bluetooth permissions on startup to fetch bonded devices
This commit is contained in:
@@ -64,6 +64,8 @@ class MainActivity : AppCompatActivity() {
|
||||
DeviceListAdapter(kirbyDevices)
|
||||
}
|
||||
|
||||
private var onPermissionsGrantedCallback: Runnable? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -77,12 +79,14 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
binding.fab.setOnClickListener { view ->
|
||||
if (!hasRequiredRuntimePermissions()) {
|
||||
onPermissionsGrantedCallback = object : Runnable {
|
||||
override fun run() {
|
||||
toggleScanning()
|
||||
}
|
||||
}
|
||||
requestRelevantRuntimePermissions()
|
||||
}
|
||||
if (connectionManager.isScanning) {
|
||||
connectionManager.stopScan()
|
||||
} else {
|
||||
startBleScan()
|
||||
toggleScanning()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +94,14 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
}
|
||||
|
||||
private fun toggleScanning(): Unit {
|
||||
if (connectionManager.isScanning) {
|
||||
connectionManager.stopScan()
|
||||
} else {
|
||||
connectionManager.startScan()
|
||||
}
|
||||
}
|
||||
|
||||
private fun isKirbyDevice(device: BluetoothDevice): Boolean {
|
||||
return (device.name ?: "").lowercase().contains("kirby")
|
||||
}
|
||||
@@ -104,11 +116,25 @@ class MainActivity : AppCompatActivity() {
|
||||
animator.supportsChangeAnimations = false
|
||||
}
|
||||
|
||||
if (hasRequiredRuntimePermissions()) {
|
||||
addBondedDevices()
|
||||
} else {
|
||||
onPermissionsGrantedCallback = object : Runnable {
|
||||
override fun run() {
|
||||
addBondedDevices()
|
||||
}
|
||||
}
|
||||
requestRelevantRuntimePermissions()
|
||||
}
|
||||
|
||||
|
||||
//addDummyDevices()
|
||||
}
|
||||
|
||||
private fun addBondedDevices(): Unit {
|
||||
bluetoothAdapter.bondedDevices.filter { isKirbyDevice(it) }.forEach {
|
||||
newKirbyDevice(it)
|
||||
}
|
||||
|
||||
//addDummyDevices()
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
@@ -163,14 +189,6 @@ class MainActivity : AppCompatActivity() {
|
||||
return device
|
||||
}
|
||||
|
||||
private fun startBleScan() {
|
||||
if (!hasRequiredRuntimePermissions()) {
|
||||
requestRelevantRuntimePermissions()
|
||||
} else {
|
||||
connectionManager.startScan()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
menuInflater.inflate(R.menu.menu_main, menu)
|
||||
@@ -231,7 +249,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
allGranted && hasRequiredRuntimePermissions() -> {
|
||||
startBleScan()
|
||||
onPermissionsGrantedCallback?.run()
|
||||
}
|
||||
|
||||
else -> {
|
||||
|
||||
Reference in New Issue
Block a user