Toggling A2DP Device (Android)

后端 未结 2 1226
别那么骄傲
别那么骄傲 2021-02-13 18:15

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\

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 18:33

    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.

提交回复
热议问题