简体   繁体   English

如何使用 a2dp 和 avrcp 蓝牙配置文件连接两个 android 设备?

[英]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.我正在尝试在两个 android 设备之间实现 a2dp 和 avrcp,我找不到任何合适的参考。 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(); 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. BluetoothProfileManager仅适用于 Android Things 设备,不适用于手机或平板电脑。

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.另一个问题是 android 设备用作音乐源,而不是接收器,即它有一个 2dp 源配置文件,您不能在您的应用程序中更改它。 This is only possible by modifying android source code.这只能通过修改android源代码来实现。

So if u want to make one android device to act as bluetooth music receiver u need to make a few changes in AOSP.因此,如果您想将一台 android 设备用作蓝牙音乐接收器,您需要在 AOSP 中进行一些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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