Android进阶之路Android开发

Android12.0如何开发一个蓝牙,简单一文教你实战!

2022-05-31  本文已影响0人  码农的地中海

在我的申请下,公司终于购买了一台基于Android12.0的手机,然后我就开心的拿去安装测试了,发现程序崩溃了,于是我这里就写下来,Android12.0的蓝牙适配方法。

微信图片_20220531133124.gif

/ Android版本中蓝牙简介 /

在Android系统版本中,蓝牙的变化有,但是不多,这里简要说明一下。

/ 新建项目 /

在Android12.0中新增加了三个运行时权限,我们依次来说明一下,这里我们依然创建一个项目来说明,新建一个Android12Bluetooth项目,如下图所示:

这里使用Kotlin来写,点击Finish。

配置settings.gradle和build.gradle

然后来配置一下项目的依赖库,首先是在工程的settings.gradle中增加如下依赖:

    maven { url "https://jitpack.io" }

增加位置如下图所示:

微信图片_20220531133233.png

然后在app的build.gradle中增加。

    buildFeatures {        viewBinding true        dataBinding true    }    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'

增加位置如下图所示:


微信图片_20220531133240.png

然后Sync Now。

配置AndroidManifest.xml

下面配置AndroidMainfest.xml,权限如下所示:

    <uses-permission android:name="android.permission.BLUETOOTH"/>    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>    <!--Android12 的蓝牙权限 如果您的应用与已配对的蓝牙设备通信或者获取当前手机蓝牙是否打开-->    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>    <!--Android12 的蓝牙权限 如果您的应用查找蓝牙设备(如蓝牙低功耗 (BLE) 外围设备)-->    <uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>    <!--Android12 的蓝牙权限 如果您的应用使当前设备可被其他蓝牙设备检测到-->    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>

增加位置如下图所示:


微信图片_20220531133246.png

/ 打开蓝牙 /

下面我们构建一下activity_main.xml中的代码:

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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"    tools:context=".MainActivity">    <Button        android:id="@+id/btn_open_bluetooth"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:insetTop="0dp"        android:insetBottom="0dp"        android:text="打开蓝牙"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

这里就是写一个按钮,用来点击打开系统蓝牙的开关的。在Android12.0之前打开蓝牙的之前需要先判断蓝牙是否打开,我们可以这样来写,在MainActivity中增加如下代码:

    private fun isOpenBluetooth(): Boolean {        val manager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager        val adapter = manager.adapter ?: return false        return adapter.isEnabled    }

同样我们还需要一个方法判断当前是否为Android12及以上版本。

    private fun isAndroid12() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S

同样还有一个检查此权限是否授予的方法和一个显示Toast的方法:

    private fun hasPermission(permission: String) =        checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED    private fun showMsg(msg: String) {        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()    }

打开蓝牙意图

在MainActivity中新增如下代码:

    //打开蓝牙意图    val enableBluetooth = registerForActivityResult(StartActivityForResult()) {        if (it.resultCode == Activity.RESULT_OK) {            showMsg(if (isOpenBluetooth()) "蓝牙已打开" else "蓝牙未打开")        }    }

此方法就替代了之前startActivityForResult,现在我们使用registerForActivityResult。再返回中可以得知当前是否打开了蓝牙。

请求BLUETOOTH_CONNECT权限意图

registerForActivityResult不光能用于页面获取值,也能用于请求权限。

    //请求BLUETOOTH_CONNECT权限意图    val requestBluetoothConnect = registerForActivityResult(ActivityResultContracts.RequestPermission()) {            if (it) {                //打开蓝牙                enableBluetooth.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))            } else {                showMsg("Android12中未获取此权限,则无法打开蓝牙。")            }        }

请求权限返回无非就是同意不同意,如果同意了我们就调用

enableBluetooth.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))

去打开系统蓝牙,不同意就提示一下。下面再用ViewBinding来配置一下:


微信图片_20220531133308.png

这里有一个initView的函数,在这个函数中我们对按钮的点击事件进行操作,新增initView()函数,代码如下:

    private fun initView() {        binding.btnOpenBluetooth.setOnClickListener {            //蓝牙是否已打开            if (isOpenBluetooth()){                showMsg("蓝牙已打开")                return@setOnClickListener            }            //是Android12            if (isAndroid12()) {                //检查是否有BLUETOOTH_CONNECT权限                if (hasPermission(Manifest.permission.BLUETOOTH_CONNECT)) {                    //打开蓝牙                    enableBluetooth.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))                } else {                    //请求权限                    requestBluetoothConnect.launch(Manifest.permission.BLUETOOTH_CONNECT)                }                return@setOnClickListener            }            //不是Android12 直接打开蓝牙            enableBluetooth.launch(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE))        }    }

这里的代码就比较好理解,首先判断蓝牙是否已经打开了,打开了就不往下执行,没打开,再判断当前是否为Android12,不是就直接打开系统蓝牙,是Android12,再去检查是否授予BLUETOOTH_CONNECT权限,授予了就打开系统蓝牙,没有授予就去请求此权限,下面我们运行一下:

微信图片_20220531133326.gif

/ 蓝牙扫描 /

在Android6.0 - Android11.0之间,扫描蓝牙都是需要打开定位权限的,而在Android12中则不需要了,换成了BLUETOOTH_SCAN权限,那么我们下面来看看,怎么操作的。

这里扫描蓝牙就以低功耗蓝牙为例子。

扫描者

在MainActivity中定义如下变量:

    private val TAG = MainActivity::class.java.simpleName    //获取系统蓝牙适配器    private lateinit var mBluetoothAdapter: BluetoothAdapter    //扫描者    private lateinit var scanner: BluetoothLeScanner    //是否正在扫描    var isScanning = false

获取系统蓝牙适配器,要在onCreate回调中,如下图所示:


微信图片_20220531133340.png

扫描回调

扫描设备时会有扫描的结果,在MainActivity中增加如下代码:

    //扫描结果回调    private val scanCallback = object : ScanCallback() {        override fun onScanResult(callbackType: Int, result: ScanResult) {            val device = result.device            Log.d(TAG, "name: ${device.name}, address: ${device.address}")        }    }

这里可能你的device.name下面会有一个红线,这是因为AS会检查你这里需要一个BLUETOOTH_CONNECT权限,而这个权限我们在打开蓝牙时已经请求过了,那么为了避免麻烦,我们在当前MainActivity上面增加如下注解。

@SuppressLint("MissingPermission")

如下图所示:


微信图片_20220531133351.png

这个注解加上去之后你需要小心蓝牙权限的问题。

扫描方法

下面我们写一个开始扫描和停止扫描的方法,代码如下:

    private fun startScan() {        if (!isScanning) {            scanner.startScan(scanCallback)            isScanning = true            binding.btnScanBluetooth.text = "停止扫描"        }    }    private fun stopScan() {        if (isScanning) {            scanner.stopScan(scanCallback)            isScanning = false            binding.btnScanBluetooth.text = "扫描蓝牙"        }    }

扫描和停止扫描时修改一下变量值并且改动按钮的文字以表示当前是否正在扫描中。

执行扫描

执行扫描就很简单了,首先我们需要在MainActivity中创建扫描意图:

    //请求BLUETOOTH_SCAN权限意图    private val requestBluetoothScan =        registerForActivityResult(ActivityResultContracts.RequestPermission()) {            if (it) {                //进行扫描                startScan()            } else {                showMsg("Android12中未获取此权限,则无法扫描蓝牙。")            }        }

这个意图我们将在点击扫描按钮的时候会用到,下面我们在initView中增加扫描按钮点击的代码:

        //扫描蓝牙        binding.btnScanBluetooth.setOnClickListener {            if (isAndroid12()) {                if (hasPermission(Manifest.permission.BLUETOOTH_SCAN)) {                    //扫描或者停止扫描                    if (isScanning) stopScan() else startScan()                } else {                    //请求权限                    requestBluetoothScan.launch(Manifest.permission.BLUETOOTH_SCAN)                }            }        }

相对来说还是比较的简洁,其实还可以更简洁。我在扫描回调中打印了日志,如果有扫描到设备的话,就会有日志,下面我们扫描一下看看:

微信图片_20220531133404.png

扫描启动了,但是没有设备被扫描到,可我附近明明有蓝牙设备正在广播,这是为什么呢?

应用不推导物理位置

这个说起来就和之前的Android 6.0 至 Android 11.0中需要定位权限才能扫描有关系了,就是因为这个推导物理位置,手机是可以通过扫描到的设备知道设备的具体位置的,所以之前需要定位权限,那么现在我没有定位权限了,你扫不到设备就很离谱,怎么解决呢?

如果您的应用不推导物理位置,那么您可以坚定地断言您的应用绝不会使用蓝牙权限来推导物理位置。为此,请完成以下步骤:

将 android:usesPermissionFlags 属性添加到 BLUETOOTH_SCAN 权限声明,并将此属性的值设为 neverForLocation。

注意:如果 android:usesPermissionFlags 中包含 neverForLocation,则会从扫描结果中过滤出某些 BLE 信标。

这是官方的说明,操作起来很简单,如下图所示:

微信图片_20220531133438.png

意思很明显,就是说你如果不需要推导物理地址,那么就设置一下这个权限的标识即可。下面我们再来运行一下:

微信图片_20220531133419.png

设备就扫描到了,可以看到这里有设备的Mac地址,再点一下就可以停止扫描了。

不过我们这里是控制台显示了设备,并没有在页面显示设备,下面我们完成这一步。

/ 页面显示扫描设备 /

显示蓝牙设备首先我们需要修改一下activity_main.xml布局,代码如下:

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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"    tools:context=".MainActivity">    <Button        android:id="@+id/btn_open_bluetooth"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:insetTop="0dp"        android:insetBottom="0dp"        android:text="打开蓝牙"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <Button        android:id="@+id/btn_scan_bluetooth"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_marginStart="10dp"        android:layout_marginTop="20dp"        android:layout_marginEnd="10dp"        android:insetTop="0dp"        android:insetBottom="0dp"        android:text="扫描蓝牙"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/btn_open_bluetooth" />    <androidx.recyclerview.widget.RecyclerView        android:id="@+id/rv_device"        android:layout_width="0dp"        android:layout_height="0dp"        android:layout_marginTop="20dp"        android:overScrollMode="never"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/btn_scan_bluetooth" /></androidx.constraintlayout.widget.ConstraintLayout>

蓝牙设备适配器

这个里的适配器使我们自己去写的,需要显示数据的,首先我们需要创建一个蓝牙图标,在drawable包下新建一个icon_bluetooth.xml,里面的代码如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><vector xmlns:android="http://schemas.android.com/apk/res/android"    android:width="48dp"    android:height="48dp"    android:autoMirrored="true"    android:tint="#000000"    android:viewportWidth="24.0"    android:viewportHeight="24.0">    <path        android:fillColor="@android:color/white"        android:pathData="M14.24,12.01l2.32,2.32c0.28,-0.72 0.44,-1.51 0.44,-2.33 0,-0.82 -0.16,-1.59 -0.43,-2.31l-2.33,2.32zM19.53,6.71l-1.26,1.26c0.63,1.21 0.98,2.57 0.98,4.02s-0.36,2.82 -0.98,4.02l1.2,1.2c0.97,-1.54 1.54,-3.36 1.54,-5.31 -0.01,-1.89 -0.55,-3.67 -1.48,-5.19zM15.71,7.71L10,2L9,2v7.59L4.41,5 3,6.41 8.59,12 3,17.59 4.41,19 9,14.41L9,22h1l5.71,-5.71 -4.3,-4.29 4.3,-4.29zM11,5.83l1.88,1.88L11,9.59L11,5.83zM12.88,16.29L11,18.17v-3.76l1.88,1.88z" /></vector>

因为我们的设备需要显示信号强度,那么我们创建一个数据类,在com.llw.bluetooth包下新建一个MyDevice类,代码如下:

data class MyDevice(val device: BluetoothDevice, var rssi: Int)

然后我们构建适配器的item布局,在layout包下新建一个item_device.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?><layout 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">    <data>        <variable            name="device"            type="com.llw.bluetooth.MyDevice" />    </data>    <androidx.constraintlayout.widget.ConstraintLayout        android:layout_width="match_parent"        android:layout_height="70dp">        <ImageView            android:id="@+id/imageView"            android:layout_width="50dp"            android:layout_height="50dp"            android:layout_marginStart="10dp"            android:layout_marginTop="10dp"            android:padding="8dp"            app:layout_constraintStart_toStartOf="parent"            app:layout_constraintTop_toTopOf="parent"            app:srcCompat="@drawable/icon_bluetooth" />        <TextView            android:id="@+id/tv_name"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginStart="10dp"            android:layout_marginTop="4dp"            android:layout_marginEnd="10dp"            android:text="@{device.device.name ?? `Unknown` }"            android:textColor="@color/black"            android:textSize="14sp"            android:textStyle="bold"            app:layout_constraintEnd_toStartOf="@+id/tv_rssi"            app:layout_constraintStart_toEndOf="@+id/imageView"            app:layout_constraintTop_toTopOf="@+id/imageView" />        <TextView            android:id="@+id/tv_address"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_marginStart="10dp"            android:layout_marginEnd="10dp"            android:layout_marginBottom="4dp"            android:text="@{device.device.address}"            android:textSize="12sp"            app:layout_constraintBottom_toBottomOf="@+id/imageView"            app:layout_constraintEnd_toStartOf="@+id/tv_rssi"            app:layout_constraintStart_toEndOf="@+id/imageView" />        <TextView            android:id="@+id/tv_rssi"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginEnd="10dp"            android:text="@{device.rssi+`dBm`}"            app:layout_constraintBottom_toBottomOf="parent"            app:layout_constraintEnd_toEndOf="parent"            app:layout_constraintTop_toTopOf="parent" />    </androidx.constraintlayout.widget.ConstraintLayout></layout>

这里的布局数据采用了DataBinding。下面我们去写适配器,在com.llw.bluetooth包新建一个MyDeviceAdapter类,里面的代码如下:

class MyDeviceAdapter(data: MutableList<MyDevice>) :    BaseQuickAdapter<MyDevice, BaseDataBindingHolder<ItemDeviceBinding>>(R.layout.item_device, data) {    override fun convert(holder: BaseDataBindingHolder<ItemDeviceBinding>, item: MyDevice) {        holder.dataBinding?.apply {            device = item            executePendingBindings()        }    }}

这里就用到之前build.gradle中第三方依赖库的适配器,相当好用。下面我们回到MainActivity中。

显示列表设备

在MainActivity中创建两个变量:

    //设备列表    private val deviceList = mutableListOf<MyDevice>()    //适配器    private lateinit var myDeviceAdapter: MyDeviceAdapter

这里我们需要思考一个问题,那就是列表设备的唯一性,因为蓝牙设备是一直广播的,所以我们扫描到的结果会有重复的设备,重复的设备有信号强度上的差异,这个地方我们要做的就是判断当前列表中是否有此设备,有就更新rssi,没有就添加,我们新增一个findDeviceIndex()函数,代码如下:

    private fun findDeviceIndex(scanDevice: MyDevice, deviceList: List<MyDevice>): Int {        var index = 0        for (device in deviceList) {            if (scanDevice.device.address.equals(device.device.address)) return index            index += 1        }        return -1    }

下面我们再新增一个addDeviceList()函数,代码如下:

    private fun addDeviceList(device: MyDevice) {        val index = findDeviceIndex(device, deviceList)        if (index == -1) {            Log.d(TAG, "name: ${device.device.name}, address: ${device.device.address}")            deviceList.add(device)            myDeviceAdapter.notifyDataSetChanged()        } else {            deviceList[index].rssi = device.rssi            myDeviceAdapter.notifyItemChanged(index)        }    }

最后我们在扫描回调中调用此方法:

微信图片_20220531133438.png

最后别忘记了我们的适配器和列表都需要初始化的,我写在initView()函数中,如下图所示:

微信图片_20220531133441.png

现在就可以运行了。

微信图片_20220531133446.gif

其实Android12蓝牙只有权限上要注意一下,虽然有三个动态权限,但是只要你同意了一个就都同意了,因为它们属于同一个权限组,所以如果你能确保当前拥有其中一个权限的话,蓝牙的操作就和之前一样的。不过还是有一些问题的,那就是在打开蓝牙之后要对变量进行赋值,如下图所示:

微信图片_20220531133452.png

/ 适配Android12以下设备 /

当前的代码我们在Android12上是没有问题了,但是Android12一下Android6.0以上还是扫描不到设备,因为需要定位权限,那么我们在AndroidManifest.xml中增加:

        <!--Android6-11 定位权限-->    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

然后我们回到MainActivity中,增加一个定位意图。

    //请求定位权限意图    private val requestLocation =        registerForActivityResult(ActivityResultContracts.RequestPermission()) {            if (it) {                //扫描蓝牙                startScan()            } else {                showMsg("Android12以下,6及以上需要定位权限才能扫描设备")            }        }

然后我们回到扫描按钮的点击事件。


微信图片_20220531133501.png

下面我们在Android10.0上运行一下:


微信图片_20220531133507.gif

Github源码:

https://github.com/lilongweidev/Android12Bluetooth
更多技术干货及源码:Android核心技术进阶

上一篇下一篇

猜你喜欢

热点阅读