参考:

Android实现主动连接蓝牙耳机

具体实现:

    private static final String TAG = "BluetoothA2DPTest";
private BroadcastReceiver mBroadcastReceiver;
private BluetoothA2dp mBluetoothA2dp;
private BluetoothAdapter mBluetoothAdapter;
private String DEVICE_NAME = "KUWO_K1";
private BluetoothDevice mBluetoothDevice;
private MediaPlayer mMediaPlayer; private void initParameters(){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null){
Log.e(TAG,"have no bluetooth adapter.");
return;
} if(!mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.enable();
}else{
//开始搜索附近蓝牙
startDiscovery();
//绑定BluetoothA2DP,获得service
getBluetoothA2DP();
} //监听广播
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice device;
switch (intent.getAction()) {
case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
//<editor-fold>
switch (intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)) {
case BluetoothA2dp.STATE_CONNECTING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "device: " + device.getName() +" connecting");
break;
case BluetoothA2dp.STATE_CONNECTED:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "device: " + device.getName() +" connected");
//连接成功,开始播放
startPlay();
break;
case BluetoothA2dp.STATE_DISCONNECTING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "device: " + device.getName() +" disconnecting");
break;
case BluetoothA2dp.STATE_DISCONNECTED:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(TAG, "device: " + device.getName() +" disconnected");
// setResultPASS();
break;
default:
break;
}
//</editor-fold>
break;
case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:
//<editor-fold>
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1);
switch (state) {
case BluetoothA2dp.STATE_PLAYING:
Log.i(TAG, "state: playing.");
break;
case BluetoothA2dp.STATE_NOT_PLAYING:
Log.i(TAG, "state: not playing");
break;
default:
Log.i(TAG, "state: unkown");
break;
}
//</editor-fold>
break;
case BluetoothDevice.ACTION_FOUND:
//<editor-fold>
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int deviceClassType = device.getBluetoothClass().getDeviceClass();
//找到指定的蓝牙设备
if ((deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
|| deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES)
&& device.getName().equals(DEVICE_NAME)) {
Log.i(TAG, "Found device:" + device.getName());
mBluetoothDevice = device;
//start bond,开始配对
createBond();
}
//</editor-fold>
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
//<editor-fold>
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.BOND_NONE);
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (bondState){
case BluetoothDevice.BOND_BONDED: //配对成功
Log.i(TAG,"Device:"+device.getName()+" bonded.");
mBluetoothAdapter.cancelDiscovery(); //取消搜索
connect(); //连接蓝牙设备
break;
case BluetoothDevice.BOND_BONDING:
Log.i(TAG,"Device:"+device.getName()+" bonding.");
break;
case BluetoothDevice.BOND_NONE:
Log.i(TAG,"Device:"+device.getName()+" not bonded.");
//不知道是蓝牙耳机的关系还是什么原因,经常配对不成功
//配对不成功的话,重新尝试配对
createBond();
break;
default:
break; } //</editor-fold>
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
//<editor-fold>
state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
Log.i(TAG, "BluetoothAdapter is turning on.");
break;
case BluetoothAdapter.STATE_ON:
Log.i(TAG, "BluetoothAdapter is on.");
//蓝牙已打开,开始搜索并连接service
startDiscovery();
getBluetoothA2DP();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.i(TAG, "BluetoothAdapter is turning off.");
break;
case BluetoothAdapter.STATE_OFF:
Log.i(TAG, "BluetoothAdapter is off.");
break;
}
//</editor-fold>
break;
default:
break;
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
filter.addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver, filter);
} private void startDiscovery(){
Log.i(TAG,"mBluetoothAdapter startDiscovery.");
if(mBluetoothAdapter!=null && mBluetoothAdapter.isEnabled() && !mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.startDiscovery();
}
} private void getBluetoothA2DP(){
Log.i(TAG,"getBluetoothA2DP");
if(mBluetoothAdapter == null){
return;
} if(mBluetoothA2dp != null){
return;
} mBluetoothAdapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if(profile == BluetoothProfile.A2DP){
//Service连接成功,获得BluetoothA2DP
mBluetoothA2dp = (BluetoothA2dp)proxy;
}
} @Override
public void onServiceDisconnected(int profile) { }
},BluetoothProfile.A2DP);
} private void createBond() {
Log.i(TAG, "createBond");
mBluetoothDevice.createBond();
} //connect和disconnect都是hide方法,普通应用只能通过反射机制来调用该方法
private void connect(){
Log.i(TAG,"connect");
if(mBluetoothA2dp == null){
return;
}
if(mBluetoothDevice == null){
return;
} try {
Method connect = mBluetoothA2dp.getClass().getDeclaredMethod("connect", BluetoothDevice.class);
connect.setAccessible(true);
connect.invoke(mBluetoothA2dp,mBluetoothDevice);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Log.e(TAG,"connect exception:"+e);
e.printStackTrace();
}
} private void startPlay(){
Log.i(TAG, "startPlay");
AudioManager mAudioManager= (AudioManager)getSystemService(AUDIO_SERVICE);
if(mAudioManager!=null){
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,maxVolume,0);
} Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+ R.raw.speaker_test);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.reset();
try {
mMediaPlayer.setDataSource(this,uri);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//播放完成,可以考虑断开连接
disconnect();
}
});
mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e(TAG, "Playback error.");
return false;
}
});
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch(IllegalStateException|IOException e) {
Log.e(TAG, "Exception: prepare or start mediaplayer");
setResultFAIL();
}
} //程序退出前,要release播放器
private void stopPlay(){
Log.i(TAG,"stopPlay");
if(mMediaPlayer!=null){
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
} private void disconnect(){
Log.i(TAG,"disconnect");
if(mBluetoothA2dp == null){
return;
}
if(mBluetoothDevice == null){
return;
} try {
Method disconnect = mBluetoothA2dp.getClass().getDeclaredMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(mBluetoothA2dp,mBluetoothDevice);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
Log.e(TAG,"connect exception:"+e);
e.printStackTrace();
}
} //取消配对
private void unPairAllDevices(){
Log.i(TAG,"unPairAllDevices");
for(BluetoothDevice device:mBluetoothAdapter.getBondedDevices()){
try {
Method removeBond = device.getClass().getDeclaredMethod("removeBond");
removeBond.invoke(device);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
} //注意,在程序退出之前(OnDestroy),需要断开蓝牙相关的Service
//否则,程序会报异常:service leaks
private void disableAdapter(){
Log.i(TAG,"disableAdapter");
if(mBluetoothAdapter == null){
return;
} if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
} //关闭ProfileProxy,也就是断开service连接
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.A2DP,mBluetoothA2dp);
if(mBluetoothAdapter.isEnabled()){
boolean ret = mBluetoothAdapter.disable();
Log.i(TAG,"disable adapter:"+ret);
}
}

Android连接蓝牙耳机播放音乐的更多相关文章

  1. mac,macbook 连接蓝牙耳机播放音乐断断续续

    个人的情况是, mac本连的网线,用的无线鼠标, 屋里80多号人都在用笔记本,应该也有好多开着无线的东西 解决方法: mac 或macbook 连接蓝牙耳机播放音乐断断续续的原因, 在网上找了好多方法 ...

  2. Android实例-MediaPlayer播放音乐和视频(XE8+小米2)

    结果: 1.播放视频需要手动放入MediaPlayerControl1控件,设置MediaPlayerControl1.MediaPlayer := MediaPlayer1; 2.播放声音文件正常, ...

  3. Android应用开发--MP3音乐播放器代码实现(一)

    需求1:将内存卡中的MP3音乐读取出来并显示到列表当中 1.   从数据库中查询所有音乐数据,保存到List集合当中,List当中存放的是Mp3Info对象 2.   迭代List集合,把每一个Mp3 ...

  4. Android命令行播放MP3音乐

    /*************************************************************************** * Android命令行播放MP3音乐 * 说 ...

  5. Android跟蓝牙耳机建立连接有两种方式

    Android 跟蓝牙耳机建立连接有两种方式. 1. Android 主动跟蓝牙耳机连BluetoothSettings 中和蓝牙耳机配对上之后, BluetoothHeadsetService 会收 ...

  6. android 播放音乐-进度条

    今天学渣研究了一下使用MediaPlayer播放音乐时加入进度条,进度条如今用的是android自带的seekbar,后期会跟换UI的,在之前可以播放音乐的基础上,如今加入的主要功能有两个: 1实时显 ...

  7. Android基于发展Service音乐播放器

    这是一个基于Service组件的音乐播放器,程序的音乐将会由后台的Service组件负责播放,当后台的播放状态改变时,程序将会通过发送广播通知前台Activity更新界面:当用户单击前台Activit ...

  8. Android中播放音乐的几种方式

    前言 前几天一直在研究RxJava2,也写了记录了几篇博客,但因为工作任务原因,需要研究音频相关的知识,暂时放下Rxjava,本文的demo中,MediaPalyer 部分使用RxJava编写一点逻辑 ...

  9. Android播放器推荐:可以播放本地音乐、视频、在线播放音乐、视频、网络收音机等

    下载链接:http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTAxNTczfGMyNjNkMzFlfDEzNzY1MzkwNTR8 ...

随机推荐

  1. UIViewController 的 presentedViewController 和 presentingViewController

    #import "TestViewController.h" #import "OneViewController.h" 在TextViewController ...

  2. Javascript面向对象编程

    https://segmentfault.com/a/1190000002900676 介绍 和java这种基于类(class-base)的面向对象的编程语言不同,javascript没有类这样的概念 ...

  3. php发送http请求方法实例及详解

    http请求有get,post. php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]. file_get_contents();详情见:http://www.whosmall.com ...

  4. UI学习之常用方法

    1.-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递) if ([delegate respond ...

  5. 自然语言20_The corpora with NLTK

    QQ:231469242 欢迎喜欢nltk朋友交流 https://www.pythonprogramming.net/nltk-corpus-corpora-tutorial/?completed= ...

  6. Spring解析

    Spring还是蛮有技术含量的,可以自己用代码实践一遍,找了一篇实践的案例: http://qingwengang.iteye.com/blog/621678 先mark下,等后面有时间了实践一遍. ...

  7. CSS学习笔记——选择器

    选择器 当我们定义一条样式规则时候,这条样式规则会作用于网页当中的某些元素,而我们的规定的这些元素的规则就叫做选择器. 常用的选择器: 1.id选择器 #idname 2.类选择器 .classnam ...

  8. C++ Pointer-to-Member Selector

    http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm https://m ...

  9. Xcode卡顿解决方案

    1.禁用indexing 在终端(terminal) 输入 defaults write com.apple.dt.XCode IDEIndexDisable 并重启Xcode. (不是大神勿用哈,附 ...

  10. SCI答复审稿人的策略和答复信的写作技巧

    SCI论文被录用的最后一步 –---答复审稿人的策略和答复信的写作技巧 [好文转载] : 一篇稿子从酝酿到成型历经艰辛,投出去之后又是漫长的等待,好容易收到编辑的回信,得到的往往又是审稿人不留情面的一 ...