I am working on a VoIP-Android-App and The app needs to be able to accept/decline call thought Bluetooth headset.
But the problem is that after adding connection to SCO
audioManager.startBluetoothSco()
audioManager.isBluetoothScoOn = true
Once I click to the headset button I can hear a sound that usually comes when I accept call using telephony, so I assume that some android system component catch this signal and doesn't throw it further
What I've tried already:
1) Telephony State listener (It is always IDLE)
val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
phoneStateListener = MyPhoneStateListener()
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
2) MediaSession + silent noise + media button listener Doesn't work for the first click, second+ clicks handled correctly
3) MEDIA_BUTTON receiver doesn't work
I found a similar question on SO but without the answer how to make it work Accepting a Call via Bluetooth Headset
So is there anyway how I can intercept Bluetooth button click from Bluetooth Headset Service?
Accepting a Call via Bluetooth Headset
Adding my answer from there to here too.
These events are handled internally in HeadsetStateMachine (under packages/apps/Bluetooth).
These events are forwarded to IBluetoothHeadsetPhone interface. The single application to which all the events are forwarded is defined at run-time by following binding code in HeadsetStateMachine.java. This is to allow phone manufacturers to forward them to custom phone application instead of default one in cases where default one is not used.
Intent intent = new Intent(IBluetoothHeadsetPhone.class.getName());
intent.setComponent(intent.resolveSystemService(context.getPackageManager(), 0));
if (intent.getComponent() == null || !context.bindService(intent, mConnection, 0)) {
Log.e(TAG, "Could not bind to Bluetooth Headset Phone Service");
}
To make the events get forwarded to your application instead of default phone application you would have to modify aosp code. You would need to intercept the events at one of HeadsetStateMachine , BluetoothHeadsetPhone proxy or the phone application.
Unfortunately what you are looking for is currently not possible without modifying aosp code. Some headsets like Plantronics have custom BT events which are forwarded to all applications - some of the existing VoIP applications support these custom intents to support at-least answering calls for some of the headsets.