无法连接到Android中的蓝牙耳机 [英] can not connect to bluetooth headset in android

查看:210
本文介绍了无法连接到Android中的蓝牙耳机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在错误的道路上沉迷了近两个月后,我发现了自己的错误所在. 现在,我正在解决一个新问题,但我找不到以下答案: 尝试连接耳机时使用此功能:

After steeping in the wrong path for almost two months i found out what my mistake was. Now i am pacing a new problem which I cannot find the answer to: Using this function while trying to connect to the 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函数.

I cannot initialize the mBluetoothHeadset object ,for some reason the debugger wont step into the onServiceConnected function..

任何帮助将不胜感激……真的需要一个 shai

Any help will be appreciated...realy needs one shai

更多信息: 指出在Android重新启动后蓝牙需要启用的问题在于在代码中解决了问题: 这是功能代码: Log("PM.CheckForHeadSet","In");

More Info: Inded what haped was that after a android restart Bluetooth nneded to be enabled'solved in code: This is the function code: Log("PM.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变量仍为null,并且getProfileProxy返回false

If i place the GetProfileProxy above the new ProfileListener (as in the docomantaion example|) the mProfileListener var is still null and getProfileProxy return 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

推荐答案

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

getProxyProfile returns false in three cases (this is according to the android-15 source, so it might be slightly different in android-11):

  • 传入的上下文为空
  • 传入的BluetoothProfile.ServiceListener为空
  • 传入的int不是BluetoothProfile.HEADSET,BluetoothProfile.A2DP,BluetoothProfile.INPUT_DEVICE,BluetoothProfile.PAN或BluetoothProfile.HEALTH之一

我建议您在对getProxyProfile的调用上设置一个断点,以查明是哪种情况导致了false返回值.

I'd suggest setting a breakpoint on your call to getProxyProfile to figure out which of these cases is causing the false return value.

修改:更多信息

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

Bluetooth has to be supported and enabled for the interface methods to be called. If Bluetooth is not supported, getDefaultAdapter() will return null. If you have an adapter, you can check whether Bluetooth is enabled by calling isEnabled() on the adapter. That's pretty straightforward, so you may have already done that.

onServiceConnectedBluetoothHeadset类中的android.content.ServiceConnection成员(mServiceConnection)调用,当BluetoothHeadset对象为创建.如果此服务的绑定失败,则logcat消息日志中应该出现错误,例如:

onServiceConnected is called by the android.content.ServiceConnection member (mServiceConnection) in the BluetoothHeadset class, which is in turn called by the Android OS by virtue of a bindService call when the BluetoothHeadset object is created. If the binding of this service fails, there should be an error in the logcat message log something like:

       "Could not bind to Bluetooth Headset Service"

带有"BluetoothHeadset"标签的.如果您没有看到此错误,则说明事情可能在后台运行.

with a tag of "BluetoothHeadset". If you don't see this error, things are probably working as they should under the hood.

最后一点是,Android一次仅支持一个连接的耳机.运行此代码时,请确保未连接任何耳机.同样,许多设备一次仅支持一个蓝牙连接.如果适用于您的设备,则在运行此代码时,请确保它尚未连接.

A final note is that Android only supports one connected headset at a time. Make sure that no headset is connected when you are running this code. On a similar note, many devices only support one bluetooth connection at a time. If that applies to your device, you'll want to make sure it is not already connected when you run this code.

这几乎耗尽了我所知道的知识,并且可以轻松确定有关蓝牙连接的信息.希望它可以帮助您解决问题.

That pretty much exhausts what I know and can easily determine about Bluetooth connections. Hope it helps you solve your problem.

这篇关于无法连接到Android中的蓝牙耳机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆