1

I want to use the BluetoothA2DPSink service in android, it's a hidden class but I built a modified SDK and ROM and now Android studio can see it. The problem is I can't use it, whenever i try 'BluetoothA2DPSink sink = new BluetoothA2DPSink()' I get this error: "BluetoothA2DPSink() is not public in 'android.bluetooth.BluetoothA2dpSink'. Connot be accesed from outside package". I verified it and it is in fact public: "public final class BluetoothA2dpSink implements BluetoothProfile{..." How can I use its methods? Any help would be much appreciated.

Dhia Mili
  • 43
  • 5

2 Answers2

3

If you pasted your error message correctly, the problem is not with the class, but with the constructor. Note the parentheses in "BluetoothA2DPSink() is not public in 'android.bluetooth.BluetoothA2dpSink'. Connot be accesed from outside package" — that is a reference to a constructor, not a class. Make sure the zero-argument constructor is public.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Try the below code. It is done using reflection. You can not simply create an object by calling the constructor for BluetoothA2DPSink. You also need to use another class BluetoothProfile.java.

Object mBluetoothA2DPSink;

/**
 * This function will connect to A2DPsink profile of the server device to manage audio profile connection
 */
public void getBluetoothA2DPsink() {
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    try {
        final int A2DPprofile = BluetoothProfile.class.getField("A2DP_SINK").getInt(null);
        BluetoothProfile.ServiceListener mProfileListener1 = new BluetoothProfile.ServiceListener() {
            public void onServiceConnected(int profile, BluetoothProfile proxy) {

                if (profile == A2DPprofile) {
                    mBluetoothA2DPSink = proxy;

                }
            }
            public void onServiceDisconnected(int profile) {
                if (profile == A2DPprofile) {
                    mBluetoothA2DPSink = null;
                    try {
                        mContext.unregisterReceiver(mA2DPReciever);
                    } catch (IllegalArgumentException e) {
                        Log.e(TAG, e.getMessage());
                    }
                }
            }
        };
        // Establish connection to the proxy.
        mBluetoothAdapter.getProfileProxy(mContext, mProfileListener1, A2DPprofile);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Log.e(TAG,  e.getMessage());
    }
}
taug
  • 378
  • 1
  • 8