简体   繁体   English

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

[英]can not connect to bluetooth headset in 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;
             }
       }
    };

I cannot initialize the mBluetoothHeadset object ,for some reason the debugger wont step into the onServiceConnected function..我无法初始化 mBluetoothHeadset 对象,出于某种原因调试器不会进入 onServiceConnected 函数..

Any help will be appreciated...realy needs one shai任何帮助将不胜感激......真的需要一个人

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");更多信息:发现在 android 重启后蓝牙需要启用'解决的问题是代码:这是函数代码: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";

If i place the GetProfileProxy above the new ProfileListener (as in the docomantaion example|) the mProfileListener var is still null and getProfileProxy return false如果我将 GetProfileProxy 放在新的 ProfileListener 之上(如在 docomantaion 示例中|),则 mProfileListener var 仍然为 null 并且 getProfileProxy 返回 false

And this is the logcat:这是 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 returns false in three cases (this is according to the android-15 source, so it might be slightly different in android-11): getProxyProfile 在三种情况下返回 false(这是根据 android-15 来源,因此在 android-11 中可能略有不同):

  • the Context passed in is null传入的 Context 为 null
  • the BluetoothProfile.ServiceListener passed in is null传入的BluetoothProfile.ServiceListener为null
  • the int passed in is not one of BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.INPUT_DEVICE, BluetoothProfile.PAN, or BluetoothProfile.HEALTH传入的 int 不是 BluetoothProfile.HEADSET、BluetoothProfile.A2DP、BluetoothProfile.INPUT_DEVICE、BluetoothProfile.PAN 或 BluetoothProfile.HEALTH 之一

I'd suggest setting a breakpoint on your call to getProxyProfile to figure out which of these cases is causing the false return value.我建议您在调用 getProxyProfile 时设置一个断点,以确定这些情况中的哪一种导致了false返回值。

edit: Further info编辑:更多信息

Bluetooth has to be supported and enabled for the interface methods to be called.必须支持和启用蓝牙才能调用接口方法。 If Bluetooth is not supported, getDefaultAdapter() will return null .如果不支持蓝牙, getDefaultAdapter()将返回null If you have an adapter, you can check whether Bluetooth is enabled by calling isEnabled() on the adapter.如果您有适配器,您可以通过调用适配器上的isEnabled()来检查是否启用了蓝牙。 That's pretty straightforward, so you may have already done that.这非常简单,所以您可能已经这样做了。

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. onServiceConnectedBluetoothHeadset类中的android.content.ServiceConnection成员 ( mServiceConnection ) 调用,该类又由 Android 操作系统在创建BluetoothHeadset对象时通过bindService调用进行调用。 If the binding of this service fails, there should be an error in the logcat message log something like:如果此服务的绑定失败,那么 logcat 消息日志中应该会出现类似以下内容的错误:

 "Could not bind to Bluetooth Headset Service"

with a tag of "BluetoothHeadset".带有“蓝牙耳机”标签。 If you don't see this error, things are probably working as they should under the hood.如果您没有看到此错误,那么事情可能在内部正常运行。

A final note is that Android only supports one connected headset at a time.最后要注意的是,Android 一次仅支持一个连接的耳机。 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.希望它可以帮助您解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM