轉(zhuǎn)載請注明出處:http://blog.csdn.net/vnanyesheshou/article/details/71713786
本文主要是Android做為Audio Source端,A2DP的基本操作:包括連接、斷開連接、設置優(yōu)先級、獲取優(yōu)先級、獲取A2DP連接狀態(tài)、獲取A2DP連接的設備列表等功能。
1 簡介
A2DP全名是Advanced Audio Distribution Profile,高質(zhì)量音頻數(shù)據(jù)傳輸?shù)膮f(xié)議,其定義里了傳送單聲道或立體聲等高質(zhì)量音頻(區(qū)別于藍牙SCO鏈路上傳輸?shù)钠胀ㄕZ音)信息的協(xié)議和過程。A2DP的典型應用是將音樂播放器的音頻數(shù)據(jù)發(fā)送到耳機或音箱。
A2DP定義了兩種角色:
Audio Source(音頻源) 音頻的輸入端對音頻數(shù)據(jù)進行編碼,發(fā)送到Sink端。
Audio Sink(音頻接收器) 接收到音頻數(shù)據(jù)后,進行解碼操作還原出音頻。
2 A2DP profile
要想操作A2DP相關(guān),首先要獲取A2DP代理對象,獲取代理對象的方法比較簡單,如下:
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if(!mBtAdapter.isEnabled()){
//彈出對話框提示用戶是后打開
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, 1);
}
//獲取A2DP代理對象
mBtAdapter.getProfileProxy(mContext, mListener, BluetoothProfile.A2DP);
getProfileProxy并不會直接返回A2DP代理對象,而是通過mListener中回調(diào)獲取。
private ServiceListener mListener = new ServiceListener() {
@Override
public void onServiceDisconnected(int profile) {
if(profile == BluetoothProfile.A2DP){
mA2dp = null;
}
}
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if(profile == BluetoothProfile.A2DP){
mA2dp = (BluetoothA2dp) proxy; //轉(zhuǎn)換
}
}
};
成功會回調(diào)mListener中的onServiceConnected函數(shù),判斷proflie是否為BluetoothProfile.A2DP,轉(zhuǎn)換為BluetoothA2dp對象。通過代理對象即可進行A2DP的相關(guān)操作了。
3 A2DP操作
A2DP連接首先需要與藍牙耳機進行配對,如何配對這里就不細說了。
我這里是連接到之前配對過的一個設備。設備名稱為:
private final String BT_NAME = "QCY-QY7";
獲取該設備,首先獲取配對的藍牙設備,然后遍歷這些藍牙設備,找出藍牙名稱符合條件的設備,就是要操作的設備,
//獲取配對的藍牙設備
Set<BluetoothDevice> bondDevice = mBtAdapter.getBondedDevices();
for(BluetoothDevice device:bondDevice){
//獲取指定名稱的設備
if(BT_NAME.equals(device.getName())){
mConnectDevice = device;
}
}
mConnectDevice為要操作的設備。
1 A2DP連接
private void connectA2dp(BluetoothDevice device){
setPriority(mConnectDevice, 100); //設置priority
try {
//通過反射獲取BluetoothA2dp中connect方法(hide的),進行連接。
Method connectMethod =BluetoothA2dp.class.getMethod("connect",
BluetoothDevice.class);
connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
}
BluetoothA2dp中的connect方法是hide的,不能直接訪問,需要通過反射的機制獲取該方法進行連接。連接成功后手機可以播放音樂,聲音就會從藍牙耳機出來。
2 斷開連接
private void disConnectA2dp(BluetoothDevice device){
setPriority(mConnectDevice, 0);
try {
//通過反射獲取BluetoothA2dp中connect方法(hide的),斷開連接。
Method connectMethod =BluetoothA2dp.class.getMethod("disconnect",
BluetoothDevice.class);
connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
}
BluetoothA2dp中的disconnect方法也是hide的,與connect類似。
3 設置優(yōu)先級
變量 | 值 |
---|---|
PRIORITY_OFF | 0 |
PRIORITY_ON | 100 |
PRIORITY_AUTO_CONNECT | 1000 |
PRIORITY_UNDEFINED | -1 |
設置優(yōu)先級是必要的,否則可能導致連接或斷開連接失敗等問題。
public void setPriority(BluetoothDevice device, int priority) {
if (mA2dp == null) return;
try {//通過反射獲取BluetoothA2dp中setPriority方法(hide的),設置優(yōu)先級
Method connectMethod =BluetoothA2dp.class.getMethod("setPriority",
BluetoothDevice.class,int.class);
connectMethod.invoke(mA2dp, device, priority);
} catch (Exception e) {
e.printStackTrace();
}
}
4 獲取優(yōu)先級
public int getPriority(BluetoothDevice device) {
int priority = 0;
if (mA2dp == null) return priority;
try {//通過反射獲取BluetoothA2dp中g(shù)etPriority方法(hide的),獲取優(yōu)先級
Method connectMethod =BluetoothA2dp.class.getMethod("getPriority",
BluetoothDevice.class);
priority = (Integer) connectMethod.invoke(mA2dp, device);
} catch (Exception e) {
e.printStackTrace();
}
return priority;
}
5 獲取與某設備A2DP連接狀態(tài)
mA2dp.getConnectionState(device);
6 獲取連接設備列表
//返回值類型List<BluetoothDevice>
mA2dp.getConnectedDevices();
7 A2DP是否正在發(fā)送音頻流
//返回值類型boolean,表示設備是否在通過A2DP發(fā)送音頻流。
mA2dp.isA2dpPlaying(device);
4 狀態(tài)監(jiān)聽
通過廣播接收者監(jiān)聽A2DP連接狀態(tài)的改變,A2DP播放狀態(tài)的改變。
private void initReceiver(){
//注冊廣播接收者監(jiān)聽狀態(tài)改變
IntentFilter filter = new IntentFilter(BluetoothA2dp.
ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
廣播接收者,通過intent獲取狀態(tài)值。
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG,"onReceive action="+action);
//A2DP連接狀態(tài)改變
if(action.equals(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)){
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_DISCONNECTED);
Log.i(TAG,"connect state="+state);
}else if(action.equals(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)){
//A2DP播放狀態(tài)改變
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothA2dp.STATE_NOT_PLAYING);
Log.i(TAG,"play state="+state);
}
}
};
有問題歡迎交流指正。
歡迎大家關(guān)注、評論、點贊。
你們的支持是我堅持的動力。