美文网首页蓝牙和WIFI
蓝牙笔记 | A2DP连接 a2dpConnect

蓝牙笔记 | A2DP连接 a2dpConnect

作者: 力卉编程 | 来源:发表于2019-12-31 17:00 被阅读0次

    A2DP(高级音频传送规格)– 允许传输立体声音频信号。 (相比用于 HSP 和 HFP 的单声道加密,质量要好得多)。

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.getProfileProxy(XApplication.getInstance(), new   profileListener(), BluetoothProfile.A2DP);
    //a2dpConnect(device)
    //获取A2DP的代理:
       public class profileListener implements BluetoothProfile.ServiceListener {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            Log.i(TAG, "connect to a2dp server");
            a2dpProfile = proxy;
        }
        @Override
        public void onServiceDisconnected(int profile) {
            Log.i(TAG, "disconnect to a2dp server");
            a2dpProfile = null;
        }
    }
    //判断A2DP的连接状态:
      public boolean getA2dpState(BluetoothDevice device){
        if(a2dpProfile != null && a2dpProfile.getConnectionState(device) == BluetoothProfile.STATE_CONNECTED){
            return true;
        }
        return false;
    }
    public void a2dpConnect(BluetoothDevice device){
        if(a2dpProfile != null){
            BluetoothA2dp a2dp = (BluetoothA2dp) a2dpProfile;
            Class clazz = a2dp.getClass();
            Method m2;
            try {
                Log.i(TAG,"use reflect to connect a2dp");
                m2 = clazz.getMethod("connect",BluetoothDevice.class);
                m2.invoke(a2dp, device);
            } catch (Exception e) {
                Log.e(TAG,"error:" + e.toString());
            }
        }
    }
    

    完~~
    文 | 力卉编程

    相关文章

      网友评论

        本文标题:蓝牙笔记 | A2DP连接 a2dpConnect

        本文链接:https://www.haomeiwen.com/subject/unfioctx.html