Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggling A2DP Device (Android)

I have two paired bluetooth devices (my car's head-unit for phone audio and a separate bluetooth receiver for A2DP). On my phone there's a checkbox for "Use for media audio" that I have to manually toggle for my A2DP output to go to my car's speakers. My goal is to toggle this programmatically.

I tried using both the AudioManager class with the deprecated setBluetoothA2dpOn and the setBluetoothScoOn but neither seemed to have any effect. I was able to get a list of the bluetooth paired devices and get a handle to the connection I want to toggle but I couldn't seem to get it quite right. I also tried getting the default bluetooth adapter and then using getProfileProxy but I feel like I'm barking up the wrong tree there.

Can anyone point me in the right direction? Basically all I want to do is the equivalent of checking that "Use for media audio" box.

like image 686
JamesB41 Avatar asked Oct 22 '12 15:10

JamesB41


People also ask

What is A2DP service android?

A2DP stands for Advanced Audio Distribution Profile. This profile defines how high quality audio (stereo or mono) can be streamed from one device to another over a Bluetooth connection - for example, music streamed from a mobile phone to a wireless headset.

What is an A2DP audio device?

Advanced Audio Distribution Profile (A2DP) is a Bluetooth profile that enables multimedia audio communication, such as music, between a Jabra headset and a supported Bluetooth device. A2DP is used in selected Jabra headsets.


1 Answers

A short time ago I had a similar problem trying connect a bluetooth device to android phone. Although your device profile being different, I think the solution is the same.

First you need create a package in your project named android.bluetooth and put the following IBluetoothA2dp.aidl in there:

package android.bluetooth;

import android.bluetooth.BluetoothDevice;

/**
 * System private API for Bluetooth A2DP service
 *
 * {@hide}
 */
interface IBluetoothA2dp {
    boolean connectSink(in BluetoothDevice device);
    boolean disconnectSink(in BluetoothDevice device);
    boolean suspendSink(in BluetoothDevice device);
    boolean resumeSink(in BluetoothDevice device);
    BluetoothDevice[] getConnectedSinks(); 
    BluetoothDevice[] getNonDisconnectedSinks();
    int getSinkState(in BluetoothDevice device);
    boolean setSinkPriority(in BluetoothDevice device, int priority);
    int getSinkPriority(in BluetoothDevice device);

    boolean connectSinkInternal(in BluetoothDevice device);
    boolean disconnectSinkInternal(in BluetoothDevice device);
}

Then, to access those functionalities, put the following class in your project:

public class BluetoothA2dpConnection {

private IBluetoothA2dp mService = null;

public BluetoothA2dpConnection() {

    try {
        Class<?>  classServiceManager = Class.forName("android.os.ServiceManager");
        Method methodGetService = classServiceManager.getMethod("getService", String.class);
        IBinder binder = (IBinder) methodGetService.invoke(null, "bluetooth_a2dp");
        mService = IBluetoothA2dp.Stub.asInterface(binder); 
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

public boolean connect(BluetoothDevice device) {
    if (mService == null || device == null) {
        return false;
    }
    try {
        mService.connectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

public boolean disconnect(BluetoothDevice device) {
    if (mService == null || device == null) {
        return false;
    }
    try {
        mService.disconnectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

}

Finally, to connect your A2dp device, pick one BluetoothDevice from a list of paired devices and send it as parameter of connect method. Be sure to pick a device with the correct profile, otherwise you will have an exception.

I have tested this solution in a phone with android version 2.3 and it worked fine.

Sorry any English mistake. I hope this can help you.

like image 135
Suelen Avatar answered Sep 20 '22 05:09

Suelen