2015-08-28 158 views
4

我需要檢測藍牙設備上按下「電話」按鈕時,大多數人都會有一個按鈕來應答/掛斷。檢測Android藍牙應答/掛斷按鈕事件

使用audioManager.registerMediaButtonEventReceiver()與意向過濾器MEDIA_BUTTON,我能夠檢測除手機按鈕(即:跳過下一個,跳過上一個,播放/暫停)以外的所有按鈕。

使用CALL或CALL_BUTTON過濾器不起作用(未收到任何事件)。

該按鈕的默認行爲是斷開音頻並切換回聽筒。在Skype應用程序中發生同樣的行爲,但是,在進行正常的GSM呼叫時,內置的電話應用程序會正確處理該按鈕,並可以應答並掛斷呼叫。

我在試圖找到電話應用程序如何處理這個問題,但一直未能找到代碼。

有誰知道如何正確檢測藍牙電話按鈕事件?

+0

你有沒有想過這個? – jobbert

+0

不,看起來不可能 – behelit

+1

我實際上找到了一種方法來做到這一點。這感覺有點不好意思,如果你有興趣,我可以發佈它?當然是 – jobbert

回答

2

很難找出如何做到這一點,只發現一個hacky掛斷解決方案。我將此代碼用於SIP呼叫應用程序。我將活動對話的音頻路由到藍牙耳機,將音頻模式設置爲通信(audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);)。當在對話期間按下藍牙耳機的掛斷按鈕時,意向STATE_AUDIO_DISCONNECTED get被調用。我使用該意圖操作來掛斷當前通話。我在改變音頻模式(部分使用其他來源)後使用以下呼叫:

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. 
       } 
      } 
     } 
    }; 
} 

我希望這可以幫助任何人。

+1

不像我想的那樣哈克:)會給它一個鏡頭 – behelit

+0

它是否爲你做了詭計? – jobbert