2017-02-11 204 views
0

我一直試圖攔截我的藍牙耳機的應答按鈕徒勞。我可以在耳機中接收來電鈴聲,並且一旦我從我的應用程序接聽電話,我就可以在耳機上收聽。但是,我無法弄清楚如何在我的應用程序中攔截答案按鈕,以便我可以使用耳機接聽電話。Android:攔截藍牙耳機的應答按鈕

我的手機與Jelly Bean操作系統。我已經嘗試使用註冊媒體按鈕接收器,並與IntentFilter ACTION_AUDIO_STATE_CHANGED,並再次與ACTION_CALL_BUTTON一個普通的接收器,但似乎沒有任何工作。請讓我知道我如何才能做到這一點。非常感謝你的幫助。

回答

0

爲了讓藍牙設備工作首先你需要在你的應用程序的清單文件添加權限:

<manifest ... > 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    ... 
</manifest> 

,我敢肯定,你一定已經完成。

我們控制藍牙耳機,您可以使用藍牙耳機服務:

的示例代碼片段:

BluetoothHeadset mBluetoothHeadset; 

// Get the default adapter 
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() { 
    public void onServiceConnected(int profile, BluetoothProfile proxy) { 
     if (profile == BluetoothProfile.HEADSET) { 
      mBluetoothHeadset = (BluetoothHeadset) proxy; 
     } 
    } 
    public void onServiceDisconnected(int profile) { 
     if (profile == BluetoothProfile.HEADSET) { 
      mBluetoothHeadset = null; 
     } 
    } 
}; 

// Establish connection to the proxy. 
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET); 

// ... call functions on mBluetoothHeadset 

// Close proxy connection after use. 
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset); 
+0

非常感謝您的回覆。是的,我在清單中做了聲明,但不清楚如何使用BluetoothHeadset和適配器。我很抱歉,但我無法清楚地瞭解您的代碼,在哪裏添加事件捕獲按鈕單擊耳機? BluetoothHeadset提供了一些事件嗎?我檢查了但找不到我必須使用哪種方法來捕獲按鈕單擊事件。 –