无法连接到android中的蓝牙耳机

时间:2012-03-02 11:04:02

标签: android bluetooth headset

在错误的道路上浸泡了近两个月后,我发现了我的错误。 现在我正在调整一个我无法找到答案的新问题: 尝试连接耳机时使用此功能:

   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函数..

任何帮助将不胜感激......真的需要一个 晒

更多信息: 在Android重新启动蓝牙nneded在代码中启用了解决方案的内容是什么? 这是功能代码:     日志( “PM.CheckForHeadSet”, “在”);

    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仍为null,getProfileProxy返回false

这是logcat:

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

1 个答案:

答案 0 :(得分:0)

getProxyProfile在三种情况下返回false(这是根据android-15源代码,因此在android-11中可能略有不同):

  • 传入的Context为null
  • 传入的BluetoothProfile.ServiceListener为null
  • 传入的int不是BluetoothProfile.HEADSET,BluetoothProfile.A2DP,BluetoothProfile.INPUT_DEVICE,BluetoothProfile.PAN或BluetoothProfile.HEALTH
  • 之一

我建议您在调用getProxyProfile时设置断点,以确定哪些情况导致false返回值。

编辑:更多信息

必须支持并启用蓝牙才能调用接口方法。如果不支持蓝牙,getDefaultAdapter()将返回null。如果您有适配器,则可以通过调用适配器上的isEnabled()来检查是否启用了蓝牙。这非常简单,所以你可能已经做到了。

onServiceConnectedandroid.content.ServiceConnection类中的mServiceConnection成员(BluetoothHeadset)调用,而bindService类依次通过{{1在创建BluetoothHeadset对象时调用。如果此服务的绑定失败,则logcat消息日志中应该出现如下错误:

       "Could not bind to Bluetooth Headset Service"
带有“BluetoothHeadset”标签的

。如果你没有看到这个错误,那么事情可能就像他们应该在幕后工作一样。

最后一点是,Android一次只支持一个连接的耳机。运行此代码时,请确保没有连接耳机。在类似的说明中,许多设备一次只支持一个蓝牙连接。如果这适用于您的设备,则在运行此代码时,您需要确保它尚未连接。

这几乎耗尽了我所知道的并且很容易确定蓝牙连接。希望它可以帮助您解决问题。