Commit 807fbd8c authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

MainActivity: Enable Bluetooth if disabled.

parent 1e7cab48
......@@ -23,7 +23,9 @@
package de.ccc.events.badge.card10
import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
......@@ -36,29 +38,54 @@ import de.ccc.events.badge.card10.installer.InstallerFragment
import de.ccc.events.badge.card10.scanner.ScannerFragment
import java.util.concurrent.TimeUnit
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 1
private const val REQUEST_CODE_PERMISSIONS = 2
class MainActivity : AppCompatActivity() {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val permission = ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
)
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
0
)
maybePermissionGranted()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE_ENABLE_BLUETOOTH) {
if (resultCode == Activity.RESULT_OK)
maybePermissionGranted()
else
finish()
} else {
permissionGranted()
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
finish()
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (!grantResults.isEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
maybePermissionGranted()
else
finish()
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
fun maybePermissionGranted() {
if (!bluetoothAdapter.isEnabled) {
startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), REQUEST_CODE_ENABLE_BLUETOOTH)
} else if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
REQUEST_CODE_PERMISSIONS
)
} else {
permissionGranted()
}
......@@ -78,8 +105,7 @@ class MainActivity : AppCompatActivity() {
}
val workManager = WorkManager.getInstance()
if (!BluetoothAdapter
.getDefaultAdapter().bondedDevices.isEmpty()
if (!bluetoothAdapter.bondedDevices.isEmpty()
) {
val setClockRequest =
PeriodicWorkRequestBuilder<SetClockJob>(1, TimeUnit.MINUTES)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment