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