前言
??上一篇中了解了特性和屬性,同時顯示設(shè)備藍(lán)牙服務(wù)下的特性和屬性,本文中就需要來使用這些特性和屬性來完成一些功能。
目錄
- Ble藍(lán)牙App(一)掃描
- Ble藍(lán)牙App(二)連接與發(fā)現(xiàn)服務(wù)
- Ble藍(lán)牙App(三)特性和屬性
- Ble藍(lán)牙App(四)UI優(yōu)化和描述符
- Ble藍(lán)牙App(五)數(shù)據(jù)操作
正文
??在進(jìn)行具體的功能開發(fā)之前,對于UI的優(yōu)化也是必不可少的,就拿主頁面來說,目前是比較奇怪的,如果你是第一次使用這個App的話,可能就會很疑惑,要怎么使用?基于這個點我們增加一個布局,優(yōu)化以下主頁面的提示。
一、UI優(yōu)化
① 主頁面提示優(yōu)化
??首先在layout下新建一個lay_empty_ble.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:gravity="center"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_empty_ble" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="24dp"
android:text="暫無藍(lán)牙設(shè)備,請點擊頁面左上角藍(lán)牙圖標(biāo)按鈕,進(jìn)入掃描頁面,選擇設(shè)備連接后進(jìn)行操作"
android:textColor="@color/gray"
android:textSize="16sp" />
</LinearLayout>
這里使用到了一個圖標(biāo),ic_empty_ble.xml
,代碼如下所示:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:fillColor="#e6e6e6"
android:pathData="M997.9,516.6a488.4,488.4 0,0 1,-489.5 488.2,488.4 488.4,0 0,1 -489.5,-490.8C19.9,242.3 240.1,23.8 511,25.8a488.4,488.4 0,0 1,486.9 490.8zM534.7,726l99.2,-74.9 -99.2,-78.7v153.7zM633.9,379.4l-98.9,-73.2v151.8L633.9,379.4zM561.3,515.2l130.3,-103.6c29.2,-23.3 29.8,-42.8 0.8,-64.7 -54.2,-40.9 -108.3,-81.8 -163.8,-120.9 -10.2,-7.3 -29.4,-12.1 -39.1,-7.3 -9.6,4.9 -17.1,23.1 -17.5,35.7 -1.8,54.8 -0.8,109.7 -0.9,164.6 0,6.7 -1,13.3 -1.9,23.9L339.3,341l-37.4,46.4 160.3,127.8c-40.6,32.3 -79.9,62.5 -118,94.1 -45.2,37.4 -52.4,22.8 -6.9,77.9 0.9,1 2.9,1.1 4.4,1.6l129.5,-101.2c0,69 -0.9,131.3 0.9,193.4 0.3,11.8 8.7,27.9 18.4,33.6 7.8,4.6 25.6,0.3 34.7,-6.2a7693.4,7693.4 0,0 0,167.1 -124.8c29.3,-22.3 28.8,-41.2 -0.7,-64.8 -42.6,-34.2 -85.4,-68.1 -130.2,-103.7z" />
</vector>
然后在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">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/orange"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="@drawable/ic_scan_ble"
app:title="GoodBle"
app:titleCentered="true"
app:titleTextColor="@color/white">
<TextView
android:id="@+id/tv_disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:text="斷開連接"
android:textColor="@color/white"
android:visibility="gone" />
</com.google.android.material.appbar.MaterialToolbar>
<TextView
android:id="@+id/tv_device_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<include
android:id="@+id/empty_ble_lay"
layout="@layout/lay_empty_ble"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_service"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_device_info" />
</androidx.constraintlayout.widget.ConstraintLayout>
預(yù)覽的效果如下圖所示:
??服務(wù)列表在沒有數(shù)據(jù)的時候就是空白的,那么下面我們修改MainActivity中的代碼,因為默認(rèn)是顯示的,所以可以在連接藍(lán)牙設(shè)備發(fā)現(xiàn)服務(wù)之后就隱藏這個布局,修改如下圖所示:
然后就是在斷開連接的回調(diào)時,顯示空內(nèi)容布局,再清空服務(wù)列表,刷新適配器,修改代碼如下所示:
override fun onConnectionStateChange(state: Boolean) {
runOnUiThread {
if (state) binding.tvDisconnect.visibility = View.VISIBLE
else {
binding.emptyBleLay.root.visibility = View.VISIBLE
mServiceList.clear()
mServiceAdapter?.notifyDataSetChanged()
}
}
}
下面運行一下:
② UUID顯示優(yōu)化
??這樣看起來主頁面在沒有設(shè)備信息的時候不會顯得單調(diào),那么還有一個小細(xì)節(jié)就是,當(dāng)設(shè)備的藍(lán)牙服務(wù)和特性不屬于SIG定義的,是廠商自定義時,我們最好就顯示完成的UUID,為了方便使用,在ble包下新建一個BleConstant類,代碼如下所示:
object BleConstant {
const val APP_NAME = "GoodBle"
const val UNKNOWN_DEVICE = "Unknown device"
const val UNKNOWN_SERVICE = "Unknown Service"
const val UNKNOWN_CHARACTERISTICS = "Unknown Characteristics"
const val UNKNOWN_DESCRIPTOR = "Unknown Descriptor"
const val BROADCAST = "Broadcast"
const val READ = "Read"
const val WRITE_NO_RESPONSE = "Write No Response"
const val WRITE = "Write"
const val NOTIFY = "Notify"
const val INDICATE = "Indicate"
const val AUTHENTICATED_SIGNED_WRITES = "Authenticated Signed Writes"
const val EXTENDED_PROPERTIES = "Extended Properties"
}
這里定義了一些常量,包括未知服務(wù)、未知特性,和一些其他的屬性,這樣做在修改的時候修改一個常量就可以了。下面我們分別修改一下BleUtils中的getServiceName()
和getCharacteristicsName()
函數(shù)的else的值為常量中的常量名,剩下的就可以在服務(wù)適配器和特性適配器中去修改了,首先是服務(wù)適配器,修改
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val serviceName = BleUtils.getServiceName(services[position].uuid)
holder.binding.tvServiceName.text = serviceName
holder.binding.tvServiceUuid.text = if (serviceName != UNKNOWN_SERVICE) BleUtils.getShortUUID(services[position].uuid) else services[position].uuid.toString()
...
}
services[position].uuid.toString()
就是服務(wù)完整的uuid,默認(rèn)是小寫的,你也可以改成大寫。
那么同樣特性適配器也改一下:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val characteristicName = BleUtils.getCharacteristicsName(characteristics[position].uuid)
holder.binding.tvCharacterName.text = characteristicName
holder.binding.tvCharacterUuid.text = if (characteristicName != UNKNOWN_CHARACTERISTICS) BleUtils.getShortUUID(characteristics[position].uuid) else characteristics[position].uuid.toString()
...
}
再運行一下,對于未知設(shè)備服務(wù)和特性的UUID就會顯示完成的值。
③ 設(shè)備信息優(yōu)化
??當(dāng)前連接設(shè)備之后我們可以看到設(shè)備的服務(wù)信息,但是卻不知道是那個設(shè)備,沒有一個地方顯示設(shè)備的信息,最重要的當(dāng)然就是設(shè)備的名稱和Mac地址了,這里我們可以優(yōu)化一下,首先修改一下activity_main.xml
中的MaterialToolbar
控件的內(nèi)容,因為我想把設(shè)備名稱作為標(biāo)題,設(shè)備Mac地址作為子標(biāo)題,所以增加兩個子標(biāo)題的屬性值:
<com.google.android.material.appbar.MaterialToolbar
...
app:subtitleCentered="true"
app:subtitleTextColor="@color/white">
這里就分別設(shè)置子標(biāo)題的顯示位置和文字顏色,下面要做的就是獲取設(shè)備,在BleCore中增加一個函數(shù),代碼如下所示:
fun getDevice() = mGatt?.device
然后回到MainActivity中,只需要修改連接狀態(tài)的回調(diào)函數(shù)中的代碼即可,如下所示:
@SuppressLint("MissingPermission")
override fun onConnectionStateChange(state: Boolean) {
runOnUiThread {
if (state) {
binding.tvDisconnect.visibility = View.VISIBLE
bleCore.getDevice()?.apply {
binding.toolbar.title = name ?: UNKNOWN_DEVICE
binding.toolbar.subtitle = address
}
}
else {
binding.toolbar.title = APP_NAME
binding.toolbar.subtitle = ""
binding.tvDeviceInfo.text = ""
binding.emptyBleLay.root.visibility = View.VISIBLE
mServiceList.clear()
mServiceAdapter?.notifyDataSetChanged()
}
}
}
下面運行一下看看:
二、描述
??在上一篇中提到了特性和屬性,特性有那些功能是屬性決定的,那么描述又是做什么的呢?
① 概念
在藍(lán)牙低功耗(BLE)中,Descriptor(描述符)是用于提供有關(guān)特征值的額外信息的數(shù)據(jù)結(jié)構(gòu)。Descriptor 提供了特定特征的更詳細(xì)描述和配置選項。Descriptor 是特征(Characteristics)的子項,用于描述特征的特定屬性或行為。每個特征可以有一個或多個 Descriptor。
以下是一些常見的 BLE Descriptor 類型及其含義:
-
聲明 Descriptor:這個 Descriptor 用于描述特征的聲明信息,包括特征的唯一標(biāo)識符、權(quán)限、值的格式和其他標(biāo)志。它提供了特征的基本信息供其他設(shè)備了解。
-
用戶描述(User Description)Descriptor:用于提供特征的人類可讀描述信息。這個描述可以是特征的名稱、標(biāo)簽或其他有關(guān)特征的說明性文字。
-
配置 Descriptor:用于描述特征的配置選項。這個 Descriptor 可以包含特征的可選設(shè)置,例如采樣率、測量單位或閾值等。
-
通知 Descriptor:用于配置特征是否支持通知功能。這個 Descriptor 可以用于使設(shè)備可以接收特征值變化的通知。
-
線性區(qū)間 Descriptor:用于描述特征值的線性關(guān)系,例如數(shù)值范圍和步長等。
-
客戶端配置 Descriptor:用于允許遠(yuǎn)程設(shè)備(例如中心設(shè)備)訂閱特征值的變化通知,這個很重要。
這些只是一些常見的 BLE Descriptor 類型和其含義的示例,實際上可以根據(jù)應(yīng)用需求定義自定義的 Descriptor。Descriptor 提供了對特征更詳細(xì)的描述和配置,它們可以通過藍(lán)牙協(xié)議進(jìn)行傳輸和訪問。在 BLE 應(yīng)用中,Descriptor 充當(dāng)了配置和元數(shù)據(jù)信息的重要角色,幫助設(shè)備之間準(zhǔn)確地交換和理解數(shù)據(jù)。
那么現(xiàn)在你已經(jīng)了解了描述符的作用了,而我們目前的特性下還沒有描述符的,注意不是每一個特性都有描述符,下面我們就來把描述符寫出來了。首先我們在item_characteristic.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="wrap_content"
android:foreground="?attr/selectableItemBackground"
android:paddingStart="16dp">
<TextView
android:id="@+id/tv_character_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="特性"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_uuid_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UUID:"
app:layout_constraintStart_toStartOf="@+id/tv_character_name"
app:layout_constraintTop_toBottomOf="@+id/tv_character_name" />
<TextView
android:id="@+id/tv_character_uuid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UUID"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="@+id/tv_uuid_title"
app:layout_constraintStart_toEndOf="@+id/tv_uuid_title"
app:layout_constraintTop_toTopOf="@+id/tv_uuid_title" />
<TextView
android:id="@+id/tv_property_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Properties:"
app:layout_constraintStart_toStartOf="@+id/tv_character_name"
app:layout_constraintTop_toBottomOf="@+id/tv_uuid_title" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_property"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/tv_property_title"
app:layout_constraintStart_toEndOf="@+id/tv_property_title"
app:layout_constraintTop_toTopOf="@+id/tv_property_title" />
<LinearLayout
android:id="@+id/lay_descriptors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_property_title"
tools:layout_editor_absoluteX="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Descriptors:"
android:textColor="@color/black"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_descriptor"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
通過預(yù)覽圖得知,實際上就是這一塊內(nèi)容。
下面我們就可以正式去寫描述符的適配器了。
② 描述適配器
??首先在layout下增加一個item_descriptor.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_descriptor_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="描述"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_uuid_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UUID:"
app:layout_constraintStart_toStartOf="@+id/tv_descriptor_name"
app:layout_constraintTop_toBottomOf="@+id/tv_descriptor_name" />
<TextView
android:id="@+id/tv_descriptor_uuid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UUID"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="@+id/tv_uuid_title"
app:layout_constraintStart_toEndOf="@+id/tv_uuid_title"
app:layout_constraintTop_toTopOf="@+id/tv_uuid_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
然后關(guān)于描述符的名稱,我們可以在BleUtils中寫一個函數(shù),代碼如下所示:
fun getDescriptorName(uuid: UUID) =
when ("0x${uuid.toString().substring(4, 8).uppercase(Locale.getDefault())}") {
"0x2900" -> "Characteristic Extended Properties"
"0x2901" -> "Characteristic User Description"
"0x2902" -> "Client Characteristic Configuration"
"0x2903" -> "Server Characteristic Configuration"
"0x2904" -> "Characteristic Presentation Format"
"0x2905" -> "Characteristic Aggregate Format"
"0x2906" -> "Valid Range"
"0x2907" -> "External Report Reference"
"0x2908" -> "Report Reference"
"0x2909" -> "Number of Digitals"
"0x290A" -> "Value Trigger Setting"
"0x290B" -> "Environmental Sensing Configuration"
"0x290C" -> "Environmental Sensing Measurement"
"0x290D" -> "Environmental Sensing Trigger Setting"
"0x290E" -> "Time Trigger Setting"
"0x290F" -> "Complete BR-EDR Transport Block Data"
"0x2910" -> "Observation Schedule"
"0x2911" -> "Valid Range and Accuracy"
else -> BleConstant.UNKNOWN_DESCRIPTOR
}
下面我們寫描述符適配器,在adapter包下新建一個DescriptorAdapter
類,代碼如下所示:
class DescriptorAdapter(
private val descriptors: List<BluetoothGattDescriptor>
) : RecyclerView.Adapter<DescriptorAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ItemDescriptorBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val descriptorName = BleUtils.getDescriptorName(descriptors[position].uuid)
holder.binding.tvDescriptorName.text = descriptorName
holder.binding.tvDescriptorUuid.text = if (descriptorName != BleConstant.UNKNOWN_DESCRIPTOR) BleUtils.getShortUUID(descriptors[position].uuid) else descriptors[position].uuid.toString()
}
override fun getItemCount() = descriptors.size
class ViewHolder(itemView: ItemDescriptorBinding) : RecyclerView.ViewHolder(itemView.root) {
var binding: ItemDescriptorBinding
init {
binding = itemView
}
}
}
這里的代碼同樣對于自定義UUID展示完整數(shù)據(jù),對于SIG的展示短UUID。
③ 顯示描述符
接下來就是在特性適配器中去加載顯示描述符數(shù)據(jù),修改CharacteristicAdapter中的onBindViewHolder()函數(shù),在里面增加如下所示代碼:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
...
//加載特性下的描述
if (characteristics[position].descriptors.isEmpty()) {
holder.binding.layDescriptors.visibility = View.GONE
return
}
holder.binding.rvDescriptor.apply {
layoutManager = LinearLayoutManager(context)
adapter = DescriptorAdapter(characteristics[position].descriptors)
}
}
這里的isEmpty()
函數(shù)很重要,因為不是每一個特性都有描述符,這個前面已經(jīng)說過了,沒有的我們就直接隱藏對應(yīng)的描述符布局,否則就加載描述符數(shù)據(jù),下面運行看一下。
通過這個圖就可以清晰的的看到特性下的描述符,本文就到這里了。
三、源碼
如果對你有所幫助的話,不妨 Star 或 Fork,山高水長,后會有期~文章來源:http://www.zghlxwxcb.cn/news/detail-648656.html
源碼地址:GoodBle文章來源地址http://www.zghlxwxcb.cn/news/detail-648656.html
到了這里,關(guān)于Android Ble藍(lán)牙App(四)UI優(yōu)化和描述符的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!