代码路径:
packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
1、在AvrcpControllerStateMachine.java中MESSAGE_PROCESS_PLAY_STATUS_CHANGED和MESSAGE_PROCESS_PLAY_POS_CHANGED就是获取到播放状态
case MESSAGE_PROCESS_PLAY_POS_CHANGED:
if (msg.arg2 != -1) {
mAddressedPlayer.setPlayTime(msg.arg2);
broadcastPlayBackStateChanged(getCurrentPlayBackState());
}
break;
case MESSAGE_PROCESS_PLAY_STATUS_CHANGED:
int status = msg.arg1;
mAddressedPlayer.setPlayStatus(status);
broadcastPlayBackStateChanged(getCurrentPlayBackState());
if (status == PlaybackState.STATE_PLAYING) {
a2dpSinkService.informTGStatePlaying(mRemoteDevice.mBTDevice, true);
} else if (status == PlaybackState.STATE_PAUSED
|| status == PlaybackState.STATE_STOPPED) {
a2dpSinkService.informTGStatePlaying(mRemoteDevice.mBTDevice, false);
}
break;
2、在AvrcpControllerStateMachine.java中broadcastPlayBackStateChanged()方法中发送音乐播放状态出去
public static final String ACTION_TRACK_EVENT =
"android.bluetooth.avrcp-controller.profile.action.TRACK_EVENT";
private void broadcastPlayBackStateChanged(PlaybackState state) {
Intent intent = new Intent(AvrcpControllerService.ACTION_TRACK_EVENT);
intent.putExtra(AvrcpControllerService.EXTRA_PLAYBACK, state);
if (DBG) {
Log.d(TAG, " broadcastPlayBackStateChanged = " + state.toString());
}
mContext.sendBroadcast(intent, ProfileService.BLUETOOTH_PERM);
}
3、在我们自己的服务中可以监听该广播:android.bluetooth.avrcp-controller.profile.action.TRACK_EVENT,并且做相应的处理
if (Constance.ACTION_TRACK_EVENT.equals(action)) {
PlaybackState playbackState = intent.getParcelableExtra(Constance.EXTRA_PLAYBACK);
if (null != playbackState){
Log.d(TAG, "playbackState == "+playbackState);
int playState = playbackState.getState();
if (playState == PlaybackState.STATE_PLAYING) {
isPlaying = true;
} else if (playState == PlaybackState.STATE_PAUSED) {
isPlaying = false;
}
Intent stateIntent = new Intent(MusicFragment.ACTION_TRACK_EVENT);
stateIntent.putExtra(MusicFragment.NAME_ISPLAYING, isPlaying);
sendBroadcast(stateIntent);
}
}
4、收到蓝牙音乐播放的广播后就可以开启动画
private final BroadcastReceiver mReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "receiver action == "+action);
if (ACTION_TRACK_EVENT.equals(action)) {
boolean isPlaying = intent.getBooleanExtra(NAME_ISPLAYING, false);
if (isPlaying) {
if(!isPlayState){
playorpause.setImageResource(R.mipmap.iv_music_pause);
startPropertyAnim();
isPlayState = true;
}
} else {
if(isPlayState){
stopRotate();
playorpause.setImageResource(R.mipmap.iv_music_play);
isPlayState = false;
}
}
} else if (ACTION_ATTR_INFO.equals(action)) {
String artist = intent.getStringExtra("artist");
String trackTitle = intent.getStringExtra("trackTitle");
String album = intent.getStringExtra("album");
tvSong.setText(trackTitle);
tvSinger.setText(artist);
Log.i(TAG, "artist == "+artist+" trackTitle == "+trackTitle+" album == "+album);
}
}
};
4、播放、暂停、上一首、下一首蓝牙音乐。
播放:BluetoothAvrcp.PASSTHROUGH_ID_PLAY
暂停:BluetoothAvrcp.PASSTHROUGH_ID_PAUSE
下一首:BluetoothAvrcp.PASSTHROUGH_ID_BACKWARD
上一首:BluetoothAvrcp.PASSTHROUGH_ID_FORWARD
private void sendPlayPauseCmd() {
if (BluetoothAdapter.getDefaultAdapter().isEnabled()) {
if(isPlayState){
bluetoothManager.sendGroupNavigationCmd(BluetoothAvrcp.PASSTHROUGH_ID_PAUSE);
playorpause.setImageResource(R.mipmap.iv_music_play);
stopRotate();
isPlayState = false;
}else{
bluetoothManager.sendGroupNavigationCmd(BluetoothAvrcp.PASSTHROUGH_ID_PLAY);
playorpause.setImageResource(R.mipmap.iv_music_pause);
startPropertyAnim();
isPlayState = true;
}
} else {
ToastUtil.showToast(getActivity(), R.string.bt_disable);
}
}
public void sendGroupNavigationCmd(int keyCode) {
if (!mAdapter.isEnabled()) {
ToastUtil.showToast(mContext, mContext.getResources().getString(R.string.bt_disable));
return;
}
if (mAvrcpCt == null) {
Log.d(TAG, "sendGroupNavigationCmd, proxy is not connected");
return;
}
List<BluetoothDevice> connectDevices = mA2dpSink.getConnectedDevices();
BluetoothDevice mDevice = null;
if (!connectDevices.isEmpty()) {
mDevice = connectDevices.remove(0);
} else {
Log.d(TAG, "sendPlayPauseCmd, mDevice is null");
return;
}
Log.i(TAG, "sendGroupNavigationCmd ,keyCode == "+keyCode);
mAvrcpCt.sendPassThroughCmd(mDevice,keyCode,BluetoothAvrcp.PASSTHROUGH_STATE_PRESS);
mAvrcpCt.sendPassThroughCmd(mDevice,keyCode,BluetoothAvrcp.PASSTHROUGH_STATE_RELEASE);
}