can not connect to bluetooth headset in android

≡放荡痞女 提交于 2020-04-21 08:14:18

问题


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

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");

    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

And this is the 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:


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

  • the Context passed in is null
  • the BluetoothProfile.ServiceListener passed in is null
  • the int passed in is not one of BluetoothProfile.HEADSET, BluetoothProfile.A2DP, BluetoothProfile.INPUT_DEVICE, BluetoothProfile.PAN, or 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.

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

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"

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



来源:https://stackoverflow.com/questions/9532233/can-not-connect-to-bluetooth-headset-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!