前言
公司最近給我丟了一個藍牙開發的項目,不瞭解怎麼辦呢,那當然是從最基礎的開始了,所以這裏相當於做筆記了。
效果圖
打開藍牙
掃描藍牙設備
正文
話不多說,創建一個項目纔是首要的任務,創建一個名爲MyBluetooth的Android項目。
① 配置項目
在工程的build.gradle中,添加
maven { url "https://jitpack.io" }
如下圖所示
然後是在app下的build.gradle中添加依賴庫
compileOptions {//指定使用的JDK1.8
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
//Google Material控件,以及遷移到AndroidX下一些控件的依賴
implementation 'com.google.android.material:material:1.0.0'
//RecyclerView最好的適配器,讓你的適配器一目瞭然,告別代碼冗餘
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//權限請求框架
implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "io.reactivex.rxjava2:rxjava:2.0.0"
改動之後記得Sync一下,否則不生效的。
配置AndroidManifest.xml文件
<!--藍牙連接權限-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<!--藍牙通訊權限-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!--位置信息 獲取精準位置-->
<!--Android 6.0及後續版本,使用藍牙掃描,還需要添加如下的權限,且該權限還需要在使用時動態申請-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
然後改動colors.xml中系統默認的顏色
然後是styles.xml文件
② 佈局和樣式
圖片資源
在drawable下創建一個名爲progressbar.xml的樣式文件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="@drawable/icon_loading"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="360.0" />
<!-- 其中360.0值越大,轉的圈圈越快 -->
<span style="white-space:pre" />
</item>
</layer-list>
修改activity_main.xml佈局文件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<!--標題-->
<androidx.appcompat.widget.Toolbar
android:elevation="3dp"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我的藍牙"
android:textColor="#000"
android:textSize="18sp" />
</androidx.appcompat.widget.Toolbar>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#EEEEEE" />
<!--加載佈局-->
<LinearLayout
android:id="@+id/loading_lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="gone">
<ProgressBar
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/progressbar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="掃描中..." />
</LinearLayout>
<!--設備展示列表-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#EEEEEE" />
<!--掃描藍牙-->
<TextView
android:id="@+id/scan_devices"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:text="掃描藍牙" />
</LinearLayout>
在layout下創建列表展示的item的佈局文件,名爲item_device_list.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/item_device"
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="center_vertical"
android:padding="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_device_type"
android:src="@mipmap/icon_bluetooth"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:id="@+id/tv_name"
android:paddingLeft="12dp"
android:textSize="16sp"
android:text="設備名稱"
android:textColor="#000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:gravity="right"
android:id="@+id/tv_bond_state"
android:text="綁定狀態"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
<View
android:background="#EBEBEB"
android:layout_marginLeft="54dp"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</LinearLayout>
③ 編碼
在此之前呢,記得放一個工具類,用於改變狀態欄的文字和背景顏色的。創建一個util包,包下創建一個StatusBarUtil.java文件
工具類代碼如下:
package com.llw.mybluetooth.util;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 狀態欄工具類
*/
public class StatusBarUtil {
/**
* 修改狀態欄爲全透明
*
* @param activity
*/
@TargetApi(19)
public static void transparencyBar(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = activity.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 狀態欄亮色模式,設置狀態欄黑色文字、圖標,
* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
* @param activity
* @return 1:MIUUI 2:Flyme 3:android6.0
*/
public static int StatusBarLightMode(Activity activity) {
int result = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (MIUISetStatusBarLightMode(activity, true)) {
result = 1;
} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
result = 2;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
result = 3;
}
}
return result;
}
/**
* 已知系統類型時,設置狀態欄黑色文字、圖標。
* 適配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
*
* @param activity
* @param type 1:MIUUI 2:Flyme 3:android6.0
*/
public static void StatusBarLightMode(Activity activity, int type) {
if (type == 1) {
MIUISetStatusBarLightMode(activity, true);
} else if (type == 2) {
FlymeSetStatusBarLightMode(activity.getWindow(), true);
} else if (type == 3) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
}
/**
* 狀態欄暗色模式,清除MIUI、flyme或6.0以上版本狀態欄黑色文字、圖標
*/
public static void StatusBarDarkMode(Activity activity, int type) {
if (type == 1) {
MIUISetStatusBarLightMode(activity, false);
} else if (type == 2) {
FlymeSetStatusBarLightMode(activity.getWindow(), false);
} else if (type == 3) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
/**
* 設置狀態欄圖標爲深色和魅族特定的文字風格
* 可以用來判斷是否爲Flyme用戶
*
* @param window 需要設置的窗口
* @param dark 是否把狀態欄文字及圖標顏色設置爲深色
* @return boolean 成功執行返回true
*/
public static boolean FlymeSetStatusBarLightMode(Window window, boolean dark) {
boolean result = false;
if (window != null) {
try {
WindowManager.LayoutParams lp = window.getAttributes();
Field darkFlag = WindowManager.LayoutParams.class
.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
Field meizuFlags = WindowManager.LayoutParams.class
.getDeclaredField("meizuFlags");
darkFlag.setAccessible(true);
meizuFlags.setAccessible(true);
int bit = darkFlag.getInt(null);
int value = meizuFlags.getInt(lp);
if (dark) {
value |= bit;
} else {
value &= ~bit;
}
meizuFlags.setInt(lp, value);
window.setAttributes(lp);
result = true;
} catch (Exception e) {
}
}
return result;
}
/**
* 需要MIUIV6以上
*
* @param activity
* @param dark 是否把狀態欄文字及圖標顏色設置爲深色
* @return boolean 成功執行返回true
*/
public static boolean MIUISetStatusBarLightMode(Activity activity, boolean dark) {
boolean result = false;
Window window = activity.getWindow();
if (window != null) {
Class clazz = window.getClass();
try {
int darkModeFlag = 0;
Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
if (dark) {
extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//狀態欄透明且黑色字體
} else {
extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字體
}
result = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//開發版 7.7.13 及以後版本採用了系統API,舊方法無效但不會報錯,所以兩個方式都要加上
if (dark) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
} catch (Exception e) {
}
}
return result;
}
}
然後是創建設備列表展示數據的適配器了,創建一個adapter包,包下創建一個DeviceAdapter.java文件
適配器代碼如下:
package com.llw.mybluetooth.adapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.mybluetooth.R;
import java.util.List;
public class DeviceAdapter extends BaseQuickAdapter<BluetoothDevice, BaseViewHolder> {
public DeviceAdapter(int layoutResId, @Nullable List<BluetoothDevice> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, BluetoothDevice item) {
if (item.getName() == null) {
helper.setText(R.id.tv_name, "無名");
} else {
helper.setText(R.id.tv_name, item.getName());
}
ImageView imageView = helper.getView(R.id.iv_device_type);
getDeviceType(item.getBluetoothClass().getMajorDeviceClass(), imageView);
//藍牙設備綁定狀態判斷
switch (item.getBondState()) {
case 12:
helper.setText(R.id.tv_bond_state, "已配對");
break;
case 11:
helper.setText(R.id.tv_bond_state, "正在配對...");
break;
case 10:
helper.setText(R.id.tv_bond_state, "未配對");
break;
}
//添加item點擊事件
helper.addOnClickListener(R.id.item_device);
}
/**
* 刷新適配器
*/
public void changeBondDevice(){
notifyDataSetChanged();
}
/**
* 根據類型設置圖標
* @param type 類型碼
* @param imageView 圖標
*/
private void getDeviceType(int type, ImageView imageView) {
switch (type) {
case BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES://耳機
case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET://穿戴式耳機
case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE://藍牙耳機
case BluetoothClass.Device.Major.AUDIO_VIDEO://音頻設備
imageView.setImageResource(R.mipmap.icon_headset);
break;
case BluetoothClass.Device.Major.COMPUTER://電腦
imageView.setImageResource(R.mipmap.icon_computer);
break;
case BluetoothClass.Device.Major.PHONE://手機
imageView.setImageResource(R.mipmap.icon_phone);
break;
case BluetoothClass.Device.Major.HEALTH://健康類設備
imageView.setImageResource(R.mipmap.icon_health);
break;
case BluetoothClass.Device.AUDIO_VIDEO_CAMCORDER://照相機錄像機
case BluetoothClass.Device.AUDIO_VIDEO_VCR://錄像機
imageView.setImageResource(R.mipmap.icon_vcr);
break;
case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO://車載設備
imageView.setImageResource(R.mipmap.icon_car);
break;
case BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER://揚聲器
imageView.setImageResource(R.mipmap.icon_loudspeaker);
break;
case BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE://麥克風
imageView.setImageResource(R.mipmap.icon_microphone);
break;
case BluetoothClass.Device.AUDIO_VIDEO_PORTABLE_AUDIO://打印機
imageView.setImageResource(R.mipmap.icon_printer);
break;
case BluetoothClass.Device.AUDIO_VIDEO_SET_TOP_BOX://音頻視頻機頂盒
imageView.setImageResource(R.mipmap.icon_top_box);
break;
case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_CONFERENCING://音頻視頻視頻會議
imageView.setImageResource(R.mipmap.icon_meeting);
break;
case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER://顯示器和揚聲器
imageView.setImageResource(R.mipmap.icon_tv);
break;
case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_GAMING_TOY://遊戲
imageView.setImageResource(R.mipmap.icon_game);
break;
case BluetoothClass.Device.AUDIO_VIDEO_VIDEO_MONITOR://可穿戴設備
imageView.setImageResource(R.mipmap.icon_wearable_devices);
break;
default://其它
imageView.setImageResource(R.mipmap.icon_bluetooth);
break;
}
}
}
萬事俱備,現在可以進入到MainActivity.java了,進行功能的實現了。
首先實現底部TextView的點擊事件
然後會實現一個onClick方法
/**
* 控件點擊事件
* @param v 視圖
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.scan_devices) {
//此處進行點擊後的操作
}
}
爲了使這個點擊生效,所以要初始化控件。
private static int REQUEST_ENABLE_BLUETOOTH = 1;//請求碼
BluetoothAdapter bluetoothAdapter;//藍牙適配器
private TextView scanDevices;//掃描設備
private LinearLayout loadingLay;//加載佈局
private RecyclerView rv;//藍牙設備展示列表
private BluetoothReceiver bluetoothReceiver;//藍牙廣播接收器
private RxPermissions rxPermissions;//權限請求
DeviceAdapter mAdapter;//藍牙設備適配器
List<BluetoothDevice> list = new ArrayList<>();//數據來源
其中BluetoothReceiver這個會報紅,不用慌張,這是一個內部的廣播接收器,等下會創建的。
/**
* 初始化控件
*/
private void initView() {
loadingLay = findViewById(R.id.loading_lay);
scanDevices = findViewById(R.id.scan_devices);
rv = findViewById(R.id.rv);
scanDevices.setOnClickListener(this);
}
完成這個之後你的點擊事件纔會生效哦~
現在基本的控件都已經初始化了,這個時候我們需要對Android的版本進行判斷,看是否需要動態申請權限。我的手機是Android10.0,所以鐵定是要動態申請了,不過代碼上最好還是判斷一下。下面檢查版本
/**
* 檢查Android版本
*/
private void checkVersion() {
if (Build.VERSION.SDK_INT >= 23) {//6.0或6.0以上
permissionsRequest();//動態權限申請
} else {//6.0以下
initBlueTooth();//初始化藍牙配置
}
}
這裏面有兩個方法,一個是動態權限申請,一個是初始化藍牙配置,先來寫這個初始化藍牙配置吧。方法如下:
/**
* 初始化藍牙配置
*/
private void initBlueTooth() {
IntentFilter intentFilter = new IntentFilter();//創建一個IntentFilter對象
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);//獲得掃描結果
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//綁定狀態變化
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);//開始掃描
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);//掃描結束
bluetoothReceiver = new BluetoothReceiver();//實例化廣播接收器
registerReceiver(bluetoothReceiver, intentFilter);//註冊廣播接收器
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//獲取藍牙適配器
}
這個裏面的BluetoothReceiver依然會報紅,不過管它,等會再說,心急吃不了熱豆腐。
然後是動態權限申請的代碼
/**
* 動態權限申請
*/
private void permissionsRequest() {//使用這個框架使用了Lambda表達式,設置JDK版本爲 1.8或者更高
rxPermissions = new RxPermissions(this);//實例化這個權限請求框架,否則會報錯
rxPermissions.request(Manifest.permission.ACCESS_FINE_LOCATION)
.subscribe(granted -> {
if (granted) {//申請成功
initBlueTooth();//初始化藍牙配置
} else {//申請失敗
showMsg("權限未開啓");
}
});
}
這裏可以看到,我在權限申請成功之後進行藍牙初始化,失敗則給一個提示,這個地方是一個靜態的方法,其實就是彈出一個Toast,但是Android原生的代碼太長了,所以這裏我寫個方法來調用顯示,看起來會簡潔很多。方法如下:
/**
* 消息提示
*
* @param msg 消息內容
*/
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
OK,現在關於權限的問題就已經是解決了,接下來就該掃描了吧。讓我們回到onClick方法那裏,在這裏首先要獲取藍牙適配器,這一步我們再初始化藍牙配置的裏面就已經做好了,所以這裏只要判斷是否爲空就可以了。如果不爲空我再判斷藍牙是否打開,如果沒有打開,就要去打開,如果已經打開了就開始掃描,於是下面的代碼就這樣寫。
/**
* 控件點擊事件
* @param v 視圖
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.scan_devices) {
if (bluetoothAdapter != null) {//是否支持藍牙
if (bluetoothAdapter.isEnabled()) {//打開
//開始掃描周圍的藍牙設備,如果掃描到藍牙設備,通過廣播接收器發送廣播
bluetoothAdapter.startDiscovery();
} else {//未打開
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BLUETOOTH);
}
} else {
showMsg("你的設備不支持藍牙");
}
}
}
這個應該一目瞭然吧,不過打開藍牙是會有一個返回的,因爲我們用的是startActivityForResult,所以要在返回裏做確認。
/**
* 結果返回
*
* @param requestCode 請求碼
* @param resultCode 結果碼
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
if (resultCode == RESULT_OK) {
showMsg("藍牙打開成功");
} else {
showMsg("藍牙打開失敗");
}
}
}
寫代碼是講究這個邏輯的,所以很多東西不是隻看表面,細節也是很重要的。
通過上面的代碼,我們已經實現了點擊掃描時,如果藍牙已打開則掃描周邊藍牙設備,但是掃描的結果呢?這時你有沒有想到我們之前一直報紅的BluetoothReceiver呢?該它出馬了。
/**
* 廣播接收器
*/
private class BluetoothReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_FOUND://掃描到設備
showDevicesData(context, intent);//數據展示
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED://設備綁定狀態發生改變
mAdapter.changeBondDevice();//刷新適配器
break;
case BluetoothAdapter.ACTION_DISCOVERY_STARTED://開始掃描
loadingLay.setVisibility(View.VISIBLE);//顯示加載佈局
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED://掃描結束
loadingLay.setVisibility(View.GONE);//隱藏加載佈局
break;
}
}
}
這裏還是要做一下簡單的說明,我之前在初始化藍牙的時候加了四個過濾器,所以這裏就可以在接收的時候做處理了,從而實現相應的操作,還有一個就是這個廣播接收器是和onCreate方法平級的,所以只要是在MainActivity這個{}裏面,你想放哪就放哪。代碼裏面的註釋已經說明了一切,我們現在應該最關心的是這個數據展示的方法了吧!OK,下面看這個方法。
/**
* 顯示藍牙設備信息
*
* @param context 上下文參數
* @param intent 意圖
*/
private void showDevicesData(Context context, Intent intent) {
getBondedDevice();//獲取已綁定的設備
//獲取周圍藍牙設備
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (list.indexOf(device) == -1) {//防止重複添加
if (device.getName() != null) {//過濾掉設備名稱爲null的設備
list.add(device);
}
}
mAdapter = new DeviceAdapter(R.layout.item_device_list, list);
rv.setLayoutManager(new LinearLayoutManager(context));
rv.setAdapter(mAdapter);
mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//點擊時獲取狀態,如果已經配對過了就不需要在配對
if (list.get(position).getBondState() == BluetoothDevice.BOND_NONE) {
createOrRemoveBond(1, list.get(position));//開始匹配
} else {
showDialog("確定要取消配對嗎?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消配對
createOrRemoveBond(2, list.get(position));//取消匹配
}
});
}
}
});
}
這個時候你要是首先這一段代碼的話,你肯定會發現很多報紅,因爲你還沒有創建相應的方法的。首先來看getBondedDevice() 這個方法,用戶獲取已綁定的設備。
/**
* 獲取已綁定設備
*/
private void getBondedDevice() {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {//如果獲取的結果大於0,則開始逐個解析
for (BluetoothDevice device : pairedDevices) {
if (list.indexOf(device) == -1) {//防止重複添加
if (device.getName() != null) {//過濾掉設備名稱爲null的設備
list.add(device);
}
}
}
}
}
這個方法也比較簡單,相信我不解釋你也明白的。
然後是createOrRemoveBond 這個方法用於綁定或者解綁設備,裏面傳入兩個參數一個是類型,另一個是設備。方法代碼如下:
/**
* 創建或者取消匹配
*
* @param type 處理類型 1 匹配 2 取消匹配
* @param device 設備
*/
private void createOrRemoveBond(int type, BluetoothDevice device) {
Method method = null;
try {
switch (type) {
case 1://開始匹配
method = BluetoothDevice.class.getMethod("createBond");
method.invoke(device);
break;
case 2://取消匹配
method = BluetoothDevice.class.getMethod("removeBond");
method.invoke(device);
list.remove(device);//清除列表中已經取消了配對的設備
break;
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
這樣寫的好處就是看起來數據一些,雖然頁面上方法比較多,但是邏輯上是一環扣一環的,也沒有什麼解釋的必要了,內容一目瞭然。
最後來看showDialog 這個方法就是顯示一個彈窗,使用戶的操作沒有那麼突兀,方法如下。
/**
* 彈窗
* @param dialogTitle 標題
* @param onClickListener 按鈕的點擊事件
*/
private void showDialog(String dialogTitle, @NonNull DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(dialogTitle);
builder.setPositiveButton("確定", onClickListener);
builder.setNegativeButton("取消", null);
builder.create().show();
}
現在你再回頭看showDevicesData,這個方法裏面就不會再有報紅了。
然後再優化一下onClick
在onClick方法中加入:
if (mAdapter != null) {//當適配器不爲空時,這時就說明已經有數據了,所以清除列表數據,再進行掃描
list.clear();
mAdapter.notifyDataSetChanged();
}
然後在onCreate方法中調用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StatusBarUtil.StatusBarLightMode(this);//狀態欄黑色字體
initView();//初始化控件
checkVersion();//檢查版本
}
最後在onDestroy
/**
* 銷燬
*/
@Override
protected void onDestroy() {
super.onDestroy();
//卸載廣播接收器
unregisterReceiver(bluetoothReceiver);
}
至此,這個功能就寫完了,效果如下圖所示:
有什麼問題歡迎提出,當然你也可以給我發郵件[email protected]
我是初學者-Study,山高水長,後會有期~