Commit a8d69a8e authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

MainActivity: Request location permission at runtime.

parent 0af049d5
......@@ -16,9 +16,16 @@
package com.example.card10companion
import android.bluetooth.*
import androidx.appcompat.app.AppCompatActivity
import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCallback
import android.bluetooth.BluetoothGattCharacteristic
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.example.card10companion.scanner.ScannerFragment
import java.util.*
......@@ -83,6 +90,35 @@ class MainActivity : AppCompatActivity() {
gatt = remoteDevice.connectGatt(this, false, callback)
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
)
} else {
permissionGranted()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
finish()
} else {
permissionGranted()
}
}
override fun onDestroy() {
gatt?.close();
super.onDestroy()
}
fun permissionGranted() {
var fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
if (fragment == null) {
fragment = ScannerFragment()
......@@ -91,9 +127,4 @@ class MainActivity : AppCompatActivity() {
.commit()
}
}
override fun onDestroy() {
gatt?.close();
super.onDestroy()
}
}
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