Android 蓝牙配对Settings应用里面的简要流程记录

Android 蓝牙配对Settings应用里面的简要流程记录

文章目录

  • Android 蓝牙配对Settings应用里面的简要流程记录
    • 一、前言
    • 二、Settings蓝牙配对的关键代码
      • 1、接收蓝牙请求的地方 AndroidManifest.xml
      • 2、BluetoothPairingRequest
      • 3、BluetoothPairingService
      • 4、BluetoothPairingDialog
      • 5、BluetoothNameDialogFragment.java
      • 6、BluetoothPairingController
    • 三、其他
      • 1、Settings和TvSettings的配对界面
        • (1)TvSettings蓝牙配对对话框
        • (2)Settings蓝牙配对对话框
      • 2、自定义的应用界面监听和处理蓝牙配对广播
      • 3、Android 蓝牙相关广播介绍
      • 4、Android13 不能静态注册的几个广播
      • 5、Android13 蓝牙协议属性配置详解

一、前言

本文只是简单分析一下原生设置Settings中蓝牙配对的大致流程,具体细节有需要的自行研究。

另外我这里的开发平台是AML平台的,所以会有Settings和TvSettings,
其实这两个应用都会监听到蓝牙配请求,都会进行处理,这也是为啥会出现两次蓝牙配对弹框确认的情况。

如果想看看蓝牙配对流程或者蓝牙配对界面就行修改可以收藏看看。

二、Settings蓝牙配对的关键代码

Settings中蓝牙界面和蓝牙相关逻辑的代码,都是在:packages\apps\Settings\src\com\android\settings\bluetooth\ 目录

1、接收蓝牙请求的地方 AndroidManifest.xml

packages\apps\Settings\src\com\android\Settings\AndroidManifest.xml

<activity android:name=".bluetooth.BluetoothPairingDialog" //(1)这是一个Activityandroid:permission="android.permission.BLUETOOTH_PRIVILEGED"android:excludeFromRecents="true"android:windowSoftInputMode="stateVisible|adjustResize"android:theme="@style/Theme.AlertDialog"android:exported="true"android:taskAffinity=".bluetooth.BluetoothPairingDialog"><intent-filter android:priority="1">//(2)接收蓝牙请求<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /><category android:name="android.intent.category.DEFAULT" /></intent-filter>
</activity><receiver android:name=".bluetooth.BluetoothPairingRequest" //(3)这是一个静态广播接收者android:exported="true"><intent-filter>//(4)接收蓝牙请求<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /><action android:name="android.bluetooth.action.CSIS_SET_MEMBER_AVAILABLE"/></intent-filter>
</receiver>

2、BluetoothPairingRequest

Settings\src\com\android\settings\bluetooth\BluetoothPairingRequest .java

public final class BluetoothPairingRequest extends BroadcastReceiver {private static final String TAG = "BluetoothPairingRequest";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action == null) {return;}
。。。if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_CONSENT...) {device.setPairingConfirmation(true); //(1)直接确认配对的情况} else if (powerManager.isInteractive() && shouldShowDialog) {// Since the screen is on and the BT-related activity is in the foreground,// just open the dialog// convert broadcast intent into activity intent (same action string)Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent, BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND);//(2)拉起蓝牙配对对话框context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);} else {// (3)拉起 BluetoothPairingServiceintent.setClass(context, BluetoothPairingService.class);intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);context.startServiceAsUser(intent, UserHandle.CURRENT);...mBluetoothManager.getCachedDeviceManager().pairDeviceByCsip(device, groupId);}}
}

可以看到这个静态的广播接收者,主要功能大概有:

某些条件下直接确认配对设备某些条件下拉起蓝牙配对确认对话框某些条件下拉起蓝牙配对服务

AndroidManifest.xml 已经监听配对会拉起蓝牙配对对话框,这里再拉起会冲突吗?

其实不会,因为这里会有判断对话框是否已经拉起。

3、BluetoothPairingService

Settings\src\com\android\settings\bluetooth\BluetoothPairingService.java

//取消配对
mDevice.cancelBondProcess();

这个主要是启动蓝牙服务;

BluetoothPairingService主要是监听配对是否取消和配对过程异常等情况,具体逻辑就不分析了。

4、BluetoothPairingDialog

Settings\src\com\android\settings\bluetooth\BluetoothPairingDialog.java

这里主要是拉起显示配对的对话框和随时监听配对情况

public class BluetoothPairingDialog extends FragmentActivity {private final BroadcastReceiver mReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();//如果已经绑定了 或者取消配对都会关闭对话框界面if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR);if (bondState == BluetoothDevice.BOND_BONDED ||bondState == BluetoothDevice.BOND_NONE) {dismiss();}} else if (BluetoothDevice.ACTION_PAIRING_CANCEL.equals(action)) {BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);if (device == null || mBluetoothPairingController.deviceEquals(device)) {dismiss();}}}};@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);BluetoothPairingDialogFragment bluetoothFragment = ...;//正常情况下会拉起蓝牙配对对话框if (!fragmentFound) {bluetoothFragment.show(getSupportFragmentManager(), FRAGMENT_TAG);}}}

从上面代码看,BluetoothPairingDialog主要作用是拉起对话框界面BluetoothPairingDialogFragment;

如果要修改蓝牙对话对话框的界面和相关信息,不是修改 BluetoothPairingDialog 的代码,

而是要修改BluetoothNameDialogFragment 的代码。

5、BluetoothNameDialogFragment.java

packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothNameDialogFragment.java

该界面主要是根据情况显示配对对话框的内容

public class BluetoothPairingDialogFragment extends InstrumentedDialogFragment {//里面的代码基本都是对话框界面和相关逻辑的控制
...//点击取消和确认配对情况的回调@Overridepublic void onClick(DialogInterface dialog, int which) {if (which == DialogInterface.BUTTON_POSITIVE) {mPositiveClicked = true;mPairingController.onDialogPositiveClick(this);} else if (which == DialogInterface.BUTTON_NEGATIVE) {mPairingController.onDialogNegativeClick(this);}mPairingDialogActivity.dismiss();}}

6、BluetoothPairingController

Settings\src\com\android\settings\bluetooth\BluetoothPairingController.java

这个类就相当于一个工具类,执行具体的逻辑。

public class BluetoothPairingController implements OnCheckedChangeListener,BluetoothPairingDialogListener {//确认配对后的操作private void onPair(String passkey) {Log.d(TAG, "Pairing dialog accepted");switch (mType) {case BluetoothDevice.PAIRING_VARIANT_PIN:case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS:mDevice.setPin(passkey);break;case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:case BluetoothDevice.PAIRING_VARIANT_CONSENT:mDevice.setPairingConfirmation(true); //确认配对关键break;case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:case BluetoothDevice.PAIRING_VARIANT_PASSKEY:// Do nothing.break;default:Log.e(TAG, "Incorrect pairing type received");}}//取消配对或者关闭配对对话框的操作public void onCancel() {Log.d(TAG, "Pairing dialog canceled");mDevice.cancelBondProcess(); //取消配对关键}
}

三、其他

1、Settings和TvSettings的配对界面

dumpsys window 查看相关界面信息:

//(1)查看Settings 的配对界面显示情况
console:/ # dumpsys window| grep mFocmFocusedApp=ActivityRecord{e4d15f7 u0 com.android.settings/.bluetooth.BluetoothPairingDialog t30}mFocusedWindow=Window{5509839 u0 com.android.settings/com.android.settings.bluetooth.BluetoothPairingDialog}
console:/ # 
console:/ #//(2)查看TvSettings 的配对界面显示情况
console:/ # dumpsys window| grep mFocmFocusedApp=ActivityRecord{f08fb5d u0 com.android.tv.settings/.accessories.BluetoothPairingDialog t31}mFocusedWindow=Window{931897e u0 com.android.tv.settings/com.android.tv.settings.accessories.BluetoothPairingDialog}console:/ #

从dumpsys window 的窗口信息可以看到:

1、Settings的应用包名是:com.android.settings
2、TvSettings的应用包名是:com.android.tv.settings
3、Settings拉起配对对话框的类是:com.android.settings.bluetooth.BluetoothPairingDialog
4、TvSettings拉起配对对话框的类是:com.android.tv.settings.accessories.BluetoothPairingDialog

Settings和TvSettings的代码都在packages/apps/目录下,

原生Settings的具体逻辑是比TvSettings处理更详细一些,有兴趣的可以自己看看。

2、TvSettings蓝牙配对对话框和原生Settings配对对话框

(1)TvSettings蓝牙配对对话框

在这里插入图片描述

(2)Settings蓝牙配对对话框

在这里插入图片描述

(3)隐藏"通讯录访问"选框

有些方案是商显或者平板方案,可能需要取消这个现实,修改的地方;

Settings\src\com\android\settings\bluetooth\BluetoothPairingDialogFragment.java

//	contactSharing.setVisibility(mPairingController.isContactSharingVisible() ? View.VISIBLE : View.GONE);//隐藏"访问通讯录和通话记录"选项contactSharing.setVisibility(View.GONE);

可能有多种对话框会显示,全局搜索 “contactSharing.setVisibility”进行修改就可以了。

修改后的样式:

在这里插入图片描述

隐藏"通讯录选项"还可以修改:BluetoothPairingController的isContactSharingVisible()方法逻辑。

3、如果要去除TvSettings 的蓝牙配对监听

package\apps\TvSettings\Settings\AndroidManifest.xml

删除或者注释掉下面这段代码就OK了:

        <receiverandroid:name=".accessories.BluetoothPairingRequest"android:exported="true"><intent-filter><action android:name="android.bluetooth.device.action.PAIRING_REQUEST"/></intent-filter></receiver><activityandroid:name=".accessories.BluetoothPairingDialog"android:configChanges="keyboard|keyboardHidden|navigation"android:excludeFromRecents="true"android:exported="true"android:permission="android.permission.BLUETOOTH_PRIVILEGED"android:taskAffinity=""><intent-filter><action android:name="android.bluetooth.device.action.PAIRING_REQUEST"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>

2、自定义的应用界面监听和处理蓝牙配对广播

主要代码如下:

//监听蓝牙配对广播
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST); //;蓝牙配对广播
context.registerReceiver(mBluetoothReceiver, mIntentFilter);class BluetoothReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context content, Intent intent) {String action = intent.getAction();LogUtil.debug("onReceive action = " + action);if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {BluetoothDevice device =  intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//设置蓝牙配对device.setPairingConfirmation(true);abortBroadcast();//关闭广播传递,防止原生设置监听到配对}}      

这里是监听到蓝牙配对后,后台直接确认配对,不用点击系统Settings的配对对话框就会确认配对。

并且这里进行了 abortBroadcast ,其他应用就不会收到蓝牙配对广播。

动态监听的方式肯定是比静态静态的方式更快收到广播。

自定义代码中也可以自己写对话框确认是否配对和取消配对。

3、Android 蓝牙相关广播介绍

蓝牙相关广播都是在 BluetoothDevice.java 和 BluetoothAdapter.java 中进行了定义。蓝牙相关广播主要包括:蓝牙开关,蓝牙连接,蓝牙状态改变,蓝牙配对等等等等。//Android13 中的源码地址:packages\modules\Bluetooth\framework\java\android\bluetooth\BluetoothDevice.javapackages\modules\Bluetooth\framework\java\android\bluetooth\BluetoothAdapter.java

https://blog.csdn.net/wenzhi20102321/article/details/134956116

4、Android13 不能静态注册的几个广播

android.intent.action.SCREEN_ON //屏幕亮起android.intent.action.SCREEN_OFF//屏幕亮起android.intent.action.BATTERY_CHANGED //电池电量改变android.intent.action.CONFIGURATION_CHANGED //配置改变,界面语言,设备方向等配置信息android.intent.action.TIME_TICK //每分钟回调一次

具体内容:

https://blog.csdn.net/wenzhi20102321/article/details/134956090

5、Android13 蓝牙协议属性配置详解

https://blog.csdn.net/wenzhi20102321/article/details/139703045

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1452726.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

分布式微服务: springboot底层机制实现

springboot底层机制实现 搭建SpringBoot底层机制开发环境ConfigurationBean会发生什么,并分析机制提出问题: SpringBoot 是怎么启动Tomcat, 并可以支持访问Controller源码分析: SpringApplication.run()SpringBoot的debug流程 实现SpringBoot底层机制[Tomcat启动分析 Spring容…

支付宝 沙盒demo使用

简介&#xff1a;支付宝沙箱环境是一个为开发者提供的模拟测试环境&#xff0c;用于在应用上线前进行接口功能开发和联调。在这个环境中&#xff0c;开发者可以模拟开放接口&#xff0c;进行开发调试工作&#xff0c;以确保应用上线后能顺利运行。 1. 配置沙盒 1. 1 沙箱控制…

System-Verilog 实现DE2-115流水灯

文章目录 一、 SystemVerilog1. SystemVerilog简介2. 基本语法和特性 二、实验过程hello.v文件引脚分配 三、实验效果参考 一、 SystemVerilog 1. SystemVerilog简介 SystemVerilog是一种高级的硬件描述语言&#xff08;HDL&#xff09;&#xff0c;它不仅继承了Verilog语言的…

Apple Intelligence 横空出世!它的独家秘诀在哪里?

在 WWDC 2024 大会上&#xff0c;苹果公司揭晓了自家的生成式 AI 项目——Apple Intelligence&#xff0c;其策略核心在于采用 ⌈ 更为聚焦的小型模型 ⌋ &#xff0c;而非盲目追求大模型的普遍趋势。横空出世的它究竟有什么过人之处&#xff1f;一文带你探究竟&#xff01;生成…

基于微信小程序的童书购买系统的设计与实现

基于微信小程序的童书购买系统的设计与实现 摘 要 《“十三五”规划》第一次把“保障妇女、未成年人、残疾人的基本权利”作为重要内容&#xff0c;充分体现了党和国家对广大人民群众的关心&#xff0c;为广大人民群众营造了良好的学习氛围&#xff0c;并出台多项文件及政策&…

个人网站制作 Part 26 添加在线日历功能 | Web开发项目添加页面缓存

文章目录 &#x1f469;‍&#x1f4bb; 基础Web开发练手项目系列&#xff1a;个人网站制作&#x1f680; 添加在线日历功能&#x1f528;使用日历服务&#x1f527;步骤 1: 选择日历服务&#x1f527;步骤 2: 安装FullCalendar&#x1f527;步骤 3: 创建FullCalendar组件&…

DeepDriving | CUDA编程-05:流和事件

本文来源公众号“DeepDriving”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;CUDA编程-05&#xff1a;流和事件 1 CUDA流 在CUDA中有两个级别的并发&#xff1a;内核级并发和网格级并发。前面的文章DeepDriving | CUDA编程-04&…

jdk17详细安装步骤

本文以Windows系统&#xff0c;JDK17版本作为示例&#xff0c;其他版本的操作步骤类似。 一、下载 进入官网后往下翻&#xff0c;找到JAVA17&#xff0c;然后点击Windows 点击下载。 二、安装 安装 JDK的安装是无脑安装&#xff0c;就是一路下一步下一步。。直到完成。默认安…

Docker部署MaxKB 知识库(提高问答命中率)

前言 上一篇文章简单的介绍了下MaxKB&#xff0c;这一篇文章就讲如何部署MaxKB。 MaxKB实现逻辑也比较简单&#xff0c;如下图。 安装 修改Docker镜像源 由于不可抗力&#xff0c;部分源已经无法使用&#xff0c;需要修改以下的源地址来拉取镜像。如果是linux&#xff0c;…

VMware软件的安装与安装Win10系统

上一篇写了&#xff08;虚拟机&#xff09;VMware软件的安装及Ubuntu系统安装&#xff0c;这次续上部分&#xff0c;安装完Ubuntu系统后&#xff0c;又安装了win10&#xff0c;也记录一下。 事前准备好win10镜像文件&#xff0c;可在微软官网下载 入口地址&#xff1a;软件下…

Codesys V3.5 下载安装教程+ 所有版本大全集合

文章目录 一、codesys V3.5 所有的版本集合二、 视频安装教程三、 安装包下载地址四、详细的安装步骤 一、codesys V3.5 所有的版本集合 codesys 版本更新很快&#xff0c;这是小编整理的所有codesys版本&#xff0c;都放在百度网盘上了&#xff0c;在文章最后可以获取下载链接…

MyBatis使用Demo

文章目录 01、Mybatis 意义02、Mybatis 快速入门04、Mapper 代理开发05、Mybatis 配置文件07、查询所有&结果映射08、查询-查看详情09、查询-条件查询10、查询-动态条件查询多条件动态查询单条件动态查询 11、添加&修改功能添加功能修改功能 12、删除功能删除一个批量删…

远程桌面失败:你的凭据不工作

远程桌面失败&#xff1a;你的凭据不工作 远程桌面失败&#xff1a;你的凭据不工作_您的凭据不工作-CSDN博客https://blog.csdn.net/weixin_38004638/article/details/82290796

Python合并文件(dat、mdf、mf4)

天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物。 每个人都有惰性&#xff0c;但不断学习是好好生活的根本&#xff0c;共勉&#xff01; 文章均为学习整理笔记&#xff0c;分享记录为主&#xff0c;如有错误请指正&#xff0c;共同学习进步。…

鸿蒙轻内核M核源码分析系列二一 03 文件系统LittleFS

2.2 文件信息数组操作 函数LfsAllocFd()设置文件信息数组元素信息。参数fileName为文件路径信息&#xff0c;传出参数fd为文件描述符即数组索引。遍历文件信息数组&#xff0c;遍历到第一个未使用的元素标记其为已使用状态&#xff0c;设置文件路径信息&#xff0c;把数组索引…

C++ 45 之 赋值运算符的重载

#include <iostream> #include <string> #include <cstring> using namespace std;class Students05{ public:int m_age;char* m_name;Students05(){}Students05(const char* name,int age){// 申请堆空间保存m_name;this->m_name new char[strlen(name)…

宝藏速成秘籍(7)堆排序法

一、前言 1.1、概念 堆排序&#xff08;Heapsort&#xff09;是指利用堆这种数据结构所设计的一种排序算法 。堆是一个近似 完全二叉树 的结构&#xff0c;并同时满足堆积的性质&#xff1a;即子结点的键值或索引总是小于&#xff08;或者大于&#xff09;它的父节点。 1.2、排…

本地靶场搭建

1、windows service2003 链接&#xff1a;https://pan.baidu.com/s/1RIealrcfcDWKu1AIuYFbAQ?pwd4bv8 提取码&#xff1a;4bv8 2、asp环境搭建&#xff08;虚拟机内&#xff09; ①asp工作原理&#xff1a; 客户发送网站请求&#xff0c;iis接收客户请求&#xff0c;解析…

[图解]《分析模式》漫谈04-Martin Fowler叫的是哪家的士

1 00:00:01,230 --> 00:00:04,190 今天我们来探讨一个有趣的话题 2 00:00:05,130 --> 00:00:08,350 Martin Fowler&#xff0c;他叫的是哪一家的的士 3 00:00:11,980 --> 00:00:15,240 第2章这里&#xff0c;Martin Fowler写 4 00:00:15,250 --> 00:00:18,550 他…

【redis的基本数据类型】

基本数据类型 Redis的基本数据类型有五种&#xff0c;分别是 StringListHashSetSortedSet 这些基本的数据类型构成了其他数据类型的基石&#xff0c;而这些基本数据类型又对应着不同的底层实现&#xff0c;不同的底层实现往往是针对不同的使用场景做的特殊的优化&#xff0c;…