代码地址如下:
http://www.demodashi.com/demo/13259.html
前言
讲讲android对于蓝牙耳机连接技术的实现
今天涉及的内容有:
- 流程讲解
- 新建广播BluetoothReceiver,用于监听处理蓝牙连接过程中各状态
- 在MainActivity中调用
- 注意的点
- 项目结构图和效果图
下面做以讲解
一. 流程讲解
在实现蓝牙耳机链接的时候,需要做一些前期工作,第一步,判断设备是否支持蓝牙。
1.1 设备是否支持蓝牙
/**设备是否支持蓝牙**/
public boolean isSupportBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
return true;
}
return false;
}
若支持蓝牙,则需要判断设备蓝牙是否打开
1.2 设备蓝牙是否开启
/**蓝牙是否已经启动**/
public boolean isBluetoothOpen() {
if (mBluetoothAdapter != null) {
return mBluetoothAdapter.isEnabled();
}
return false;
}
如果蓝牙没有开启,则需要请求开启蓝牙
1.3 请求开启蓝牙
/**请求启动蓝牙**/
public void requestStartBluetooth(int requestCode,Context context) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((MainActivity)context).startActivityForResult(enableBtIntent, requestCode);
}
当然,蓝牙还有一个强制开启的方法:
/**强制打开蓝牙**/
public void openBluetooth(){
if(isSupportBluetooth()){
mBluetoothAdapter.enable();
}
}
蓝牙开启后,接下来是查询已配对过的设备
1.3 获取配对过的设备列表
/**查询配对设备**/
public List<BluetoothDevice> checkDevices() {
List<BluetoothDevice> devices=new ArrayList<>();
if(mBluetoothAdapter!=null){
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices != null && pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
devices.add(device);
}
}
}
return devices;
}
若已配对列表中没有你的蓝牙耳机设备,则需要搜索
1.4 搜索新设备
/**发现新设备**/
public void findBluetoothDevice() {
//其实是启动了一个异步线程,该方法将立即返回一个布尔值,指示发现是否已成功启动。
// 发现过程通常涉及大约12秒的查询扫描,随后是每个找到的设备的页面扫描以检索其蓝牙名称
if(mBluetoothAdapter!=null && mBluetoothAdapter.isEnabled() && !mBluetoothAdapter.isDiscovering()){
if (mBluetoothAdapter.startDiscovery()) {
LogUtil.i("=======已成功启动寻找新设备的异步线程=======");
} else {
LogUtil.i("=======启动寻找新设备的异步线程失败=======");
}
}
}
然后便是进行蓝牙耳机的配对,连接。
以上基本的蓝牙方法,我已经封装到BluetoothManager类中。
在蓝牙耳机的搜索,配对,连接等过程中,我们需要新建一个广播,对各个过程做一个监听。
二. 新建广播BluetoothReceiver,用于监听处理蓝牙连接过程中各状态
下面给出BluetoothReceiver中主要代码:
@Override
public void onReceive(Context context, Intent intent){
LogUtil.i("=========蓝牙接收处理广播========"+intent.getAction());
BluetoothDevice device;
switch (intent.getAction()) {
case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
switch (intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)) {
case BluetoothA2dp.STATE_CONNECTING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.i("device: " + device.getName() +" connecting");
break;
case BluetoothA2dp.STATE_CONNECTED:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.i("device: " + device.getName() +" connected");
mOnBluetoothListener.deviceConnected(device);
break;
case BluetoothA2dp.STATE_DISCONNECTING:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.i("device: " + device.getName() +" disconnecting");
break;
case BluetoothA2dp.STATE_DISCONNECTED:
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LogUtil.i("device: " + device.getName() +" disconnected");
break;
default:
break;
}
break;
case BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED:
int state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1);
switch (state) {
case BluetoothA2dp.STATE_PLAYING:
LogUtil.i("state: playing.");
break;
case BluetoothA2dp.STATE_NOT_PLAYING:
LogUtil.i("state: not playing");
break;
default:
LogUtil.i("state: unkown");
break;
}
break;
case BluetoothDevice.ACTION_FOUND:
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) {
LogUtil.i("Found device:" + device.getName()+" address:"+device.getAddress());
if(StringUtil.isNotEmpty(device.getName())){
//添加到设备列表
mOnBluetoothListener.deviceFound(device);
}
}else{//找到可用蓝牙
if(StringUtil.isNotEmpty(device.getName())){
LogUtil.i("=====Found device====11===" + device.getName()+" address:"+device.getAddress());
//添加到设备列表
mOnBluetoothListener.deviceFound(device);
}
}
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
switch (bondState){
case BluetoothDevice.BOND_BONDED: //配对成功
LogUtil.i("Device:"+device.getName()+" bonded.");
//取消搜索,连接蓝牙设备
mOnBluetoothListener.deviceBonded(device);
break;
case BluetoothDevice.BOND_BONDING:
LogUtil.i("Device:"+device.getName()+" bonding.");
break;
case BluetoothDevice.BOND_NONE:
LogUtil.i("Device:"+device.getName()+" not bonded.");
//不知道是蓝牙耳机的关系还是什么原因,经常配对不成功
//配对不成功的话,重新尝试配对
mOnBluetoothListener.deviceBondNone(device);
break;
default:
break;
}
break;
case BluetoothAdapter.ACTION_STATE_CHANGED:
state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
LogUtil.i("BluetoothAdapter is turning on.");
break;
case BluetoothAdapter.STATE_ON:
LogUtil.i("BluetoothAdapter is on.");
// //蓝牙已打开,开始搜索并连接service
// findBluetoothDevice();
// getBluetoothA2DP();
mOnBluetoothListener.blootoothStateOn();
break;
case BluetoothAdapter.STATE_TURNING_OFF:
LogUtil.i("BluetoothAdapter is turning off.");
break;
case BluetoothAdapter.STATE_OFF:
LogUtil.i("BluetoothAdapter is off.");
break;
}
break;
default:
break;
}
}
三. 在MainActivity中调用
3.1 初始化时注册广播,扫描设备列表
//注册广播
registerReceiver();
//初始化设备列表
initDeviceList();
//发现新设备
findDevices();
其中registerReceiver方法为:
/**注册广播**/
private void registerReceiver(){
mBluetoothReceiver=new BluetoothReceiver();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
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);
mContext.registerReceiver(mBluetoothReceiver, filter);
}
findDevices方法为:
/**发现新设备**/
private void findDevices(){
if(BluetoothManager.getInstance().isSupportBluetooth()&&BluetoothManager.getInstance().isBluetoothOpen()){
//强制打开蓝牙
BluetoothManager.getInstance().openBluetooth();
List<BluetoothDevice>list=BluetoothManager.getInstance().checkDevices();
LogUtil.i("====list=====list=="+list.size());
Iterator<BluetoothDevice> it = list.iterator();
while (it.hasNext()) {
BluetoothDevice device = it.next();
if (device != null&& StringUtil.isEmpty(device.getName())) {
it.remove();
}
}
mDevices.addAll(list);
myAdapter.notifyDataSetChanged();
}
}
3.2 点击设备列表去连接蓝牙耳机或者开启蓝牙扫描
myAdapter.setOnRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerClick(View view, int position) {
BluetoothDevice device= mDevices.get(position);
if(!BluetoothManager.getInstance().isSupportBluetooth()){
ToastUtil.showShortToast(mContext,"本设备不支持蓝牙");
return;
}
if(!BluetoothManager.getInstance().isBluetoothOpen()){
//去启动蓝牙
BluetoothManager.getInstance().requestStartBluetooth(REQUEST_ENABLE_BT,mContext);
}else{
LogUtil.i("====开始配对=======");
//绑定BluetoothA2DP,获得service
BluetoothManager.getInstance().getBluetoothA2DP(mContext);
//开始配对
BluetoothManager.getInstance().createBond(device);
}
}
});
3.3 关闭资源
退出app的时候需要关闭蓝牙耳机连接
//注销蓝牙链接
BluetoothManager.getInstance().disableAdapter();
注销广播
//注销广播
if(mBluetoothReceiver!=null){
mContext.unregisterReceiver(mBluetoothReceiver);
}
当然,你还可以考虑是否需要关闭蓝牙
//关闭蓝牙
BluetoothManager.getInstance().closeBluetooth();
四. 注意的点
蓝牙耳机的连接需要蓝牙权限,所以你得在你的mainfast.xml中以下权限设置:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
五. 项目结构图和效果图
项目结构图
运行效果图
Android实现蓝牙耳机连接
代码地址如下:
http://www.demodashi.com/demo/13259.html
注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权