Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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:

publicclassBluetoothA2dpConnection {

privateIBluetoothA2dpmService=null;

publicBluetoothA2dpConnection() {

    try {
        Class<?>  classServiceManager = Class.forName("android.os.ServiceManager");
        MethodmethodGetService= classServiceManager.getMethod("getService", String.class);
        IBinderbinder= (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();
    }
}

publicbooleanconnect(BluetoothDevice device) {
    if (mService == null || device == null) {
        returnfalse;
    }
    try {
        mService.connectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        returnfalse;
    }
    returntrue;
}

publicbooleandisconnect(BluetoothDevice device) {
    if (mService == null || device == null) {
        returnfalse;
    }
    try {
        mService.disconnectSink(device);
    } catch (RemoteException e) {
        e.printStackTrace();
        returnfalse;
    }
    returntrue;
}

}

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.

Solution 2:

First you need to set the program to activate the Bluetooth on the phone and select the device that it should pair with via

bluetoothAdapter.disable() / enable() 

(im not sure about the pairing but this must be done via some config activity)

Then you should set A2DP to connect to the car's stereo

follow this link to try and find the code for it, if i have time i will try and find it for you but its a start =]

hidden & internal api's

Post a Comment for "Toggling A2dp Device (android)"