Bluedroid 打开蓝牙流程
1、bluetooth.cc enable执行打开蓝牙
static int enable(bool start_restricted) {
LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
restricted_mode = start_restricted;
if (!interface_ready()) return BT_STATUS_NOT_READY;
//异步启动协议栈
stack_manager_get_interface()->start_up_stack_async();
return BT_STATUS_SUCCESS;
}
stack_manager线程中加入启动协议栈的事件event_start_up_stack
static void start_up_stack_async(void) {
thread_post(management_thread, event_start_up_stack, NULL);
}
2、启动协议栈
static void event_start_up_stack(UNUSED_ATTR void* context) {
if (stack_is_running) {
LOG_INFO(LOG_TAG, "%s stack already brought up", __func__);
return;
}
ensure_stack_is_initialized();
LOG_INFO(LOG_TAG, "%s is bringing up the stack", __func__);
future_t* local_hack_future = future_new();
hack_future = local_hack_future;
// Include this for now to put btif config into a shutdown-able state
module_start_up(get_module(BTIF_CONFIG_MODULE));
//enable协议栈
bte_main_enable();
if (future_await(local_hack_future) != FUTURE_SUCCESS) {
LOG_ERROR(LOG_TAG, "%s failed to start up the stack", __func__);
stack_is_running = true; // So stack shutdown actually happens
event_shut_down_stack(NULL);
return;
}
stack_is_running = true;
LOG_INFO(LOG_TAG, "%s finished", __func__);
//通知协议栈启动消息
btif_thread_post(event_signal_stack_up, NULL);
}
2.1 bte_main_enable
创建bte task
void bte_main_enable() {
APPL_TRACE_DEBUG("%s", __func__);
module_start_up(get_module(BTSNOOP_MODULE));
module_start_up(get_module(HCI_MODULE));
BTU_StartUp();
}
2.2 BTU_StartUp-初始化BTU 控制
void BTU_StartUp(void) {
btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
//创建bt_workqueue线程
bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
if (bt_workqueue_thread == NULL) goto error_exit;
thread_set_rt_priority(bt_workqueue_thread, BTU_TASK_RT_PRIORITY);
// Continue startup on bt workqueue thread.
thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
return;
error_exit:;
LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue",
__func__);
BTU_ShutDown();
}
2.3 btu_task_start_up启动btu
void btu_task_start_up(UNUSED_ATTR void* context) {
LOG(INFO) << "Bluetooth chip preload is complete";
/* Initialize the mandatory core stack control blocks
(BTU, BTM, L2CAP, and SDP)
*/
btu_init_core();
/* Initialize any optional stack components */
BTE_InitStack();
bta_sys_init();
/* Initialise platform trace levels at this point as BTE_InitStack() and
* bta_sys_init()
* reset the control blocks and preset the trace level with
* XXX_INITIAL_TRACE_LEVEL
*/
module_init(get_module(BTE_LOGMSG_MODULE));
//创建btu_message_loop线程,用于处理btu消息
message_loop_thread_ = thread_new("btu message loop");
if (!message_loop_thread_) {
LOG(FATAL) << __func__ << " unable to create btu message loop thread.";
}
thread_set_rt_priority(message_loop_thread_, THREAD_RT_PRIORITY);
//btu_message_loop_run 添加到btu message线程的工作队列
thread_post(message_loop_thread_, btu_message_loop_run, nullptr);
}
2.4 btu_message_loop_run
创建消息队列发送消息btif_init_ok
void btu_message_loop_run(UNUSED_ATTR void* context) {
message_loop_ = new base::MessageLoop();
run_loop_ = new base::RunLoop();
// Inform the bt jni thread initialization is ok.
message_loop_->task_runner()->PostTask(
FROM_HERE, base::Bind(base::IgnoreResult(&btif_transfer_context),
btif_init_ok, 0, nullptr, 0, nullptr));
run_loop_->Run();
delete message_loop_;
message_loop_ = NULL;
delete run_loop_;
run_loop_ = NULL;
}
void btif_init_ok(UNUSED_ATTR uint16_t event, UNUSED_ATTR char* p_param) {
BTIF_TRACE_DEBUG("btif_task: received trigger stack init event");
btif_dm_load_ble_local_keys();
BTA_EnableBluetooth(bte_dm_evt);
}
tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK* p_cback) {
/* Bluetooth disabling is in progress */
if (bta_dm_cb.disabling) return BTA_FAILURE;
bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
/* if UUID list is not provided as static data */
bta_sys_eir_register(bta_dm_eir_update_uuid);
bta_sys_cust_eir_register(bta_dm_eir_update_cust_uuid);
do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_enable, p_cback));
return BTA_SUCCESS;
}
2.5 bta_dm_enable-初始化蓝牙设备管理
void bta_dm_enable(tBTA_DM_SEC_CBACK* p_sec_cback) {
/* if already in use, return an error */
if (bta_dm_cb.is_bta_dm_active) {
tBTA_DM_SEC enable_event;
APPL_TRACE_WARNING("%s Device already started by another application",
__func__);
memset(&enable_event, 0, sizeof(tBTA_DM_SEC));
enable_event.enable.status = BTA_FAILURE;
if (p_sec_cback != NULL) p_sec_cback(BTA_DM_ENABLE_EVT, &enable_event);
return;
}
/* first, register our callback to SYS HW manager */
bta_sys_hw_register(BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback);
/* make sure security callback is saved - if no callback, do not erase the
previous one,
it could be an error recovery mechanism */
if (p_sec_cback != NULL) bta_dm_cb.p_sec_cback = p_sec_cback;
/* notify BTA DM is now active */
bta_dm_cb.is_bta_dm_active = true;
/* send a message to BTA SYS */
tBTA_SYS_HW_MSG* sys_enable_event =
(tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT;
sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH;
bta_sys_sendmsg(sys_enable_event);
}
Bluedroid 打开蓝牙流程相关推荐
- Android 打开蓝牙流程
以下是基于Android 4.2代码,对Bluetooth BR/EDR Enable process的分析.BluetoothAdapter类代表的是local device Bluetooth a ...
- android -- 蓝牙 bluetooth (二) 打开蓝牙
4.2的蓝牙打开流程这一部分还是有些变化的,从界面上看蓝牙开关就是设置settings里那个switch开关,widget开关当然也可以,起点不同而已,后续的流程是一样的.先来看systemServe ...
- Android 蓝牙开关打开enable流程--framework层---全网最详细
同学,别退出呀,我可是全网最牛逼的 Android 蓝牙分析博主,我写了上百篇蓝牙文章,请点击下面了解本专栏,进入本博主主页看看再走呗,一定不会让你后悔的,记得一定要去看主页置顶文章哦. 本文详细 ...
- Android BLE蓝牙4.0开发 实现扫描、连接、通讯、获取通知、特性等 (一、打开蓝牙 进行扫描)
目录 首先说下蓝牙4.0 目前android蓝牙有2种: ble蓝牙开发流程: 1.首先当然是进行权限申请啦: 2.获取蓝牙适配器 BluetoothAdapter 3.打开蓝牙 4.关闭蓝牙 5.在 ...
- 解决:设置中打开蓝牙,測试机不会自己主动搜索设备
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huangyabin001/article/details/36027575 [操作步骤]:设置中打开 ...
- Android 蓝牙开发,申请打开蓝牙
申请打开蓝牙 <!-- 蓝牙权限 --> <uses-permission android:name="android.permission.BLUETOOTH" ...
- android系统app打开蓝牙+设置可见性
BluetoothAdapter blue = BluetoothAdapter.getDefaultAdapter(); 这两个方法足矣满足你的需求(需要system权限): blue.enable ...
- Mac 命令行关闭和打开蓝牙
首先确保已安装brew,一个包管理工具. 命令行: 接着使用brew安装blueutil这个控制蓝牙开关的命令,就可以通过终端来关闭和打开蓝牙了. $ brew install blueutil$ b ...
- 打开蓝牙来允许”xxx”连接到配件
一直以来,在使用别人的app时,会经常看到一个"打开蓝牙来允许"xxx"连接到配件"的弹出框(如下图), 点击设置按钮,就可以跳转到ios系统的setting界 ...
最新文章
- 废旧光盘手工小制作_废旧光盘没有用,只能扔?这些光盘改造的小手工就很惊艳,附教程...
- 云数据库·ApsaraDB 产品6月刊
- arm-hisiv100nptl-linux-gcc编译boa和移植
- ASP.NET MVC实用技术:开篇
- WIN10安装VS2013出现兼容性问题解决
- linux apache gzip压缩,Linux入门教程:配置Apache开启gzip压缩传输,gzip压缩
LoadModul
- php网页背景音乐代码,HTML5背景音乐播放器代码
- linux 卸载keepalived,centos7下keepalived1.4.0安装启动卸载
- HIT 软件构造 软件维护 设计原则
- Nginx解决history模式下页面刷新404
- 微信隐藏的功能和技巧
- 《Delta-Sigma数据转换器从入门到精通》笔记之漫谈均值
- Sketch Less for More: On-the-Fly Fine-Grained Sketch Based Image Retrieval (CVPR 2020 Oral)
- 帆软报表配置入门(一丶普通报表和决策报表 )
- 斜线表头html怎么做,Excel三栏斜线表头的完美制作方法
- 大学物理复习笔记——恒定磁场
- [开源名人录] Fabrice Bellard
- 如何看待百度公司搜索引擎的竞价排名?
- 吃烧烤之命令模式学习笔记[C++版] 1
- php版葫芦侠签到,葫芦侠三楼一键签到工具