2

I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.

Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.

I have also figured out how to see if the device has any paired devices via the following.

BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() != 0)
    {
        // the device has paired devices
        for (BluetoothDevice device : pairedDevices)
        {
            Log.i(TAG, "device name: " + device.getName());
            Log.i(TAG, "device address: " + device.getAddress());
        }
    }
    else
    {
        // no paired devices
        Log.i(TAG, "no paired devices");
    }
}

I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as BluetoothProfile and BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8

I am willing to try anything at this point.

Thanks in advance

-H

Community
  • 1
  • 1
Havoc
  • 213
  • 4
  • 11
  • I have not gone native yet. I really want a solution to this issue. I have learned a lot and come a long way with my application in the past month. Thanks to all on stackoverflow – Havoc Sep 28 '11 at 21:08

2 Answers2

4

Following code works for me. First connect your BT device with android manually.

AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
    //bt device is connected with phone
}
else{
   // no device is connected with phone
}
shantanu
  • 2,408
  • 2
  • 26
  • 56
  • @fustalol Worked for me in Android 5.1 (CyanogenMod 12.1). Note that this only works for bluetooth speakers/headsets, however. (and, it doesn't tell you the device name or the like) – Venryx Feb 26 '17 at 14:33
1

Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Not the answer I wanted to hear :( – Havoc Aug 17 '11 at 19:03
  • Is there anything I can do via reflection? I know that [BluetoothHeadset](http://www.androidjavadoc.com/1.1_r1_src/android/bluetooth/BluetoothHeadset.html) exists and I could try [reflection](http://java.sun.com/developer/technicalArticles/ALT/Reflection/) or try [this](http://stackoverflow.com/questions/6020421/problem-invoking-a-private-unpublished-method-in-android-api) – Havoc Aug 17 '11 at 19:12
  • Well, you can dig through the BluetoothService class at http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/server/BluetoothService.java;h=643581e40695ee82a0392745fb3b260361698f23;hb=HEAD and see if there's something that isn't restricted to package availability that you can reach into at API level 8, but I somehow doubt it. I had to do some medical device work with a device that I needed to detect and there is unfortunately no clean way to scan for the presence of a device of a specific class without going native (I gave up :( ). – Femi Aug 17 '11 at 19:18
  • I'll go native, but don't have a clue where to start. Up until 4 weeks ago I was a 15 year C veteran. Any ideas where to start would help. – Havoc Aug 17 '11 at 20:16