Reputation: 1951
I have strange problem in Android app audio call only in Android 12 devices.
When I'm making call with Bluetooth connected in device, audio is flowing and I'm able to hear audio in Bluetooth device. But when I'm trying to switch between connected Bluetooth device and loudspeaker, it works perfectly in Android 11 and below devices.
But for Android 12 it's not working correctly. There is no audio when I'm trying to switch to Bluetooth device from loud speaker. I'm able to hear sound in loudspeaker.
After checking Android docs I even added code to ask permission to use Bluetooth connect. But still trying switching in OS 12 devices audio there is still no audio. I do understand something is missing for OS 12.
<!--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" />
I'm still confused whether what i missed for Android 12 as audio is clearly flowing and I'm able to switch between OS 11 and below devices.
This is the code I have used:
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
}
Upvotes: 2
Views: 2107
Reputation: 1
It is said Android system (12 or above) terminate SCO connection if it detected that SCO is not used. I guss an App shall do some actions after it has request startBluetoothSco().
Upvotes: 0
Reputation: 1951
After searching some codes in open source projects.
this piece of code did the trick.
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
)
All i needed was needed to run startbluetooth sco in main thread. It started working correctly in android os 12 devices.
Upvotes: 2