爲了讓藍牙設備工作首先你需要在你的應用程序的清單文件添加權限:
<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);
非常感謝您的回覆。是的,我在清單中做了聲明,但不清楚如何使用BluetoothHeadset和適配器。我很抱歉,但我無法清楚地瞭解您的代碼,在哪裏添加事件捕獲按鈕單擊耳機? BluetoothHeadset提供了一些事件嗎?我檢查了但找不到我必須使用哪種方法來捕獲按鈕單擊事件。 –