2017-02-11 216 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提供了一些事件吗?我检查了但找不到我必须使用哪种方法来捕获按钮单击事件。 –