Issue
I have strange problem in android app audio call only in android os 12 devices.
When im making call with bluetooth connected in device,audio is flowing and im able to hear audio in bluettooth device.But When im trying to switch between connected bluetooth device and loud speaker,it works perfectly in android OS 11 and below devices.
But for android OS 12 its not working correctly.there is no audio when im trying to switch to bluettooth device from loud speaker.im able to hear sound in loud speaker.
After checking android docs i even added code to ask permission to use bluettooth connect.
But still trying switching in os 12 devices audio there is still no audio.I do understand something is missing for os 12.
Can u guys please tell
<!--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" />
Im still confused whether what i missed for android os 12 as audio is clearly flowing and im able to switch between in os 11 and below devices.
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
}
this is the code i have used.
Solution
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.
Answered By - Jeeva
Answer Checked By - Robin (JavaFixing Admin)