Support Android 12 and above.

This commit is contained in:
Onuray Sahin 2022-10-31 13:32:55 +03:00
parent dd49bafabb
commit 706f513baf
3 changed files with 22 additions and 2 deletions

View File

@ -12,7 +12,7 @@
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
tools:targetApi="tiramisu" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

View File

@ -20,6 +20,8 @@ import android.app.Activity
import android.bluetooth.BluetoothDevice
import android.content.Intent
import android.content.pm.PackageManager
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
@ -32,8 +34,10 @@ import android.view.ViewGroup
import android.webkit.PermissionRequest
import android.webkit.WebMessage
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.airbnb.mvrx.Fail
@ -120,6 +124,9 @@ class WidgetFragment :
if (checkPermissions(PERMISSIONS_FOR_BLUETOOTH, requireActivity(), scanBluetoothResultLauncher)) {
startBluetoothScanning()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
configureAudioDevice()
}
}
viewModel.observeViewEvents {
@ -136,6 +143,18 @@ class WidgetFragment :
viewModel.handle(WidgetAction.LoadFormattedUrl)
}
@RequiresApi(Build.VERSION_CODES.S)
private fun configureAudioDevice() {
requireContext().getSystemService<AudioManager>()?.let { audioManager ->
audioManager
.availableCommunicationDevices
.find { it.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO }
?.let { bluetoothAudioDevice ->
audioManager.setCommunicationDevice(bluetoothAudioDevice)
}
}
}
private val termsActivityResultLauncher = registerStartForActivityResult {
Timber.v("On terms results")
if (it.resultCode == Activity.RESULT_OK) {
@ -444,6 +463,7 @@ class WidgetFragment :
activity?.let {
val widgetUri = Uri.parse(fragmentArgs.baseUrl)
Timber.d("### WidgetFragment.handleBluetoothDeviceData: $event")
if (event.data contentEquals byteArrayOf(0x00)) {
views.widgetWebView.postWebMessage(WebMessage("pttr"), widgetUri)
} else if (event.data contentEquals byteArrayOf(0x01)) {

View File

@ -141,7 +141,7 @@ class BluetoothLowEnergyService : VectorAndroidService() {
private fun onCharacteristicRead(characteristic: BluetoothGattCharacteristic) {
@Suppress("DEPRECATION") val data = characteristic.value
Timber.d("### BluetoothLowEnergyService. $data")
Timber.d("### BluetoothLowEnergyService.onCharacteristicRead ${String(data)}")
if (data.isNotEmpty()) {
callback?.onCharacteristicRead(data)
}