Android 蓝牙基础篇之 —— A2DP

本篇文章主要介绍 A2DP 基础操作。

  • 介绍

A2DP :Advanced Audio Distribution Profile。高质量音频数据传输的协议,其定义里了传送单声道或立体声等高质量音频(区别于蓝牙SCO链路上传输的普通语音)信息的协议和过程。A2DP的典型应用是将音乐播放器的音频数据发送到耳机或音箱。

A2DP 定义了两种角色:

Audio Source :(音频源) 音频的输入端对音频数据进行编码,发送到Sink端。
Audio Sink :     (音频接收器) 接收到音频数据后,进行解码操作还原出音频。

 

  • 初始化 A2DP 代理对象
  private void initBluetooth() {
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBtAdapter.isEnabled()) {
            return;
        }
        //获取A2DP代理对象
        mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);
    }

    private void initReceiver() {
        //广播接收者监听状态
        IntentFilter filter = new IntentFilter(BluetoothA2dp.
                ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
        mContext.registerReceiver(mReceiver, filter);
    }
  • 初始化 A2DP 代理对象
  private void initBluetooth() {
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBtAdapter.isEnabled()) {
            return;
        }
        //获取A2DP代理对象
        mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);
    }

    private void initReceiver() {
        //广播接收者监听状态
        IntentFilter filter = new IntentFilter(BluetoothA2dp.
                ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
        mContext.registerReceiver(mReceiver, filter);
    }
  • 广播接收者,获取连接状态

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            //A2DP连接状态改变
            if (action != null) {
                if (action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)) {
                    int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
                    callBackA2dpConnectState(state);

                } else if (action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)) {
                    //A2DP播放状态改变
                    int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
                    Log.i(TAG, "play state=" + state);
                }
            }
        }
    };
  • 获取 A2DP 代理对象 proxy 
  private BluetoothProfile.ServiceListener mListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceDisconnected(int profile) {
            Log.i(TAG, "onServiceDisconnected profile=" + profile);
            if (profile == BluetoothProfile.A2DP) {
                mA2dp = null;
            }
        }

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            Log.i(TAG, "onServiceConnected profile=" + profile);
            if (profile == BluetoothProfile.A2DP) {
                mA2dp = (BluetoothA2dp) proxy; //转换
                if (onBluetoothA2dpReadyListener != null) {
                    onBluetoothA2dpReadyListener.onBluetoothA2dpReady();
                }
            }
        }
    };
  • 连接

 a2dp connect is hide  需要通过反射获取,连接成功之后,可以在蓝牙设备中播放音乐等音频

   public void connectA2dp(BluetoothDevice device) {
        Log.i(TAG, "connect to device :" + device);
        mConnectDevice = device;
        setPriority(device, 100); //设置priority
        try {
            //通过反射获取BluetoothA2dp中connect方法(hide的),进行连接。
            Method connectMethod = BluetoothA2dp.class.getMethod("connect",
                    BluetoothDevice.class);
            connectMethod.invoke(mA2dp, device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 断开连接
 public void disConnectA2dp(BluetoothDevice device) {
        mConnectDevice = null;
        setPriority(device, 0);
        try {
            //通过反射获取BluetoothA2dp中connect方法(hide的),断开连接。
            Method connectMethod = BluetoothA2dp.class.getMethod("disconnect",
                    BluetoothDevice.class);
            connectMethod.invoke(mA2dp, device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 设置优先级,一般 priority = 100
    public void setPriority(BluetoothDevice device, int priority) {
        if (mA2dp == null) return;
        try {//通过反射获取BluetoothA2dp中setPriority方法(hide的),设置优先级
            Method connectMethod = BluetoothA2dp.class.getMethod("setPriority",
                    BluetoothDevice.class, int.class);
            connectMethod.invoke(mA2dp, device, priority);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

好了,到这里相信大家都明白了,HFP 和 A2DP 的操作流程基本一样。

 

 

 

你可能感兴趣的:(Android,A2DP)