Toggling A2DP Device (Android)

此生再无相见时 提交于 2020-01-12 07:10:34

问题


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.


回答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:

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.




回答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



来源:https://stackoverflow.com/questions/13014509/toggling-a2dp-device-android

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