diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 32fc0f68c..fa6a2b6ac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,13 +18,14 @@ tools:ignore="ScopedStorage" /> - + + + android:usesPermissionFlags="neverForLocation" tools:targetApi="S" /> + android:maxSdkVersion="28" /> @@ -32,7 +33,6 @@ - diff --git a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/BluetoothHandler.kt b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/BluetoothHandler.kt index 339e7e549..61bc0e3d9 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/BluetoothHandler.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/BluetoothHandler.kt @@ -42,7 +42,7 @@ class BluetoothHandler( ) { private var mBluetoothAdapter: BluetoothAdapter? = null private val listener: BluetoothListener - private var onRequestLocationQ: ActivityResultLauncher> + private var onRequestLocationQ: ActivityResultLauncher private var onRequestBluetoothS: ActivityResultLauncher> private var onRequestBluetooth: ActivityResultLauncher private var onRequestLocation: ActivityResultLauncher @@ -50,14 +50,9 @@ class BluetoothHandler( init { onRequestLocationQ = registry.register( - "LocationQ", ActivityResultContracts.RequestMultiplePermissions() - ) { permissions: Map -> - var isLocationAvailable = false - permissions.entries.forEach { - if (it.key == Manifest.permission.ACCESS_FINE_LOCATION) - isLocationAvailable = it.value - } - if (isLocationAvailable) requestBluetooth(context) else listener.onPermissionsFailed() + "LocationQ", ActivityResultContracts.RequestPermission() + ) { granted -> + if (granted) requestBluetooth(context) else listener.onPermissionsFailed() } onRequestBluetoothS = registry.register( "BluetoothS", ActivityResultContracts.RequestMultiplePermissions() @@ -110,7 +105,7 @@ class BluetoothHandler( .setPositiveButton(R.string.accept) { dialog: DialogInterface, _: Int -> dialog.dismiss() if (Version.isQuinceTart) - onRequestLocationQ.launch(PERMISSIONS_LOCATION) + onRequestLocationQ.launch(PERMISSION_FINE_LOCATION) else onRequestLocation.launch(PERMISSION_COURSE_LOCATION) } @@ -143,7 +138,7 @@ class BluetoothHandler( if (BuildConfig.GOOGLE_PLAY) showLocationRequest(activity) else - onRequestLocationQ.launch(PERMISSIONS_LOCATION) + onRequestLocationQ.launch(PERMISSION_FINE_LOCATION) } } } @@ -229,10 +224,6 @@ class BluetoothHandler( companion object { private const val PERMISSION_COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION private const val PERMISSION_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION - private val PERMISSIONS_LOCATION = arrayOf( - PERMISSION_FINE_LOCATION, - PERMISSION_COURSE_LOCATION - ) @RequiresApi(Build.VERSION_CODES.S) private val PERMISSIONS_BLUETOOTH = arrayOf( Manifest.permission.BLUETOOTH_SCAN, diff --git a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt index 8e1aa8f64..74b5de74b 100644 --- a/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt +++ b/app/src/main/java/com/hiddenramblings/tagmo/bluetooth/GattService.kt @@ -478,10 +478,12 @@ class GattService : Service() { getCharacteristicValue(characteristic, value) } + @Deprecated("Deprecated in Java", ReplaceWith("if (status == BluetoothGatt.GATT_SUCCESS) getCharacteristicValue(characteristic)", "android.bluetooth.BluetoothGatt")) override fun onCharacteristicRead( gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int ) { - if (status == BluetoothGatt.GATT_SUCCESS) getCharacteristicValue(characteristic) + if (status == BluetoothGatt.GATT_SUCCESS) + getCharacteristicValue(characteristic) } override fun onCharacteristicWrite( @@ -508,6 +510,7 @@ class GattService : Service() { } } + @Deprecated("Deprecated in Java", ReplaceWith("getCharacteristicValue(characteristic)")) override fun onCharacteristicChanged( gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic ) { @@ -640,40 +643,13 @@ class GattService : Service() { setResponseDescriptors(characteristic) } - /** - * Retrieves a list of supported GATT services on the connected device. This should be - * invoked only after `BluetoothGatt#discoverServices()` completes successfully. - * - * @return A `List` of supported services. - */ - private val supportedGattServices: List? - get() = mBluetoothGatt?.services - @Throws(IllegalAccessException::class, UnsupportedOperationException::class) fun setPuckServicesUUID() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter)) } - val services = supportedGattServices - if (services.isNullOrEmpty()) { - throw UnsupportedOperationException( - getString(R.string.gatt_services_null, serviceType.logTag) - ) - } - for (customService in services) { - when (customService.uuid) { - Nordic.NUS -> { - legacyInterface = false - break - } - Nordic.LegacyNUS -> { - legacyInterface = true - break - } - else -> { - continue - } - } + legacyInterface = mBluetoothGatt!!.services.any { service -> + service.characteristics.any { it.uuid == Nordic.LegacyNUS } } setCharacteristicRX() } @@ -682,35 +658,16 @@ class GattService : Service() { if (mBluetoothAdapter == null || mBluetoothGatt == null) { throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter)) } - val services = supportedGattServices - if (services.isNullOrEmpty()) { - throw UnsupportedOperationException( - getString(R.string.gatt_services_null, serviceType.logTag) - ) - } - for (customService in services) { - when (customService.uuid) { - Nordic.NUS -> { - omllboInterface = false - break - } - Nordic.OmllboNUS -> { - omllboInterface = true - break - } - else -> { - continue - } - } + omllboInterface = mBluetoothGatt!!.services.any { service -> + service.characteristics.any { it.uuid == Nordic.OmllboNUS } } setCharacteristicRX() } @Throws(IllegalAccessException::class, UnsupportedOperationException::class) fun setCharacteristicRX() { - if (mBluetoothAdapter == null || mBluetoothGatt == null) { + if (mBluetoothAdapter == null || mBluetoothGatt == null) throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter)) - } mCharacteristicRX = mBluetoothGatt!!.getService(GattNUS).getCharacteristic(GattRX) ?: mBluetoothGatt!!.services.find { service -> service.characteristics.any { @@ -725,9 +682,8 @@ class GattService : Service() { @Throws(IllegalAccessException::class, UnsupportedOperationException::class) fun setCharacteristicTX() { - if (mBluetoothAdapter == null || mBluetoothGatt == null) { + if (mBluetoothAdapter == null || mBluetoothGatt == null) throw IllegalAccessException(getString(R.string.fail_bluetooth_adapter)) - } mCharacteristicTX = mBluetoothGatt!!.getService(GattNUS).getCharacteristic(GattTX) ?: mBluetoothGatt!!.services.find { service -> service.characteristics.any { @@ -1244,23 +1200,20 @@ class GattService : Service() { companion object { private var legacyInterface = false private var omllboInterface = false - val GattNUS: UUID = if (legacyInterface) - Nordic.LegacyNUS - else if (omllboInterface) - Nordic.OmllboNUS - else - Nordic.NUS - val GattTX: UUID = if (legacyInterface) - Nordic.LegacyTX - else if (omllboInterface) - Nordic.OmllboTX - else - Nordic.TX - val GattRX: UUID = if (legacyInterface) - Nordic.LegacyRX - else if (omllboInterface) - Nordic.OmllboRX - else - Nordic.RX + val GattNUS: UUID = when { + legacyInterface -> Nordic.LegacyNUS + omllboInterface -> Nordic.OmllboNUS + else -> Nordic.NUS + } + val GattTX: UUID = when { + legacyInterface -> Nordic.LegacyTX + omllboInterface -> Nordic.OmllboTX + else -> Nordic.TX + } + val GattRX: UUID = when { + legacyInterface -> Nordic.LegacyRX + omllboInterface -> Nordic.OmllboRX + else -> Nordic.RX + } } } \ No newline at end of file