Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting a Call via Bluetooth Headset

i am working on a VoIP-Android-App. I would like to accept and decline Calls via a connnected Bluetooth Headset in an Activity.

What I have tried so far:

  • Using a Media Session to receive Media Button clicks.

    Problem: If we start BluetoothSCO we do not receive any Media Button clicks. If we do not start BluetoothSCO we do receive Media Button clicks but we cannot differentiate long and short button clicks because downtime is always 0, the keycode is always KEYCODE_MEDIA_PLAY and the ACTION_DOWN is immediately followed by ACTION_UP. Those problems only occur if we are connected via Bluetooth. If we are connnected over a cable Headset we do get the appropriate keycodes (KEYCODE_HEADSETHOOK) and the downtime is not 0.

  • Using a BroadcastReceiver to listen for Bluetooth SCO connection changes.

    private val scoReceiver = object : BroadcastReceiver() {
        fun onReceive(context: Context, intent: Intent) {
            val state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1)
            val previousState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_PREVIOUS_STATE, -1)
            if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED && previousState == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
                Log.e(TAG, "SCO Disconnected")
                hangupCall()
            }
        }
    }
    
    protected fun onStart() {
        super.onStart()
        val intentFilter = IntentFilter()
        intentFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)
        registerReceiver(scoReceiver, intentFilter)
    }
    

    With this approach i can detect when the user wants to hang up the call, for example a long press on the bluetooth headset because this triggers the SCO to disconnect.

    Problem: We can not detect if the user wants to accept an incoming call.

  • Using dispatchKeyEvent, onKeyDown and onKeyUp.

    Problem: They never get called at all.

Does anyone has any advice or a best practice how to correctly handle bluetooth headsets? Any help is very appreciated. Thanks in advance!

like image 793
Dariusch Avatar asked Jul 18 '18 10:07

Dariusch


People also ask

How do you pick up a call from earphones?

When a call comes in, there will be a button on the earbuds that you press to answer calls. That will connect the call and allow you to carry on a conversation without having to physically hold your phone. You can generally end the call in the same way, or by allowing the other person to hang up first.

Can phone calls be heard through Bluetooth?

Giving hackers the ability to insert themselves between two Bluetooth-connected devices, KNOB (Key Negotiation Of Bluetooth) can intercept practically everything, including file transfer data and phone calls. It can also inject its own data, so it could be used to infect devices with malware.


2 Answers

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.

like image 93
RocketRandom Avatar answered Oct 19 '22 16:10

RocketRandom


During normal and virtual voice call (including ringing) all events of Bluetooth headset unit buttons are processed by Bluetooth Headset Service internaly and not broadcasted as button events. Bluetooth Headset Service redirects these events into Telecom framework (answer/hangupCall).

like image 2
hank15 Avatar answered Oct 19 '22 17:10

hank15