litle fix for more correctly permision processing

This commit is contained in:
2023-09-12 21:11:38 +07:00
parent b60791af2c
commit c50c2689a5
10 changed files with 112 additions and 51 deletions

View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="3d29e5a6" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2023-06-24T21:15:01.924740400Z" />
</component>
</project>

View File

@@ -4,12 +4,20 @@ plugins {
} }
android { android {
signingConfigs {
debug {
storeFile file('/home/gogacoder/AndroidStudioProjects/keychain/key.jks')
storePassword '123456'
keyPassword '123456'
keyAlias 'key0'
}
}
namespace 'com.listerk.helible' namespace 'com.listerk.helible'
compileSdk 34 compileSdk 34
defaultConfig { defaultConfig {
applicationId "com.listerk.helible" applicationId "com.listerk.helible"
minSdk 31 minSdk 30
targetSdk 34 targetSdk 34
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"

View File

@@ -0,0 +1,20 @@
{
"version": 3,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.listerk.helible",
"variantName": "debug",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"versionCode": 1,
"versionName": "1.0",
"outputFile": "app-debug.apk"
}
],
"elementType": "File"
}

View File

@@ -2,10 +2,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" /> <!-- Legacy permissions (API 30 and below) -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<!--<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" android:maxSdkVersion="30"/>-->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.bluetooth" /> <uses-feature android:name="android.hardware.bluetooth" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" /> <uses-feature android:name="android.hardware.bluetooth_le" />
<application <application

View File

@@ -1,5 +1,5 @@
package com.listerk.helible package com.listerk.helible
object AppState { object AppState {
lateinit var bleDevice: BleDevice; lateinit var bleDevice: BleDevice
} }

View File

@@ -1,14 +1,15 @@
package com.listerk.helible package com.listerk.helible
import android.util.Log //import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout //import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
//import org.w3c.dom.Text
class BleDeviceAdapter(private val devices: List<BleDevice>, private val gotoControlBtn: Button) : class BleDeviceAdapter(private val devices: List<BleDevice>, private val gotoControlBtn: Button) :
RecyclerView.Adapter<BleDeviceAdapter.BleDeviceViewHolder>() { RecyclerView.Adapter<BleDeviceAdapter.BleDeviceViewHolder>() {
@@ -41,9 +42,9 @@ class BleDeviceAdapter(private val devices: List<BleDevice>, private val gotoCon
} }
class BleDeviceViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { class BleDeviceViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val cardView = itemView.rootView val cardView: View = itemView.rootView
val nameView = itemView.findViewById<TextView>(R.id.deviceName) val nameView: TextView = itemView.findViewById(R.id.deviceName)
val addressView = itemView.findViewById<TextView>(R.id.deviceAddress) val addressView: TextView = itemView.findViewById(R.id.deviceAddress)
val rssiView = itemView.findViewById<ImageView>(R.id.deviceRssi) val rssiView: ImageView = itemView.findViewById(R.id.deviceRssi)
} }
} }

View File

@@ -28,8 +28,7 @@ class ControlActivity : AppCompatActivity() {
sliderR1.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { sliderR1.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
sliderR1Value = progress sliderR1Value = progress
val val_text = getString(R.string.r_slider_text, 1, progress) sliderR1ValueText.text = getString(R.string.r_slider_text, 1, progress)
sliderR1ValueText.text = val_text
} }
override fun onStartTrackingTouch(p0: SeekBar?) {} override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {} override fun onStopTrackingTouch(p0: SeekBar?) {}
@@ -38,8 +37,7 @@ class ControlActivity : AppCompatActivity() {
sliderR2.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { sliderR2.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
sliderR2Value = progress sliderR2Value = progress
val val_text = getString(R.string.r_slider_text, 2, progress) sliderR2ValueText.text = getString(R.string.r_slider_text, 2, progress)
sliderR2ValueText.text = val_text
} }
override fun onStartTrackingTouch(p0: SeekBar?) {} override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {} override fun onStopTrackingTouch(p0: SeekBar?) {}
@@ -48,8 +46,7 @@ class ControlActivity : AppCompatActivity() {
sliderR3.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { sliderR3.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
sliderR3Value = progress sliderR3Value = progress
val val_text = getString(R.string.r_slider_text, 3, progress) sliderR3ValueText.text = getString(R.string.r_slider_text, 3, progress)
sliderR3ValueText.text = val_text
} }
override fun onStartTrackingTouch(p0: SeekBar?) {} override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {} override fun onStopTrackingTouch(p0: SeekBar?) {}

View File

@@ -1,6 +1,7 @@
package com.listerk.helible package com.listerk.helible
import android.Manifest import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.bluetooth.le.BluetoothLeScanner import android.bluetooth.le.BluetoothLeScanner
@@ -13,16 +14,17 @@ import android.graphics.drawable.Animatable
import android.os.Build import android.os.Build
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.util.Log import android.util.Log
//import android.os.Handler
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import androidx.annotation.RequiresApi //import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@SuppressLint("MissingPermission")
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
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
@@ -31,7 +33,7 @@ class MainActivity : AppCompatActivity() {
private val bleScanner: BluetoothLeScanner by lazy { private val bleScanner: BluetoothLeScanner by lazy {
bluetoothAdapter.bluetoothLeScanner bluetoothAdapter.bluetoothLeScanner
} }
private val requiredPermissions: MutableSet<String> = mutableSetOf(Manifest.permission.BLUETOOTH_SCAN)
private val foundDevices = mutableListOf<BleDevice>() private val foundDevices = mutableListOf<BleDevice>()
private val leScanCallback: ScanCallback = object : ScanCallback() { private val leScanCallback: ScanCallback = object : ScanCallback() {
@@ -39,6 +41,7 @@ class MainActivity : AppCompatActivity() {
super.onScanResult(callbackType, result) super.onScanResult(callbackType, result)
val bleDevicesView = findViewById<RecyclerView>(R.id.bleDevicesView) val bleDevicesView = findViewById<RecyclerView>(R.id.bleDevicesView)
val loader = findViewById<ImageView>(R.id.bleDevicesLoader) val loader = findViewById<ImageView>(R.id.bleDevicesLoader)
if (result.device.name.isNullOrBlank()) { if (result.device.name.isNullOrBlank()) {
return return
} }
@@ -48,7 +51,7 @@ class MainActivity : AppCompatActivity() {
loader.visibility = View.GONE loader.visibility = View.GONE
} }
var deviceIndex = foundDevices.indexOfFirst { it.address == result.device.address } var deviceIndex: Int = foundDevices.indexOfFirst { it.address == result.device.address }
if (deviceIndex < 0) { if (deviceIndex < 0) {
deviceIndex = foundDevices.size deviceIndex = foundDevices.size
foundDevices.add(BleDevice(result.device.name, result.device.address, result.rssi)) foundDevices.add(BleDevice(result.device.name, result.device.address, result.rssi))
@@ -76,23 +79,62 @@ class MainActivity : AppCompatActivity() {
bleDevicesView.layoutManager = LinearLayoutManager(this) bleDevicesView.layoutManager = LinearLayoutManager(this)
bleDevicesView.adapter = BleDeviceAdapter(foundDevices, gotoControlButton) bleDevicesView.adapter = BleDeviceAdapter(foundDevices, gotoControlButton)
bleDevicesView.itemAnimator = null bleDevicesView.itemAnimator = null
if (ActivityCompat.checkSelfPermission(this@MainActivity, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED)
{
Log.e("StartActivity", "We have denied permissions")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
{
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.BLUETOOTH_CONNECT), 2)
return
}
}
/*if(Build.VERSION.SDK_INT < 31) {
requiredPermissions.add(android.Manifest.permission.ACCESS_FINE_LOCATION)
requiredPermissions.add(android.Manifest.permission.ACCESS_COARSE_LOCATION)
requiredPermissions.add(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)
}
requestPermissions()*/
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { /*if (!hasNecessaryPermissions()) {
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), 1) requestPermissions()
} else { } else {*/
bleScanner.startScan(leScanCallback) bleScanner.startScan(leScanCallback)
} //}
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { /*if (!hasNecessaryPermissions()) {
ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), 1) requestPermissions()
} else { } else {*/
bleScanner.stopScan(leScanCallback) bleScanner.stopScan(leScanCallback)
//}
}
private fun hasNecessaryPermissions(): Boolean
{
for (it in requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED) {
Log.i("ScanActivity", "Permission isn't allowed: $it")
return false
}
}
return true
}
private fun requestPermissions()
{
Log.i("ScanActivity", "Trying to request permissions: $requiredPermissions")
ActivityCompat.requestPermissions(this@MainActivity, requiredPermissions.toTypedArray(), 1)
if(!hasNecessaryPermissions())
{
Log.e("ScanActivity", "Failed to get all permissions")
} else {
Log.i("ScanActivity", "App got permissions: $requiredPermissions")
} }
} }
} }

View File

@@ -22,10 +22,11 @@
android:id="@+id/bleDevicesView" android:id="@+id/bleDevicesView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/devicesTitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="12dp" android:layout_marginTop="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/devicesTitle"
tools:listitem="@layout/item_ble_device" /> tools:listitem="@layout/item_ble_device" />
<ImageView <ImageView
@@ -33,9 +34,10 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/loader" android:src="@drawable/loader"
app:layout_constraintTop_toBottomOf="@id/bleDevicesView" android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/> app:layout_constraintTop_toBottomOf="@id/bleDevicesView" />
<Button <Button
android:id="@+id/gotoControlButton" android:id="@+id/gotoControlButton"

View File

@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '8.0.2' apply false id 'com.android.application' version '8.1.0' apply false
id 'com.android.library' version '8.0.2' apply false id 'com.android.library' version '8.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
} }