kotlin 蓝牙SCO无法在Android 12设备中连接

gstyhher  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(245)

我有奇怪的问题,在Android应用程序音频通话只在Android 12设备.
当我在设备中连接蓝牙进行呼叫时,音频是流动的,我可以听到蓝牙设备中的音频。但当我试图在连接的蓝牙设备和扬声器之间切换时,它在Android 11及以下设备中工作完美。
但对于Android 12它不能正常工作。当我试图从扬声器切换到蓝牙设备时没有音频。我可以听到扬声器中的声音。
在检查了Android文档之后,我甚至添加了代码来请求使用蓝牙连接的权限。但是仍然尝试在OS12设备中切换音频,仍然没有音频。我确实理解OS12缺少了一些东西。

<!--BLUETOOTH PERMISSION-->
<uses-permission android:name="android.permission.BLUETOOTH" />
    <!-- Request legacy Bluetooth permissions on older devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!-- Needed only if your app looks for Bluetooth devices.
             If your app doesn't use Bluetooth scan results to derive physical
             location information, you can strongly assert that your app
             doesn't derive physical location. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <!-- Needed only if your app makes the device discoverable to Bluetooth
      devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <!-- Needed only if your app communicates with already-paired Bluetooth
           devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

我仍然感到困惑,我是否错过了Android 12的音频清晰流动,我能够在OS 11和以下设备之间切换。
这是我使用的代码:

fun startScoAudio(): Boolean {
        ThreadUtils.checkIsOnMainThread()

        if (scoConnectionAttempts >= MAX_SCO_CONNECTION_ATTEMPTS) {
            return false
        }

        if (bluetoothState != BluetoothState.HEADSET_AVAILABLE) {
            return false
        }

        bluetoothState = BluetoothState.SCO_CONNECTING

        audioManager?.startBluetoothSco()
    audioManager?.isBluetoothScoOn = true
    scoConnectionAttempts++
    startTimer()
   return true
}
e0bqpujr

e0bqpujr1#

在开源项目中搜索了一些代码之后。
这段代码成功了。

Handler(Looper.getMainLooper()).postDelayed(
            {
                /* Sometimes bluetooth sco not starting in some devices immediately
                 so giving a delay and running it main thread */
                audioManager?.startBluetoothSco()
                audioManager?.isBluetoothScoOn = true
                scoConnectionAttempts++
                startTimer()
            }, 500
        )

我所需要的只是在主线程中运行StartBluetooth SCO。它在Android OS 12设备中开始正常工作。

相关问题