公司最近给我丢了一个蓝牙开发的项目,不了解怎么办呢,那固然是从最基础的开始了,因此这里至关于作笔记了。css
打开蓝牙
java
扫描蓝牙设备
react
话很少说,建立一个项目才是首要的任务,建立一个名为MyBluetooth的Android项目。android
在工程的build.gradle中,添加git
maven { url "https://jitpack.io" }
以下图所示github
而后是在app下的build.gradle中添加依赖库web
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文件app
<!--蓝牙链接权限--> <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文件
maven
图片资源
在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); }
至此,这个功能就写完了,效果以下图所示:
有什么问题欢迎提出,固然你也能够给我发邮件lonelyholiday@qq.com 我是初学者-Study,山高水长,后会有期~