如何将应用程序连接到蓝牙耳机
How to connect an app to a Bluetooth headset
我正在尝试将我的应用程序连接到蓝牙耳机,但出现 mProfileListener
的错误。我不确定在哪里以及如何声明 mProfileListener
。
这是我的代码:
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
Log.d("TAGP","BLuetooth Headset: "+mBluetoothHeadset);
Log.d("TAGP ","Proxy: "+proxy);
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};
// ... call functions on mBluetoothHeadset
// Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
}
您可以在 class 范围内声明 mProfileListener 实例,如下所示:
private BluetoothProfile.ServiceListener mProfileListener;
然后在您的 "connected" 蓝牙堆栈方法中,您可以像您的代码一样初始化此 mProfileListener 实例。
我正在尝试将我的应用程序连接到蓝牙耳机,但出现 mProfileListener
的错误。我不确定在哪里以及如何声明 mProfileListener
。
这是我的代码:
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
Log.d("TAGP","BLuetooth Headset: "+mBluetoothHeadset);
Log.d("TAGP ","Proxy: "+proxy);
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
}
}
};
// ... call functions on mBluetoothHeadset
// Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(mBluetoothHeadset);
}
您可以在 class 范围内声明 mProfileListener 实例,如下所示:
private BluetoothProfile.ServiceListener mProfileListener;
然后在您的 "connected" 蓝牙堆栈方法中,您可以像您的代码一样初始化此 mProfileListener 实例。