A2DPProfile 定义了高质量音频数据传输的协议和过程,包括立体声和单声道数据的传输。这里的高质量音频指的是单声道(Mono)和立体声(Sterco) 的音频,主要区别于蓝牙 SCO 链路上传输的普通语音。A2DP 的典型应用是将音乐播放器的音频数据发送到耳机或音箱。
由于蓝牙提供的带宽较窄,音频数据可能需要进行有效的压缩才能保证接收端的实时播放。
目前 A2DP 只定义了点对点的音频传输,没有定义广播式的音频传输,可能是由于速率的原因。
A2DP 建立在 AVDTP 传输协议的基础之上,AVDTP 规定了链接是如何建立的,连接建立好之后,音频数据经过压缩之后,便可以收发了。
直接上代码的吧,有很详细的注释,各位看官应该看起来问题不大:
public class BluetoothChatA2dpService {
private BluetoothA2dp mA2dp;
private String TAG = "BluetoothChatA2dpService";
private BluetoothDevice mConnectDevice;
private BluetoothAdapter mBtAdapter;
private static Context mContext;
private static BluetoothChatA2dpService bluetoothA2dpChatService;
private int callBackState = 0;
public interface OnConnectionListener {
void onConnectionStateChanged(
BluetoothDevice bluetoothDevice, int state);
}
public interface OnBluetoothA2dpReadyListener {
void onBluetoothA2dpReady();
}
private OnBluetoothA2dpReadyListener onBluetoothA2dpReadyListener;
public void setOnBluetoothA2dpReadyListener(OnBluetoothA2dpReadyListener listener) {
onBluetoothA2dpReadyListener = listener;
}
private OnConnectionListener onConnectionListener;
public void setOnConnectionListener(OnConnectionListener listener) {
onConnectionListener = listener;
}
private BluetoothChatA2dpService() {
initReceiver();
initBluetooth();
}
public int getBluetoothDeviceA2dpStatus(BluetoothDevice bluetoothDevice) {
if (mA2dp == null) {
return -2;
}
return mA2dp.getConnectionState(bluetoothDevice);
}
private void initBluetooth() {
mBtAdapter = BluetoothAdapter.getDefaultAdapter();