检测Android蓝牙应答/挂断按钮事件

beh*_*lit 6 android bluetooth button

我需要检测何时按下蓝牙设备上的"电话"按钮,大多数都会有一个按钮用于接听/挂断.

使用audioManager.registerMediaButtonEventReceiver()意图过滤器MEDIA_BUTTON,我能够检测到电话按钮的所有按钮除外(即:跳过下一个,跳过上一个,播放/暂停).

使用CALL或CALL_BUTTON过滤器不起作用(没有收到任何事件).

按钮的默认行为是断开音频并切换回耳机.在Skype应用程序中也会出现相同的行为,但是,当进行正常的GSM呼叫时,内置的电话应用程序正确处理按钮,并且可以回答和挂断呼叫.

我试图找到Phone应用程序如何处理这个但无法找到代码.

有谁知道如何正确检测蓝牙电话按钮事件?

job*_*ert 3

很难弄清楚如何实现这一点,只找到了一个挂断的黑客解决方案。我将此代码用于 SIP 呼叫应用程序。我将活动对话的音频路由到蓝牙耳机,将音频模式设置为“通信中” (audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);)。当在通话过程中按下蓝牙耳机的挂断按钮时,会调用意图 STATE_AUDIO_DISCONNECTED。我使用该意图操作来挂断当前通话。更改音频模式后,我使用以下调用(部分从其他来源使用):

public class BluetoothIntentListener {
    private static final String TAG = "BluetoothIntent";
    private static BluetoothIntentListener bluetoothIntentListener;
    protected BluetoothAdapter mBluetoothAdapter;
    protected BluetoothHeadset mBluetoothHeadset;
    protected AudioManager mAudioManager;
    private Context context;

    private BluetoothIntentListener(Context context) {
        this.context = context;
    }

    public static BluetoothIntentListener getInstance(Context context) {
        if (bluetoothIntentListener == null) {
            bluetoothIntentListener = new BluetoothIntentListener(context);
        }
        return bluetoothIntentListener;
    }

    public void init() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter != null) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            if (mAudioManager.isBluetoothScoAvailableOffCall()) {
                mBluetoothAdapter.getProfileProxy(context, mHeadsetProfileListener, BluetoothProfile.HEADSET);
            }
        }
    }

    public void destroy() {

 mHeadsetProfileListener.onServiceDisconnected(BluetoothProfile.HEADSET);
    }

    protected BluetoothProfile.ServiceListener mHeadsetProfileListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceDisconnected(int profile) {
            try {
                context.unregisterReceiver(mHeadsetBroadcastReceiver);
                mBluetoothHeadset = null;
            } catch (IllegalArgumentException il) {
                Log.i(TAG, "Headset broadcast receiver wasn't registered yet.");
            }
        }

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mBluetoothHeadset = (BluetoothHeadset) proxy;
            context.registerReceiver(mHeadsetBroadcastReceiver,
                new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
        IntentFilter f = new IntentFilter(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
        f.setPriority(Integer.MAX_VALUE);
            context.registerReceiver(mHeadsetBroadcastReceiver, f);
        }
    };

    protected BroadcastReceiver mHeadsetBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            int state;

            if (action.equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
                state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
                if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
                    Log.d(TAG, "Connected");
                } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
                   // Hangup of bluetooth headset pressed.
                }
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

我希望这对任何人都有帮助。