Android Bluetooth杂记
本文仅供个人记录一些日常笔记,仅供参考!
一、打开蓝牙LOG
将/etc/bluetooth/bt_stack.conf中的打印级别改成5,然后使用 logcat -v time 抓取相关 log,下面是bt_stack.conf的内容:
# Enable BtSnoop logging function
# valid value : true, false
BtSnoopLogOutput=false # btsnoop开关,如果要抓hci log改成true# BtSnoop log output file
BtSnoopFileName=/sdcard/btsnoop_hci.log # btsnoop默认文件名及路径# Preserve existing BtSnoop log before overwriting
BtSnoopSaveLog=false #保存上一次btsnoop log# Enable trace level reconfiguration function
# Must be present before any TRC_ trace level settings
TraceConf=true # log级别重新配置开关# Trace level configuration# 各个模块log级别配置, 级别为5时打印所有debug消息,级别 0 不输出
# BT_TRACE_LEVEL_NONE 0 ( No trace messages to be generated )
# BT_TRACE_LEVEL_ERROR 1 ( Error condition trace messages )
# BT_TRACE_LEVEL_WARNING 2 ( Warning condition trace messages )
# BT_TRACE_LEVEL_API 3 ( API traces )
# BT_TRACE_LEVEL_EVENT 4 ( Debug messages for events )
# BT_TRACE_LEVEL_DEBUG 5 ( Full debug messages )
# BT_TRACE_LEVEL_VERBOSE 6 ( Verbose messages ) - Currently supported for TRC_BTAPP only.
TRC_BTM=2
TRC_HCI=2
TRC_L2CAP=2
TRC_RFCOMM=2
TRC_OBEX=2
TRC_AVCT=2
TRC_AVDT=2
TRC_AVRC=2
TRC_AVDT_SCB=2
TRC_AVDT_CCB=2
TRC_A2D=2
TRC_SDP=2
TRC_GATT=2
TRC_SMP=2
TRC_BTAPP=2
TRC_BTIF=2
二、蓝牙调试技巧
调试过程中经常需要在蓝牙协议栈中添加打印,可以重新编译生成bluetooth.default.so, 然后push到机器里面。
1) 修改协议栈源码
2) 编译bluetooth.default.so
- 进入android根目录
- source build/envsetup.sh
- lunch 56 选择对应的板子(userdebug模式) , 或 lunch 11 选择对应的板子(userdebug模式)
- 进入对应的目录执行mm: ~/work/CubieBoard7/android/external/bluetooth/bluedroid$ mm
so生成路径: Install: out/target/product/s700_cb7/system/lib/hw/bluetooth.default.so
3) push bluetooth.default.so
- $ adb root
- $ adb remount
- $ adb push bluetooth.default.so /system/lib/hw/
三、重要的函数和接口
1. 广播包上报
// system\bt\stack\btm\btm_ble_gap.cc
void btm_ble_process_adv_pkt(uint8_t data_len, uint8_t* data)
1) 如果是active scan是否有收到scan response:
bool is_active_scan =btm_cb.ble_ctr_cb.inq_var.scan_type == BTM_BLE_SCAN_MODE_ACTI;if (is_active_scan && is_scannable && !is_scan_resp) {// If we didn't receive scan response yet, don't report the device.DVLOG(1) << " Waiting for scan response " << bda;return;}
2) 发现模式是否匹配:
/*** Check ADV flag to make sure device is discoverable and match the search* condition*/
static uint8_t btm_ble_is_discoverable(const RawAddress& bda,std::vector<uint8_t> const& adv_data) {uint8_t scan_state = BTM_BLE_NOT_SCANNING;/* for observer, always "discoverable */if (btm_cb.ble_ctr_cb.is_ble_observe_active())scan_state |= BTM_BLE_OBS_RESULT;if (!adv_data.empty()) {uint8_t flag = 0;uint8_t data_len;const uint8_t* p_flag = AdvertiseDataParser::GetFieldByType(adv_data, BTM_BLE_AD_TYPE_FLAG, &data_len);if (p_flag != NULL && data_len != 0) {flag = *p_flag;if ((btm_cb.btm_inq_vars.inq_active & BTM_BLE_GENERAL_INQUIRY) &&(flag & (BTM_BLE_LIMIT_DISC_FLAG | BTM_BLE_GEN_DISC_FLAG)) != 0) {scan_state |= BTM_BLE_INQ_RESULT;}}}return scan_state;
}
3) btm_ble_process_adv_pkt最后是adv上报,扫描回调分settings扫描和apk扫描,分别对应p_inq_results_cb和p_obs_results_cb回调:
tBTM_INQ_RESULTS_CB* p_inq_results_cb = p_inq->p_inq_results_cb;if (p_inq_results_cb && (result & BTM_BLE_INQ_RESULT)) {(p_inq_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,const_cast<uint8_t*>(adv_data.data()), adv_data.size());}tBTM_INQ_RESULTS_CB* p_obs_results_cb = btm_cb.ble_ctr_cb.p_obs_results_cb;if (p_obs_results_cb && (result & BTM_BLE_OBS_RESULT)) {(p_obs_results_cb)((tBTM_INQ_RESULTS*)&p_i->inq_info.results,const_cast<uint8_t*>(adv_data.data()), adv_data.size());}
1)settings 扫描回调:p_inq_results_cb扫描回调指向bta_dm_inq_results_cb,在BTM_StartInquiry时被设置。
2)apk扫描回调:p_obs_results_cb扫描回调指向bta_dm_observe_results_cb,在BTM_BleObserve时被设置。
btm_ble_process_adv_pkt_cont详细的调用流程参考Android Bluetooth蓝牙scan过程: 三、Adv和Scan Resp接收
2. notify接收
//system\bt\stack\gatt\gatt_main.cc
void gatt_init(void) /* First, register fixed L2CAP channel for ATT over BLE */fixed_reg.pL2CA_FixedConn_Cb = gatt_le_connect_cback;fixed_reg.pL2CA_FixedData_Cb = gatt_le_data_ind;fixed_reg.pL2CA_FixedCong_Cb = gatt_le_cong_cback; /* congestion callback */L2CA_RegisterFixedChannel(L2CAP_ATT_CID, &fixed_reg);
// gatt_le_data_ind
static void gatt_le_data_ind(uint16_t chan, const RawAddress& bd_addr,BT_HDR* p_buf) {gatt_data_process(*p_tcb, L2CAP_ATT_CID, p_buf);gatt_client_handle_server_rsp(tcb, cid, op_code, msg_len, p);
// system\bt\stack\gatt\gatt_cl.cc
/** This function is called to handle the server response to client */
void gatt_client_handle_server_rsp(tGATT_TCB& tcb, uint16_t cid,uint8_t op_code, uint16_t len,uint8_t* p_data) {VLOG(1) << __func__ << " opcode: " << loghex(op_code);uint16_t payload_size = gatt_tcb_get_payload_size_rx(tcb, cid);if (op_code == GATT_HANDLE_VALUE_IND || op_code == GATT_HANDLE_VALUE_NOTIF ||op_code == GATT_HANDLE_MULTI_VALUE_NOTIF) {if (len >= payload_size) {LOG(ERROR) << StringPrintf("%s: invalid indicate pkt size: %d, PDU size: %d", __func__, len + 1,payload_size);return;}gatt_process_notification(tcb, cid, op_code, len, p_data);return;}
Android Bluetooth杂记相关推荐
- Android Bluetooth模块学习笔记
一.蓝牙基础知识 1.蓝牙( Bluetooth )是一种无线技术标准,可实现固定设备.移动设备和楼宇个人域网之间的短距离数据交换.蓝牙基于设备低成本的收发器芯片,传输距离近.低功耗. 2.微波频段: ...
- Android Bluetooth 文件接收路径修改方法
修改文件: packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppReceiveFileInfo.java 相关代码片段: ...
- Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接
我已经尝试了其他评论中的所有建议,但都没有效果,我希望有人能帮助我.我已经为这个问题挣扎了三天了.我确信我的uuid是正确的,并且我知道清单中启用了蓝牙访问. 我正在尝试将我的android应用程序连 ...
- Android Bluetooth BLE相关开发资源汇总
Android开启蓝牙开关 转载自Android:Bluetooth 的打开和关闭 检查系统蓝牙是否开启 BluetoothManager bluetoothManager = (BluetoothM ...
- Android编译自定义sdk,java – 使用自定义android.bluetooth.而不是在android studio中默认的sdk android.jar中存在一个...
我想使用自定义android.bluetooth而不是Android SDK附带的android.jar中的presend.我将android.bluetooth编译成单独的jar文件并导入到stud ...
- 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ A2DP.SINK.sink_connect.s ...
- android hid 编程,Android Bluetooth HID完成详解,androidhid
Android Bluetooth HID完成详解,androidhid Android Bluetooth HID落实详解 Android 关于蓝牙的局部运用的是BlueZ协定栈.然而直到眼前2.3 ...
- Android Bluetooth HID实现详解
Android Bluetooth HID实现详解 Android 关于蓝牙的部分使用的是BlueZ协议栈.但是直到目前2.3.3都没有扩展HID的profile,只是实现了最基本的Handset和d ...
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
一.Android Bluetooth现状 在android官网可以了解到android4.2新增了部分新功能,但是对于BT熟悉的人或许开始头疼了,那就是Android4.2引入了一个新的蓝牙协议栈针 ...
最新文章
- 学习一个 Linux 命令:shutdown 命令
- DELL通过LCD简单的判别服务器的硬件故障
- JPA HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError
- shiro +spring + spring mvc+ mybatis整合【转】
- 本地开发的 SAP UI5 应用,部署到 ABAP 服务器执行出错的问题分析
- 使用Flow快速开发Teams小应用
- android java判断字符串是否为空和是否是手机号和是否是数字,数字转中文
- lwip协议栈在linux运行,LwIP协议栈在uCOS II下的实现
- 第1次在Flash Builder中写程序
- 2015 Autodesk 开发者日( DevDays)和 助力开发周火热报名中
- 连载三:RobotFramework+Selenium+Jenkins分布式构建
- 洛谷 P3359 改造异或树
- Git命令:git常用命令
- 想当好员工,想加薪,想提高的最起码应该注意的几项工作习惯
- ITIL4 讲解:监控管理
- Everything必知必会搜索教程
- html中引号的作用,引号的作用是什么
- java实现中国象棋3:走棋规则的实现
- 一个程序猿小小的梦想-写在16年底的时候
- csgo国服一直连不上服务器,csgo国服显示连接服务器发生错误 | 手游网游页游攻略大全...
热门文章
- 达梦数据库存储过程脚本
- Koa2实现电影微信公众号前后端开发学习视频及源码
- RubyWin32Api Win32OLE
- JSP技术已死 ? (Java Server Page technology will die) ?
- 计算机专业的人的优势
- 城市管理应急无线通信集群管理方案
- 福利来了,智能工业物联网关BL110实现同时采集多种PLC数据到上位机系统与物联网云平台
- Vue 实现批量审核功能实例完整代码(表格批量操作)(Vue开发一)
- c语言程序设计与数据结构清华版,清华大学出版社-图书详情-《程序设计基础与数据结构》...
- dockerfile优化小技巧