How write a program to connect to a a2dp bluetooth device using pre 3.0 Android?

12,177

Some of the Bluetooth classes (profiles like BluetoothA2dp) are hidden in Gingerbread. It means that their declaration is annotated by @hide, and they are not included into the SDK (Android.jar). This is done intentionally, since these APIs are likely to be changed in newer Android versions. Generally it is not a good idea to use hidden APIs, since your App can stop working on newer Android versions, but if you are sure you want to, follow http://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-1-introduction/

Once you get access to them do something like (just a hint):

BluetoothA2dp mBluetoothA2dp = new BluetoothA2dp(context);
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().
// Loop through paired devices
for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) {
    if (device.getName().contains("whatyouwant")) {
        mBluetoothA2dp.addSink(device);
    }
}
Share:
12,177
fatfreddyscat
Author by

fatfreddyscat

Updated on June 04, 2022

Comments

  • fatfreddyscat
    fatfreddyscat about 2 years

    My application needs to connect to a a2dp device over bluetooth and I want to "be able to query for the visible bluetooth devices, then, select a a2dp device and have it 'connect via a2dp' so that audio starts playing through the connected device" but my phone is running gingerbread (2.3.3).

    I went through the basic bluetooth tutorial at http://developer.android.com/guide/topics/wireless/bluetooth.html and got to the part I need to connect to the bluetooth device and then I read the bottom of the page:

    "Starting in Android 3.0, the Bluetooth API includes support for working with Bluetooth profiles." -> does this mean that I am S.O.L.? Is there any way to programmatically (why does stackoverflow mark programmatically as being misspelled?!) connect to a a2dp device using a pre-3.0 version of Android? Is my only option to direct the user to go into their settings/pull up the settings programmatically?? Because I'm able to do it through the settings, I guess I just assumed it would be possible via my application as well.

    Help?

  • Tom
    Tom over 12 years
    Can you confirm that this works, and how would you instantiate an object of BluetoothA2dp if it is hidden? Would you have to localize the class and have the Dalvik VM override it with the system class, or use reflection?
  • fatfreddyscat
    fatfreddyscat over 12 years
    I will try this out and get back to you (actually need a solution for >2.1 [eclair] but, if it's gingerbread, that would be better than nothing); I will let you know my findings in my next comment... thanks for this!!! (your google searching is better than mine ;-) )
  • Yuriy Kulikov
    Yuriy Kulikov over 12 years
    Once you have added Android.jar from the link, you will be able to instantiate like it as a normal class. You might have to reload the project, so that Eclipse indexer sees the change.
  • fatfreddyscat
    fatfreddyscat over 12 years
    okay so, I got the hidden api's enabled in my android.jar.. I'm able to create a BluetoothA2dp object. However, seems it doesn't work :-( When I try to set the sink via 'connectSink()', nothing happens. Also, when I look at the documentation, it says that I need to get a reference to the BluetoothA2dp object using 'getProfileProxy(Context, BluetoothProfile.ServiceListener, int)' but I can't do that since that method doesn't seem to exist nor does 'BluetoothProfile.ServiceListener' as I'm guessing that the documentation refers to a later sdk (e.g. honeycomb). If anyone has any further help
  • fatfreddyscat
    fatfreddyscat over 12 years
    okay, so, I verified that it is actually possible to use the hidden API's to connect to a a2dp device via bluetooth (I guess it wasn't working before because the device had some problems). Thanks for the info!! Marking this as the proper answer.
  • fatfreddyscat
    fatfreddyscat over 12 years
    So, although I was able to use the hidden API's to connect to a a2dp device over bluetooth, seems that this method doesn't work anymore for Android API's later than Honeycomb as they have made the BluetoothA2dp constructor package scope only allowing you to get a reference upon connection/disconnection of a A2DP device (or headset device)... So, now my question is whether it is possible to programmatically connect to a bluetooth device using a Honeycomb-level Android or later...?? I suppose I will post a separate question for this but, if anyone reads that, help would be nice.
  • Yuriy Kulikov
    Yuriy Kulikov about 12 years
    Hi!developer.android.com/reference/android/bluetooth/…, android.bluetooth.BluetoothProfile.ServiceListener, int)
  • Yuriy Kulikov
    Yuriy Kulikov about 12 years
    oops, didn't know that editing time is limited. Anyway, it seems that public APIs do not allow to connect to A2DP. You can still use hidden APIs to connect or better start Bluetooth settings activity to connect and than verify in your application that connection was successful.
  • fatfreddyscat
    fatfreddyscat over 11 years
    it actually is possibly on pre-ICS (possibly honeycomb too) devices using the hidden apis by way of reflection. However, it isn't supported directly by the apis and post ICS (possibly post honeycomb), the made the classes private and thus unable to be accessed even through reflection. So, long story short, I never was able to make the connection with the latest Android devices.