简体   繁体   中英

How to connect two android devices using a2dp and avrcp bluetooth profiles?

I am trying to implement a2dp and avrcp between two android devices, i couldn't find any proper references for this. some I have found but are not so much helpful.

if (mBluetoothAdapter == null && !mBluetoothAdapter.isEnabled()) {
        Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
        return;
    }


    mBluetoothAdapter.setName("MyGalaxy");
    mBluetoothAdapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            mA2DPSinkProxy = proxy;
            enableDiscoverable();
        }

        @Override
        public void onServiceDisconnected(int profile) {
        }
    }, A2DP_SINK_PROFILE);

BluetoothProfileManager profileManager = BluetoothProfileManager.getInstance();

    List<Integer> enabledProfiles = profileManager.getEnabledProfiles();
    String enabled = "";
    for (Integer profile : enabledProfiles) {
        enabled += ("" + profile + ", ");
    }
    Log.d(TAG, "Enabled Profiles - " + enabled);

    Log.d(TAG, "Enabling A2dp source mode.");
    List toEnable = Arrays.asList(BluetoothProfile.A2DP);
    List toDisable = Arrays.asList(A2DP_SINK_PROFILE, AVRCP_CONTROLLER_PROFILE);
    profileManager.enableAndDisableProfiles(toEnable, toDisable);

I got this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bluetootha2dpeg1, PID: 30451
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/things/bluetooth/BluetoothProfileManager;
    at com.example.bluetootha2dpeg1.MainActivity.setupBluetoothProfiles(MainActivity.java:157)
    at com.example.bluetootha2dpeg1.MainActivity.initA2DPSink(MainActivity.java:145)
    at com.example.bluetootha2dpeg1.MainActivity.initBluetooth(MainActivity.java:70)
    at com.example.bluetootha2dpeg1.MainActivity.onCreate(MainActivity.java:58)

BluetoothProfileManager is only available for Android Things Devices, and it won't work with phones or tablets.

Another problem is that android device works as music source, not receiver, ie, it has a2dp source profile and u cant change it in your application. This is only possible by modifying android source code.

So if u want to make one android device to act as bluetooth music receiver u need to make a few changes in AOSP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM