android 蓝牙耳机 判断,Android实现蓝牙耳机连接
前言
最近看了下藍(lán)牙耳機(jī)連接的問題,查閱了相關(guān)資料,再此做一個(gè)總結(jié)。
本文參考以下鏈接:
Android實(shí)現(xiàn)主動(dòng)連接藍(lán)牙耳機(jī)
再次對作者表示感謝。
今天涉及的內(nèi)容有:
流程講解
新建廣播BluetoothReceiver,用于監(jiān)聽處理藍(lán)牙連接過程中各狀態(tài)
在MainActivity中調(diào)用
注意的點(diǎn)
效果圖
下面做以講解
一. 流程講解
在實(shí)現(xiàn)藍(lán)牙耳機(jī)鏈接的時(shí)候,需要做一些前期工作,第一步,判斷設(shè)備是否支持藍(lán)牙。
1.1 設(shè)備是否支持藍(lán)牙
/**設(shè)備是否支持藍(lán)牙**/
public boolean isSupportBluetooth() {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
return true;
}
return false;
}
若支持藍(lán)牙,則需要判斷設(shè)備藍(lán)牙是否打開
1.2 設(shè)備藍(lán)牙是否開啟
/**藍(lán)牙是否已經(jīng)啟動(dòng)**/
public boolean isBluetoothOpen() {
if (mBluetoothAdapter != null) {
return mBluetoothAdapter.isEnabled();
}
return false;
}
如果藍(lán)牙沒有開啟,則需要請求開啟藍(lán)牙
1.3 請求開啟藍(lán)牙
/**請求啟動(dòng)藍(lán)牙**/
public void requestStartBluetooth(int requestCode,Context context) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((MainActivity)context).startActivityForResult(enableBtIntent, requestCode);
}
當(dāng)然,藍(lán)牙還有一個(gè)強(qiáng)制開啟的方法:
/**強(qiáng)制打開藍(lán)牙**/
public void openBluetooth(){
if(isSupportBluetooth()){
mBluetoothAdapter.enable();
}
}
藍(lán)牙開啟后,接下來是查詢已配對過的設(shè)備
1.3 獲取配對過的設(shè)備列表
/**查詢配對設(shè)備**/
public List checkDevices() {
List devices=new ArrayList<>();
if(mBluetoothAdapter!=null){
Set pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices != null && pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
devices.add(device);
}
}
}
return devices;
}
若已配對列表中沒有你的藍(lán)牙耳機(jī)設(shè)備,則需要搜索
1.4 搜索新設(shè)備
/**發(fā)現(xiàn)新設(shè)備**/
public void findBluetoothDevice() {
//其實(shí)是啟動(dòng)了一個(gè)異步線程,該方法將立即返回一個(gè)布爾值,指示發(fā)現(xiàn)是否已成功啟動(dòng)。
// 發(fā)現(xiàn)過程通常涉及大約12秒的查詢掃描,隨后是每個(gè)找到的設(shè)備的頁面掃描以檢索其藍(lán)牙名稱
if(mBluetoothAdapter!=null && mBluetoothAdapter.isEnabled() && !mBluetoothAdapter.isDiscovering()){
if (mBluetoothAdapter.startDiscovery()) {
LogUtil.i("=======已成功啟動(dòng)尋找新設(shè)備的異步線程=======");
} else {
LogUtil.i("=======啟動(dòng)尋找新設(shè)備的異步線程失敗=======");
}
}
}
然后便是進(jìn)行藍(lán)牙耳機(jī)的配對,連接。
以上基本的藍(lán)牙方法,我已經(jīng)封裝到BluetoothManager類中。
在藍(lán)牙耳機(jī)的搜索,配對,連接等過程中,我們需要新建一個(gè)廣播,對各個(gè)過程做一個(gè)監(jiān)聽。
二. 新建廣播BluetoothReceiver,用于監(jiān)聽處理藍(lán)牙連接過程中各狀態(tài)
下面給出BluetoothReceiver中主要代碼:
@Override
public void onReceive(Context context, Intent intent){
LogUtil.i("=========藍(lán)牙接收處理廣播========"+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();
//找到指定的藍(lán)牙設(shè)備
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())){
//添加到設(shè)備列表
mOnBluetoothListener.deviceFound(device);
}
}else{//找到可用藍(lán)牙
if(StringUtil.isNotEmpty(device.getName())){
LogUtil.i("=====Found device====11===" + device.getName()+" address:"+device.getAddress());
//添加到設(shè)備列表
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.");
//取消搜索,連接藍(lán)牙設(shè)備
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.");
//不知道是藍(lán)牙耳機(jī)的關(guān)系還是什么原因,經(jīng)常配對不成功
//配對不成功的話,重新嘗試配對
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.");
// //藍(lán)牙已打開,開始搜索并連接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中調(diào)用
3.1 初始化時(shí)注冊廣播,掃描設(shè)備列表
//注冊廣播
registerReceiver();
//初始化設(shè)備列表
initDeviceList();
//發(fā)現(xiàn)新設(shè)備
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方法為:
/**發(fā)現(xiàn)新設(shè)備**/
private void findDevices(){
if(BluetoothManager.getInstance().isSupportBluetooth()&&BluetoothManager.getInstance().isBluetoothOpen()){
//強(qiáng)制打開藍(lán)牙
BluetoothManager.getInstance().openBluetooth();
Listlist=BluetoothManager.getInstance().checkDevices();
LogUtil.i("====list=====list=="+list.size());
Iterator 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 點(diǎn)擊設(shè)備列表去連接藍(lán)牙耳機(jī)或者開啟藍(lán)牙掃描
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,"本設(shè)備不支持藍(lán)牙");
return;
}
if(!BluetoothManager.getInstance().isBluetoothOpen()){
//去啟動(dòng)藍(lán)牙
BluetoothManager.getInstance().requestStartBluetooth(REQUEST_ENABLE_BT,mContext);
}else{
LogUtil.i("====開始配對=======");
//綁定BluetoothA2DP,獲得service
BluetoothManager.getInstance().getBluetoothA2DP(mContext);
//開始配對
BluetoothManager.getInstance().createBond(device);
}
}
});
3.3 關(guān)閉資源
退出app的時(shí)候需要關(guān)閉藍(lán)牙耳機(jī)連接
//注銷藍(lán)牙鏈接
BluetoothManager.getInstance().disableAdapter();
注銷廣播
//注銷廣播
if(mBluetoothReceiver!=null){
mContext.unregisterReceiver(mBluetoothReceiver);
}
當(dāng)然,你還可以考慮是否需要關(guān)閉藍(lán)牙
//關(guān)閉藍(lán)牙
BluetoothManager.getInstance().closeBluetooth();
四. 注意的點(diǎn)
藍(lán)牙耳機(jī)的連接需要藍(lán)牙權(quán)限,所以你得在你的mainfast.xml中以下權(quán)限設(shè)置:
五. 效果圖
5.gif
ok,今天的內(nèi)容就講到這里,謝謝。
總結(jié)
以上是生活随笔為你收集整理的android 蓝牙耳机 判断,Android实现蓝牙耳机连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android个人信息管理系统 源代码,
- 下一篇: android 图标的格式,Androi