在错误的道路上浸泡了将近两个月之后,我发现了我的错误所在。现在我正在解决一个新问题,我找不到答案:尝试连接到头戴式耳机时使用此功能:
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
final 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;
}
}
};
我无法初始化 mBluetoothHeadset 对象,由于某种原因调试器不会单步进入 onServiceConnected 函数。
任何帮助将不胜感激...真的需要一个晒
更多信息:在安卓重启蓝牙后,在代码中解决了什么:这是函数代码: 日志("下午.CheckForHeadSet","in"(;
if (mBluetoothAdapter == null) {
Log("PM.CheckForHeadSet","BlueTooth adapter not found");
return "Error Bluetooth adapter";
}
switch (mBluetoothAdapter.getState()){
case BluetoothAdapter.STATE_OFF:
Log("PM.CheckForHeadSet.getState"," STATE_OFF");
mBluetoothAdapter.enable();
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log("PM.CheckForHeadSet.getState","STATE_TURNING_ON");
break;
case BluetoothAdapter.STATE_ON:
Log("PM.CheckForHeadSet.getState","STATE_ON");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log("PM.CheckForHeadSet.getState","STATE_TURNING_OFF");
break;
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices, add each one to the ArrayAdapter
if (pairedDevices.size() == 1) {
for (BluetoothDevice device : pairedDevices)
if(device.getBondState() == BluetoothDevice.BOND_BONDED){
Log("PM.CheckForHeadSet Connected to:",device.getName());
}
}
Log("PM.CheckForHeadSet ServiceListener:","In");
final 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; }
}};
if(mBluetoothHeadset == null)
Log("PM.CheckForHeadSet","mBluetoothHeadset = null");
else
Log("PM.CheckForHeadSet","mBluetoothHeadset = " + mBluetoothHeadset.toString());
if(context == null)
Log("PM.CheckForHeadSet","context = null");
else
Log("PM.CheckForHeadSet","context = " + context.toString());
if(mProfileListener == null)
Log("PM.CheckForHeadSet","mProfileListener = null");
else
Log("PM.CheckForHeadSet","mProfileListener = " + mProfileListener.toString());
if(mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET) == true)
Log("PM.CheckForHeadSet.getProfileProxy","true");
else
Log("PM.CheckForHeadSet.getProfileProxy","false");
Log("PM.CheckForHeadSet","Out");
return "Set Headset";
如果我将 GetProfileProxy 放在新的 ProfileListener 上方(如 docomantaion 示例|(,则 mProfileListener var 仍然为空,并且 getProfileProxy 返回 false
这是日志猫:
03-12 10:09:49.906: D/SpySitter(4205): PM.CheckForHeadSet-In
03-12 10:09:50.968: D/dalvikvm(4205): threadid=1: still suspended after undo (sc=1 dc=1)
03-12 10:09:59.453: D/SpySitter(4205): PM.CheckForHeadSet.getState-STATE_ON
03-12 10:10:02.640: D/SpySitter(4205): PM.CheckForHeadSet Connected to:-Motorola H790
03-12 10:10:04.226: D/SpySitter(4205): PM.CheckForHeadSet ServiceListener:-In
03-12 10:10:13.945: D/SpySitter(4205): PM.CheckForHeadSet-mBluetoothHeadset = null
03-12 10:10:17.984: D/SpySitter(4205): PM.CheckForHeadSet-context = android.app.Application@408472a0
03-12 10:10:21.820: D/SpySitter(4205): PM.CheckForHeadSet-mProfileListener = com.example.HelloForm.Tools$1@40894d00
03-12 10:10:28.796: D/SpySitter(4205): PM.CheckForHeadSet.getProfileProxy-true
03-12 10:10:31.226: D/SpySitter(4205): PM.CheckForHeadSet-Out
getProxyProfile 在三种情况下返回 false(这是根据 android-15 来源,因此在 android-11 中可能略有不同(:
- 传入的上下文为空
- 传入的 BluetoothProfile.ServiceListener 为空
- 传入的 int 不是 BluetoothProfile.HEADSET、BluetoothProfile.A2DP、BluetoothProfile.INPUT_DEVICE、BluetoothProfile.PAN 或 BluetoothProfile.HEALTH 之一
我建议在调用 getProxyProfile 时设置一个断点,以确定哪些情况导致false
返回值。
编辑:更多信息
必须支持并启用蓝牙才能调用接口方法。如果不支持蓝牙,getDefaultAdapter()
将返回null
。如果您有适配器,可以通过在适配器上调用isEnabled()
来检查是否启用了蓝牙。这很简单,所以你可能已经这样做了。
onServiceConnected
由BluetoothHeadset
类中的android.content.ServiceConnection
成员(mServiceConnection
(调用,而类又由Android操作系统在创建BluetoothHeadset
对象时通过bindService
调用调用。如果此服务的绑定失败,则 logcat 消息日志中应该会出现如下错误:
"Could not bind to Bluetooth Headset Service"
带有"蓝牙耳机"标签。如果您没有看到此错误,则可能是事情在引擎盖下正常工作。
最后需要注意的是,Android一次仅支持一个连接的耳机。确保在运行此代码时未连接头戴式耳机。同样,许多设备一次仅支持一个蓝牙连接。如果这适用于你的设备,则需要确保在运行此代码时它尚未连接。
这几乎耗尽了我所知道的,并且可以轻松确定蓝牙连接。希望它能帮助您解决问题。