android 蓝牙耳机 判断,Android实现蓝牙耳机连接
前言
最近看了下藍牙耳機連接的問題,查閱了相關資料,再此做一個總結。
本文參考以下鏈接:
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 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;
}
若已配對列表中沒有你的藍牙耳機設備,則需要搜索
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();
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 點擊設備列表去連接藍牙耳機或者開啟藍牙掃描
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中以下權限設置:
五. 效果圖
5.gif
ok,今天的內容就講到這里,謝謝。
總結
以上是生活随笔為你收集整理的android 蓝牙耳机 判断,Android实现蓝牙耳机连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android个人信息管理系统 源代码,
- 下一篇: android 图标的格式,Androi