Android

分类下相关文章

Android Service & Foreground Service

在调试一段 Android 蓝牙连接的代码时,不太明白为何建立蓝牙连接在 Foreground Service 中执行。 Service 与 Activity 的区别 Service 没有 UI 界面,类似一个后台服务 Activity 需要关联一个 UI 界面 Activity is a GUI and service is non-gui thread which can run in the background. service 可以在后台运行,适合执行耗时操作。 A Service is an application component that can perform l ...

阅读全文...

Android 前台服务配置问题导致蓝牙对讲功能异常

基于 STM32 WB 蓝牙模块的开源 SDK 实现蓝牙对讲功能时,总是无法建立连接。 而使用官方 APP ST BLE Sensor 或者我自己不用 SDK 实现的 App 都可以正常建立连接。 最终发现是前台服务,及 Android 12 的兼容性问题引起的。做了下修改就可以正常运行了。 连接异常 我的状态异常: Lost connection with the node BVL-WB1 而官方 APP 是在正常连接中显示: 连接到 对应的翻译文件: <string name="progressDialogConnTitle">Connecting. ...

阅读全文...

Android 子目录引起的 Cannot resolve symbol 'R'

今天在 Ctrl C / Ctrl V 代码时,发现一个再常见不过的 Resouce 引用代码报错。 出错代码: inflate(getContext(), R.layout.view_connection_status, this); 错误信息: Cannot resolve symbol 'R' R 还需要引入么? 测试了一下,发现确实,如果是在 sub package 中使用 R,需要 import root package 的 R。 package com.sunzhongwei.someapp.newpackage; import com.sunzhongwei.someap ...

阅读全文...

Android 12 扫描蓝牙设备 Need android.permission.BLUETOOTH_SCAN permission

报错信息 在 Android 12 系统上真机执行 BLE 蓝牙设备扫描时,APP 直接崩溃,Logcat 中报错: java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@9e60bb10: GattService registerScanner ... at com.st.BlueSTSDK.Manager.startBleScan_post21(Manager.java:283) at com.st.BlueS ...

阅读全文...

Android 项目中 compileSdkVersion targetSdkVersion minSdkVersion buildToolsVersion 的区别

之前用 Android Studio 开发小工具 APP 的时候,没有在意 compileSdkVersion targetSdkVersion minSdkVersion buildToolsVersion 这几个 gradle 配置参数,使用默认的值即可。但是最近遇到一个需要用到三方 SDK 的项目,发现默认配置有问题,需要了解一下这几个参数的区别。 compileSdkVersion compileSdkVersion 是编译 app 使用的 api 版本。这意味着: 开发阶段:使用了更高的 compileSdkVersion 版本,就可以使用最新的 API。也方便知道哪些 API 即 ...

阅读全文...

Android 项目如何从 github 引用三方模块

在引用 STM32 SDK 的时候,遇到了麻烦: 这个 SDK 模块在 github 上: https://github.com/STMicroelectronics/BlueSTSDK_Android 现有的方式 As a git submodule 1. Add the repository as a submodule: \$ git submodule add <https://github.com/STMicroelectronics/BlueSTSDK_Android.git> BlueSTSDK 2. Add the SDK as a project ...

阅读全文...

STM32 蓝牙模块对应的 Android 列表页源码梳理

BlueST SDK BlueST is a multi-platform library (Android and iOS supported) that permits easy access to the data exported by a Bluetooth Low Energy (BLE) device that implements the BlueST protocol. 相关名词 Manager:singleton class 用于启动/停止蓝牙设备扫描。发现节点之后,会通过 Manager.ManagerListener Class 异步通知。 Node:代表一个远端 ...

阅读全文...

Android API 无法获取蓝牙 BLE Attribute Handle 值

最近写了一个 Android APP 用于快速配置蓝牙网关(已上架 Google Play),但是基本功能完成后, 发现无法获取指定特性 (characteristic) / 描述符 (descriptor) 的 handle 值。 handle 存在的意义 Handle 的全称是 Attribute Handle。 在用 Android API 实现这个 BLE 扫描 APP 的过程中,我发现无论是服务,还是特性、描述符其 UUID 都是可能重复的。 举个例子,如果一个蓝牙设备包含两个电池模块,则存在两个相同服务的 UUID 是合理的。 那么用 UUID 来标识一个服务/特性/描述符就不合理 ...

阅读全文...

BLE Scan: Privacy policy

Welcome to the BLE Scan app for Android! This is an Android app developed by Zhongwei Sun. The app is available on Google Play. As an avid Android user myself, I take privacy very seriously. I know how irritating it is when apps collect your data without your knowledge. I hereby state, to the best o ...

阅读全文...

Material dialogs MaterialAlertDialogBuilder 中添加文本编辑框

setView 官方 Material Design 3 的文档中并没有详细的 MaterialAlertDialogBuilder 使用说明。 https://github.com/material-components/material-components-android/blob/master/docs/components/Dialog.md 找不到如何在里面添加文本编辑框。 但是在接口文档中可以看到有一个 setView 的方法 https://developer.android.com/reference/com/google/android/material/dialog/Ma ...

阅读全文...

Android 哪些操作应该放到 ViewModel 中

权限申请是否应该放到 ViewModel 中 有此疑问的原因是,我看到权限相关的操作,需要传入 Context 参数。 private fun isLocationPermissionGranted(): Boolean { return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED } 而 Context 只能在 Activity / Fragment / View 中得到。 ...

阅读全文...

Android RecyclerView Adapter ViewHolder 中获取 Activity

之前记录过如何在 Fragment 中获取父级 Activity,这次发现需要在 RecyclerView Adapter ViewHolder 中获取 Activity。 简单来说,就是: get Context from the view 例如,ViewHolder 中包含一个 text view 用来显示姓名,可以借此获取 context,从而得到 Activity. (binding.name.context as ItemDetailHostActivity).stopSomething() 实际应用场景 例如,在各种嵌套的 RecyclerView Adapter 中想访 ...

阅读全文...

Android 使用 LiveData 显示实时状态变化

例如加载数据时, 显示转圈提示。或者显示 connecting / finding data ... 也可以加上动态图标: https://github.com/material-components/material-components-android/blob/master/docs/components/ProgressIndicator.md 用最下面的转圈图标非常直观。 注意还有失败的情况,比如列表中蓝牙设备消失或关闭。 状态放在哪里 还是存储在 ViewModel 中最合适。 同时在 activity / fragment 中对状态 LiveData 进行监听。 LiveData ...

阅读全文...

Android RecyclerView 嵌套显示 BLE 蓝牙 Service 的 Characteristic 子项

例如,外层 RecyclerView 显示的是一个 BLE 蓝牙设备的 Service 列表; 内存嵌套的 RecyclerView 显示的各个 Service 所包含的 Characteristic 特性列表。 点击展开 Service,显示其所包含的特性。 RecyclerView 嵌套 首先在父级 RecyclerView 的 Item Layout 中定义一个子 RecyclerView 控件 嵌套的 RecyclerViewAdapter 绑定逻辑,在父级 RecyclerViewAdapter 的 onBindViewHolder 中实现 item:BluetoothGattC ...

阅读全文...

Android Kotlin 中获取 context 的几种方法

Fragment 中获取 context 调用 getActivity 获取父级 activity 的 context, Kotlin 中简化为 fragment.activity: 例如: Toast.makeText( activity, "Start Scanning", Toast.LENGTH_SHORT ).show() RecyclerViewAdapter 中获取 context 使用 layout 中 view (每个小控件就是一个 view) 的 getContext 方法, Kotlin 中即简化为 view.context: 例如: recyc ...

阅读全文...