为了让蓝牙设备工作首先你需要在你的应用程序的清单文件添加权限:
<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提供了一些事件吗?我检查了但找不到我必须使用哪种方法来捕获按钮单击事件。 –