Android 低功耗蓝牙开发 (扫描过滤、自定义服务与特性)Kotlin版(1)

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerVertical=“true”

android:text=“过滤设备名为空的设备”

android:textColor=“@color/black” />

<androidx.appcompat.widget.SwitchCompat

android:id=“@+id/switch_device_name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentEnd=“true”

android:theme=“@style/SwitchStyle”

tools:ignore=“UseSwitchCompatOrMaterialXml” />

<View

android:layout_width=“match_parent”

android:layout_height=“0.5dp”

android:background=“@color/line” />

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“60dp”

android:gravity=“center_vertical”

android:orientation=“horizontal”

android:padding=“16dp”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“RSSI:”

android:textColor=“@color/black” />

<androidx.appcompat.widget.AppCompatSeekBar

android:id=“@+id/sb_rssi”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_marginStart=“12dp”

android:layout_marginEnd=“12dp”

android:layout_weight=“1”

android:max=“100”

android:min=“40”

android:progress=“100”

android:theme=“@style/SeekBarStyle” />

<TextView

android:id=“@+id/tv_rssi”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“-100 dBm”

android:textColor=“@color/black” />

<View

android:layout_width=“match_parent”

android:layout_height=“0.5dp”

android:background=“@color/line” />

<TextView

android:id=“@+id/tv_close”

android:layout_width=“match_parent”

android:layout_height=“60dp”

android:foreground=“?attr/selectableItemBackground”

android:gravity=“center”

android:text=“关闭”

android:textColor=“@color/black” />

在styles.xml中增加如下样式:

布局有了,下面进入MainActivity中写代码。增加一个方法:

/**

  • 显示扫描过滤弹窗

*/

@SuppressLint(“InflateParams”)

private fun showScanFilterDialog() = BottomSheetDialog(this, R.style.BottomSheetDialogStyle).apply {

setContentView(DialogScanFilterBinding.bind(View.inflate(context, R.layout.dialog_scan_filter, null)).apply {

switchDeviceName.setOnCheckedChangeListener { buttonView, isChecked -> }

sbRssi.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {

override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {}

override fun onStartTrackingTouch(seekBar: SeekBar) {}

override fun onStopTrackingTouch(seekBar: SeekBar) {}

})

tvClose.setOnClickListener { dismiss() }

}.root)

window?.findViewById(R.id.design_bottom_sheet)?.setBackgroundColor(Color.TRANSPARENT)

}.show()

这里面有一个开关一个滑动条,开关用于确定过滤设备名称为null的设备。首先来看这个开关,这里应该要保存开关的状态,每次每一次设置也是比较麻烦的,因此可以用一个本地缓存记录下来,通过也可以记录滑动条的位置,保存信号值强度。

④ 过滤设置缓存

写缓存会用到什么?上下文参数,那么就可以弄一个全局的上下文,怎么弄?自定义Application,在com.llw.blekotlin包下新建一个BleApplication类,代码如下:

open class BleApplication : Application() {

companion object {

@SuppressLint(“StaticFieldLeak”)

lateinit var context: Context

}

override fun onCreate() {

super.onCreate()

context = applicationContext

}

}

很简单的代码,目前来说只有一个上下文,然后我们在AndroidManifest.xml中的application标签中去配置一下。

  • 29
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值