蓝牙!你为什么搜索不到设备?

蓝牙(Bluetooth)是一种无线通讯协议,其名Bluetooth来自于十世纪丹麦国王哈拉尔 HaralBluetooth,最先由英特尔的工程师 Jim Kardach在1997的一个无线通信行业的会议上,提出使用“Bluetooth”作为无线通讯技术标准的名称,随后Bluetooth这个名字便一直沿用至今;20多年过去了,随着科技的进步与发展,在这片蓝色的星球上,覆盖面积极为广阔的“蓝色”已经不止是水资源了,“蓝牙”也应该算是其中的一份子。
从蓝牙耳机、蓝牙键盘/鼠标到物联网,当我们享受蓝牙这项技术带来便利的同时,它也给我们带来各种“罢工”的难题;不巧的是,现在GDK8就遇到了;如下图所示,当你打开蓝牙去搜索设备的时候,会发现它找不出来任何一个设备。

罢工归罢工,想让它再开始为我们“工作”,也只能一步一步的,去解决问题了!

初步试探

先输入rfkill,查看一下对应的信息。

输入hciconfig -a,看一下蓝牙设备详细信息;它确实已经开启了,但就是搜索不到设备。

试探性的输入一下hcitool scan;不出意外,找不出任何一个设备(没有低级错误,绝对开启其他的蓝牙设备了,保证正常的蓝牙能搜到)。

考虑一小会,配置蓝牙应该需要配置相应的配置文件,于是感觉打开main.conf文件一看(见下图),好家伙!全被注释掉了,莫非就是配置文件的问题?
这好像找到了答案,于是赶紧找到一台能搜到蓝牙设备的机器,查看它的配置文件;可惜,问题没有这么好解决,那里面的配置文件也是全部注释掉的;白高兴一场!
回过劲来,转念一想,之前GDK8的WiFI有问题是dts文件写的有问题,莫非蓝牙这次也是,把设备的dts转储出来,再和好用的机器的一比较,果然啊!!!就是这样!!!
它们一样的。。。。。。
在经过上面试探性的解决失败后,只能考虑使用调试器了。

编译bluez源代码

在上调试器之前,先到https://packages.ubuntu.com/impish/bluez 网站去下载bluez的源代码。
如果想要搜索ubuntu的其他包可以到这个网站去下载(https://packages.ubuntu.com/ ),或者浏览器直接搜索ubuntu packages也行。
下载完成后,先准备编译一下源代码;这主要是有两个目的,一是别以前的程序有什么Bug没有改,然后换新编译后的就可以了,二是生成一个具有调试符号的程序,这样也方便我们后续去查看问题。
先看一下readme文件,里面还是很贴心的给出了bluez依赖的东西、配置和编译的命令,当然还有相关的说明;可以看到readme里面的配置命令没有加–g的选项,但我们为了调试的需求,要把–g和–enable-debug给加上。

在GDK8上面D-Bus(library)、udev library(optional)、readline(command line clients)应该都是没有的,需要安装一下;具体安装步骤如下。

1. sudo apt-get update
2. sudo apt --fix-broken install
3. sudo apt install libdbus-1-dev
4. sudo apt install libudev-dev
5. sudo apt install libical-dev
6. sudo apt install libreadline-dev
7. sudo apt-get install python-docutils

进入bluez-5.48目录下,输入./configure --g --enable-debug --prefix=/usr --mandir=/usr/share/man --sysconfdir=/etc --localstatedir=/var;不输错应该就没有什么问题;正确的话,就如下图所示。
编译时间大概是11:02

执行sudo su切换至root用户,输入sudo make && make install命令;如下图所示。
假如已经sudo了或root下make时遇到权限问题,应该给对应的脚本(make会提示你的)加上可执行权限 [chmod +x]。

此时我们输入file ./src/bluetooth,可以看到可执行文件是有符号的(1:with debug info;2.no stripped,如果查看一个没有符号的文件,那么就会看到stripped)
stripped:将程序中的符号信息剔除掉了。
not stripped:保留了程序中的符号信息。

查看调试支持

在bluez的源代码里面可以看到下面的关于DBG定义,它们是用于输出调试信息的。

#define DBG_IDX(idx, fmt, arg...) do { \
    static struct btd_debug_desc __btd_debug_desc \
    __attribute__((used, section("__debug"), aligned(8))) = { \
        .file = __FILE__, .flags = BTD_DEBUG_FLAG_DEFAULT, \
    }; \
    if (__btd_debug_desc.flags & BTD_DEBUG_FLAG_PRINT) \
        btd_debug(idx, "%s:%s() " fmt, __FILE__, __func__ , ## arg); \
} while (0)
#define DBG(fmt, arg...) DBG_IDX(0xffff, fmt, ## arg)

我们可以在btd_debug_desc结构体内看到里面存储着文件名和标志位。

struct btd_debug_desc {
    const char *file;
#define BTD_DEBUG_FLAG_DEFAULT (0)
#define BTD_DEBUG_FLAG_PRINT   (1 << 0)
    unsigned int flags;
} __attribute__((aligned(8)));

至于是否打印这些信息,则会根据btd_debug_desc内的标志位信息进行判断。
关于上面的东西,光纸上谈兵当然不行,还要“格物”一下,才可以理解更加深刻;所以立马就打开调试器去看一下。
首先在命令行输入sudo gdb --args /usr/lib/bluetooth/bluetoothd -n -d(bluetoothd的位置一般是这个,如果不是,可以自行去查找其位置在哪里)。
-n: 在后台不做为服务运行。
-d: 输出调试信息
想要获得调试信息,有两种方法可以;一是设置-d参数,二是在源代码内修改BTD_DEBUG_FLAG_DEFAULT标志位(1:debug 0:no debug),除此之外,也可以在调试过程中手动修改BTD_DEBUG_FLAG_DEFAULT标志
更多参数及说明可以参考链接:https://linux.die.net/man/8/bluetoothd
从下面的对比中,可以很明显的看到有没有-d选项时,输出信息的数量的差异是非常巨大的。

有-d选项:
(gdb) r
Starting program: /usr/lib/bluetooth/bluetoothd -n -d
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
bluetoothd[1015]: Bluetooth daemon 5.62
bluetoothd[1015]: src/main.c:parse_config() parsing /etc/bluetooth/main.conf
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “DiscoverableTimeout” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “AlwaysPairable” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “PairableTimeout” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “Privacy” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “JustWorksRepairing” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “TemporaryTimeout” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “Name” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “Class” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “DeviceID” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have key “ReverseServiceDiscovery” in group “General”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “GATT”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “GATT”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “GATT”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “GATT”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “AVDTP”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “AVDTP”
bluetoothd[1015]: src/main.c:parse_config() Key file does not have group “AdvMon”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “BR”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/main.c:parse_mode_config() Key file does not have group “LE”
bluetoothd[1015]: src/adapter.c:adapter_init() sending read version command
bluetoothd[1015]: Starting SDP server
bluetoothd[1015]: src/sdpd-service.c:register_device_id() Adding device id record for 0002:1d6b:0246:053e
bluetoothd[1015]: src/plugin.c:plugin_init() Loading builtin plugins
bluetoothd[1015]: src/plugin.c:add_plugin() Loading hostname plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading wiimote plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading autopair plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading policy plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading a2dp plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading avrcp plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading network plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading input plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading hog plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading gap plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading scanparam plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading deviceinfo plugin
bluetoothd[1015]: src/plugin.c:add_plugin() Loading battery plugin
bluetoothd[1015]: src/plugin.c:plugin_init() Loading plugins /usr/lib/bluetooth/plugins
bluetoothd[1015]: profiles/input/suspend-none.c:suspend_init()
bluetoothd[1015]: profiles/network/manager.c:read_config() /etc/bluetooth/network.conf: Key file does not have key “DisableSecurity” in group “General”
bluetoothd[1015]: profiles/network/manager.c:read_config() Config options: Security=true
bluetoothd[1015]: profiles/network/bnep.c:bnep_init() kernel lacks bnep-protocol support
bluetoothd[1015]: src/plugin.c:plugin_init() System does not support network plugin
bluetoothd[1015]: src/main.c:main() Entering main loop
bluetoothd[1015]: src/rfkill.c:rfkill_event() RFKILL event idx 0 type 2 op 0 soft 0 hard 0
bluetoothd[1015]: Bluetooth management interface 1.10 initialized
bluetoothd[1015]: src/adapter.c:read_version_complete() sending read supported commands command
bluetoothd[1015]: src/adapter.c:read_version_complete() sending read index list command
bluetoothd[1015]: src/rfkill.c:rfkill_event() RFKILL event idx 1 type 1 op 0 soft 0 hard 0
bluetoothd[1015]: src/rfkill.c:rfkill_event() RFKILL event idx 2 type 1 op 0 soft 0 hard 0
bluetoothd[1015]: src/adapter.c:read_commands_complete() Number of commands: 61
bluetoothd[1015]: src/adapter.c:read_commands_complete() Number of events: 34
bluetoothd[1015]: src/adapter.c:read_commands_complete() enabling kernel-side connection control
bluetoothd[1015]: src/rfkill.c:rfkill_event() RFKILL event idx 3 type 2 op 0 soft 0 hard 0
bluetoothd[1015]: src/adapter.c:read_index_list_complete() Number of controllers: 1
bluetoothd[1015]: src/adapter.c:read_index_list_complete() Found index 0
bluetoothd[1015]: src/adapter.c:index_added() index 0
bluetoothd[1015]: src/adapter.c:reset_adv_monitors() sending remove Adv Monitor command with handle 0
bluetoothd[1015]: src/adapter.c:btd_adapter_new() System name: BlueZ 5.62
bluetoothd[1015]: src/adapter.c:btd_adapter_new() Major class: 0
bluetoothd[1015]: src/adapter.c:btd_adapter_new() Minor class: 0
bluetoothd[1015]: src/adapter.c:btd_adapter_new() Modalias: usb:v1D6Bp0246d053E
bluetoothd[1015]: src/adapter.c:btd_adapter_new() Discoverable timeout: 180 seconds
bluetoothd[1015]: src/adapter.c:btd_adapter_new() Pairable timeout: 0 seconds
bluetoothd[1015]: src/adapter.c:index_added() sending read info command for index 0
bluetoothd[1015]: src/adapter.c:reset_adv_monitors_complete() Failed to reset Adv Monitors: Unknown Command (0x01)
bluetoothd[1015]: src/adapter.c:read_info_complete() index 0 status 0x00
bluetoothd[1015]: src/adapter.c:clear_uuids() sending clear uuids command for index 0
bluetoothd[1015]: src/adapter.c:clear_devices() sending clear devices command for index 0
bluetoothd[1015]: src/adapter.c:set_privacy() sending set privacy command for index 0
bluetoothd[1015]: src/adapter.c:set_privacy() setting privacy mode 0x00 for index 0
bluetoothd[1015]: src/gatt-database.c:btd_gatt_database_new() GATT Manager registered for adapter: /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10001
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000007-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001800-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10002
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000007-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001801-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10003
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000007-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000180a-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/advertising.c:btd_adv_manager_new() LE Advertising Manager created for adapter: /org/bluez/hci0
bluetoothd[1015]: profiles/audio/a2dp.c:media_server_probe() path /org/bluez/hci0
bluetoothd[1015]: plugins/policy.c:policy_adapter_probe()
bluetoothd[1015]: plugins/hostname.c:hostname_probe()
bluetoothd[1015]: profiles/audio/avrcp.c:avrcp_controller_server_probe() path /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10004
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000017-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110e-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110f-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: profiles/audio/avrcp.c:avrcp_target_server_probe() path /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10005
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000017-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110c-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110e-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: profiles/audio/a2dp.c:a2dp_sink_server_probe() path /org/bluez/hci0
bluetoothd[1015]: profiles/audio/a2dp.c:a2dp_source_server_probe() path /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:btd_adapter_unblock_address() hci0 00:00:00:00:00:00
bluetoothd[1015]: src/adapter.c:load_link_keys() hci0 keys 0 debug_keys 0
bluetoothd[1015]: src/adapter.c:load_ltks() hci0 keys 0
bluetoothd[1015]: src/adapter.c:load_irks() hci0 irks 0
bluetoothd[1015]: src/adapter.c:load_conn_params() hci0 conn params 0
bluetoothd[1015]: src/adapter.c:load_connections() sending get connections command for index 0
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/adapter.c:set_did() hci0 source 2 vendor 1d6b product 246 version 53e
bluetoothd[1015]: src/adapter.c:adapter_register() Adapter /org/bluez/hci0 registered
bluetoothd[1015]: src/adapter.c:set_dev_class() sending set device class command for index 0
bluetoothd[1015]: src/adapter.c:set_name() sending set local name command for index 0
bluetoothd[1015]: src/adapter.c:adapter_start() adapter /org/bluez/hci0 has been enabled
bluetoothd[1015]: src/adapter.c:trigger_passive_scanning()
bluetoothd[1015]: Failed to set privacy: Rejected (0x0b)
bluetoothd[1015]: src/adapter.c:load_link_keys_complete() link keys loaded for hci0
bluetoothd[1015]: src/adapter.c:load_ltks_complete() LTKs loaded for hci0
bluetoothd[1015]: src/adapter.c:load_irks_complete() IRKs loaded for hci0
bluetoothd[1015]: src/adapter.c:load_conn_params_complete() Connection Parameters loaded for hci0
bluetoothd[1015]: src/adapter.c:get_connections_complete() Connection count: 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "Message Notification"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Message Notification" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Message Notification listening on PSM 4097
bluetoothd[1015]: src/profile.c:ext_start_servers() Message Notification listening on chan 17
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10006
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001133-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001134-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "Message Access"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Message Access" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Message Access listening on PSM 4099
bluetoothd[1015]: src/profile.c:ext_start_servers() Message Access listening on chan 16
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10007
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001132-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001134-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "Phone Book Access"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Phone Book Access" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Phone Book Access listening on PSM 4101
bluetoothd[1015]: src/profile.c:ext_start_servers() Phone Book Access listening on chan 15
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10008
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000112f-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001130-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "Synchronization"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Synchronization" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Synchronization listening on chan 14
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x10009
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001104-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "File Transfer"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "File Transfer" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() File Transfer listening on PSM 4103
bluetoothd[1015]: src/profile.c:ext_start_servers() File Transfer listening on chan 10
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000a
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001106-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.27
bluetoothd[1015]: src/profile.c:create_ext() Created "Object Push"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Object Push" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Object Push listening on PSM 4105
bluetoothd[1015]: src/profile.c:ext_start_servers() Object Push listening on chan 9
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000b
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000008-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001105-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: profiles/audio/avdtp.c:avdtp_register_sep() SEP 0x5555692140 registered: type:0 codec:0 seid_pool:0x5555693f48 seid:1
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000c
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000019-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110a-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110d-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: Endpoint registered: sender=:1.31 path=/MediaEndpoint/A2DPSource
bluetoothd[1015]: profiles/audio/avdtp.c:avdtp_register_sep() SEP 0x55556a4b90 registered: type:1 codec:0 seid_pool:0x5555693f48 seid:2
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000d
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000019-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110b-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 0000110d-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: Endpoint registered: sender=:1.31 path=/MediaEndpoint/A2DPSink
bluetoothd[1015]: src/profile.c:register_profile() sender :1.31
bluetoothd[1015]: src/profile.c:create_ext() Created "Headset Voice gateway"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Headset Voice gateway" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Headset Voice gateway listening on chan 12
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000e
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001108-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001112-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001203-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/profile.c:register_profile() sender :1.31
bluetoothd[1015]: src/profile.c:create_ext() Created "Headset unit"
bluetoothd[1015]: src/profile.c:ext_adapter_probe() "Headset unit" probed
bluetoothd[1015]: src/profile.c:ext_start_servers() Headset unit listening on chan 3
bluetoothd[1015]: src/adapter.c:adapter_service_add() /org/bluez/hci0
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Adding record with handle 0x1000f
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000003-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00000100-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001002-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001108-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001131-0000-1000-8000-00805f9
bluetoothd[1015]: src/sdpd-service.c:add_record_to_server() Record pattern UUID 00001203-0000-1000-8000-00805f9
bluetoothd[1015]: src/adapter.c:adapter_service_insert() /org/bluez/hci0
bluetoothd[1015]: src/adapter.c:add_uuid() sending add uuid command for index 0
bluetoothd[1015]: src/adapter.c:dev_class_changed_callback() Class: 0x000000
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Name: BlueZ 5.62
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Short name:
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Current alias: BlueZ 5.62
bluetoothd[1015]: src/adapter.c:dev_class_changed_callback() Class: 0x100000
bluetoothd[1015]: src/adapter.c:dev_class_changed_callback() Class: 0x180000
bluetoothd[1015]: src/adapter.c:dev_class_changed_callback() Class: 0x1c0000
bluetoothd[1015]: src/adapter.c:dev_class_changed_callback() Class: 0x3c0000
bluetoothd[1015]: plugins/hostname.c:property_changed() static hostname: gdk8
bluetoothd[1015]: plugins/hostname.c:property_changed() pretty hostname:
bluetoothd[1015]: plugins/hostname.c:update_name() name: gdk8
bluetoothd[1015]: src/adapter.c:adapter_set_name() name: gdk8
bluetoothd[1015]: src/adapter.c:adapter_set_name() alias: gdk8
bluetoothd[1015]: src/adapter.c:set_name() sending set local name command for index 0
bluetoothd[1015]: plugins/hostname.c:property_changed() chassis:
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Name: gdk8
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Short name:
bluetoothd[1015]: src/adapter.c:local_name_changed_callback() Current alias: gdk8
bluetoothd[1015]: src/adapter.c:start_discovery() sender :1.43
bluetoothd[1015]: src/adapter.c:update_discovery_filter()
bluetoothd[1015]: src/adapter.c:discovery_filter_to_mgmt_cp()
bluetoothd[1015]: src/adapter.c:trigger_start_discovery()
bluetoothd[1015]: src/adapter.c:cancel_passive_scanning()
bluetoothd[1015]: src/adapter.c:start_discovery_timeout()
bluetoothd[1015]: src/adapter.c:start_discovery_timeout() adapter->current_discovery_filter == 0
bluetoothd[1015]: src/adapter.c:start_discovery_complete() status 0x00
没有-d选项:
(gdb) r
Starting program: /usr/lib/bluetooth/bluetoothd -n
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
bluetoothd[1056]: Bluetooth daemon 5.62
bluetoothd[1056]: Starting SDP server
bluetoothd[1056]: profiles/network/bnep.c:bnep_init() kernel lacks bnep-protocol support
bluetoothd[1056]: src/plugin.c:plugin_init() System does not support network plugin
bluetoothd[1056]: Bluetooth management interface 1.10 initialized
bluetoothd[1056]: src/adapter.c:reset_adv_monitors_complete() Failed to reset Adv Monitors: Unknown Command (0x01)
bluetoothd[1056]: Failed to set privacy: Rejected (0x0b)
bluetoothd[1056]: Endpoint registered: sender=:1.31 path=/MediaEndpoint/A2DPSource
bluetoothd[1056]: Endpoint registered: sender=:1.31 path=/MediaEndpoint/A2DPSink

在discovering_callback函数处设置一个断点,然后跑起来;如下所示。

Breakpoint 1, discovering_callback (index=0, length=2, param=0x555568cf66, user_data=0x5555693aa0)
    at src/adapter.c:1932
1932    {
(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000055555d28d8 in discovering_callback at src/adapter.c:1932
        breakpoint already hit 1 time
(gdb)

输入disassemble看一下,可以看到_btddebug_desc,想必是discovering_callback内使用了DBG(……)。

(gdb) disassemble
Dump of assembler code for function discovering_callback:
=> 0x00000055555d28d8 <+0>:     and     w1, w1, #0xffff
   0x00000055555d28dc <+4>:     cmp     w1, #0x1
   0x00000055555d28e0 <+8>:     b.ls    0x55555d29bc <discovering_callback+228>  // b.plast
   0x00000055555d28e4 <+12>:    stp     x29, x30, [sp, #-32]!
   0x00000055555d28e8 <+16>:    adrp    x0, 0x555567d000 <__btd_debug_desc.19910>
   0x00000055555d28ec <+20>:    mov     x29, sp
   0x00000055555d28f0 <+24>:    ldr     w0, [x0, #1768]
   0x00000055555d28f4 <+28>:    stp     x19, x20, [sp, #16]
   0x00000055555d28f8 <+32>:    mov     x19, x3
   0x00000055555d28fc <+36>:    mov     x20, x2
   0x00000055555d2900 <+40>:    ldrb    w6, [x2, #1]
   0x00000055555d2904 <+44>:    tbnz    w0, #0, 0x55555d2950 <discovering_callback+120>
   0x00000055555d2908 <+48>:    ldrb    w0, [x19, #132]
   0x00000055555d290c <+52>:    cmp     w0, w6
   0x00000055555d2910 <+56>:    b.eq    0x55555d2944 <discovering_callback+108>  // b.none
   0x00000055555d2914 <+60>:    ldr     x0, [x19, #136]
   0x00000055555d2918 <+64>:    ldrb    w1, [x20]
   0x00000055555d291c <+68>:    strb    w1, [x19, #131]
   0x00000055555d2920 <+72>:    strb    w6, [x19, #132]
   0x00000055555d2924 <+76>:    cbz     x0, 0x55555d2988 <discovering_callback+176>
   0x00000055555d2928 <+80>:    ldrb    w0, [x19, #133]
   0x00000055555d292c <+84>:    cbnz    w0, 0x55555d2944 <discovering_callback+108>
   0x00000055555d2930 <+88>:    cbz     w6, 0x55555d29a0 <discovering_callback+200>
   0x00000055555d2934 <+92>:    cmp     w6, #0x1
   0x00000055555d2938 <+96>:    b.ne    0x55555d2944 <discovering_callback+108>  // b.any
   0x00000055555d293c <+100>:   ldr     w0, [x19, #176]
   0x00000055555d2940 <+104>:   cbnz    w0, 0x55555d29e0 <discovering_callback+264>
   0x00000055555d2944 <+108>:   ldp     x19, x20, [sp, #16]
   0x00000055555d2948 <+112>:   ldp     x29, x30, [sp], #32
   0x00000055555d294c <+116>:   ret
   0x00000055555d2950 <+120>:   ldrb    w7, [x3, #129]
   0x00000055555d2954 <+124>:   adrp    x1, 0x555563d000
   0x00000055555d2958 <+128>:   ldrb    w5, [x2]
   0x00000055555d295c <+132>:   add     x1, x1, #0xeb0
   0x00000055555d2960 <+136>:   ldrh    w4, [x3, #4]
   0x00000055555d2964 <+140>:   adrp    x3, 0x555563b000 <__func__.15864>
   0x00000055555d2968 <+144>:   add     x3, x3, #0xc28
   0x00000055555d296c <+148>:   adrp    x2, 0x555563c000 <__func__.25491>
   0x00000055555d2970 <+152>:   add     x3, x3, #0x438
   0x00000055555d2974 <+156>:   add     x2, x2, #0x920
---Type <return> to continue, or q <return> to quit---q

x /2gx 0x555567d000 继续看一下里面的信息;第一个参数就是文件名,利用p (char*)把结果显示成人类可见的形式后,就可以看到”src/gatt-database.c”文件名了;第二个参数就是标志位的信息了,此时标志位为1,这就代表会打印调试信息;当然也可以利用set去手动修改标志位。

(gdb) x /2gx 0x555567d000
0x555567d000 <__btd_debug_desc.19910>:  0x00000055556384a8      0x0000000000000001
(gdb) p (char*)0x00000055556384a8
$2 = 0x55556384a8 "src/gatt-database.c"
(gdb) set *(int*)(0x555567d000+8)=0
(gdb) x /2gx 0x555567d000
0x555567d000 <__btd_debug_desc.19910>:  0x00000055556384a8      0x0000000000000000
(gdb)

假如已经选上了-d选项,那么标志位是不支持修改的,固定是1。

(gdb) x /2gx 0x555567d000
0x555567d000 <__btd_debug_desc.19910>:  0x00000055556384a8      0x0000000000000001
(gdb) p (char*)0x00000055556384a8
$2 = 0x55556384a8 "src/gatt-database.c"
(gdb) set *(int*)(0x555567d000+8)=0
(gdb) x /2gx 0x555567d000
0x555567d000 <__btd_debug_desc.19910>:  0x00000055556384a8      0x0000000000000001
(gdb)

除此之外,还可以手动调用一下_btdtoggle_debug 函数,切换到调试模式,好多打印一些信息,以帮助我们去排查问题。

(gdb) call __btd_toggle_debug()
(gdb) c
Continuing.
bluetoothd[1094]: src/adapter.c:discovering_callback() hci0 type 7 discovering 0 method 0
bluetoothd[1094]: src/adapter.c:trigger_start_discovery()
bluetoothd[1094]: src/adapter.c:cancel_passive_scanning()
bluetoothd[1094]: src/adapter.c:stop_discovery() sender :1.52
bluetoothd[1094]: src/adapter.c:discovery_remove() owner :1.52
bluetoothd[1094]: src/adapter.c:discovery_free() 0x55556a76d0
bluetoothd[1094]: src/adapter.c:trigger_passive_scanning()
bluetoothd[1094]: src/adapter.c:start_discovery() sender :1.52
bluetoothd[1094]: src/adapter.c:update_discovery_filter()
bluetoothd[1094]: src/adapter.c:discovery_filter_to_mgmt_cp()
bluetoothd[1094]: src/adapter.c:trigger_start_discovery()
bluetoothd[1094]: src/adapter.c:cancel_passive_scanning()
bluetoothd[1094]: src/adapter.c:start_discovery_timeout()
bluetoothd[1094]: src/adapter.c:start_discovery_timeout() adapter->current_discovery_filter == 0
bluetoothd[1094]: src/adapter.c:start_discovery_complete() status 0x00

Breakpoint 1, discovering_callback (index=0, length=2, param=0x555568cf66, user_data=0x5555693aa0)
    at src/adapter.c:1932
1932    {
(gdb) c
Continuing.
bluetoothd[1094]: src/adapter.c:discovering_callback() hci0 type 7 discovering 1 method 0

Breakpoint 1, discovering_callback (index=0, length=2, param=0x555568cf66, user_data=0x5555693aa0)
    at src/adapter.c:1932
1932    {

先用了一下GDB,不还是使用Nano Code调试起来更爽一点;光是图形化的界面就要胜出不少,而且不管看寄存器信息、反汇编信息还是源代码,这都要比GDB方便上不少。

打印日志

蓝牙默认是不把日志打印出来的,但是假如想要获取更多关于蓝牙的信息,那么我们还是要想办法把日志打印出来;
在src/shared/log.c内加上如下的代码,输出日志到/var/log内。
先判断一下当前进程有没有日志,避免重复;之前没有日志的话,就把日志输出到/var/log下面;最后判断一下当前进程有没有日志,有日志的话,就把信息放到日志里面。

#include <time.h>

int GetLogFileName(char* szFullFileName, int dwSize)
{
    time_t timep;
    struct tm *ptm;
    time(&timep);
    ptm = localtime(&timep);
    pid_t pid;

    pid = getpid();
    sprintf(szFullFileName, "/var/log/bluez%d%02d%02d_%02d%02d%02d_%d.log", 
        ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,
        ptm->tm_hour, ptm->tm_min, ptm->tm_sec,
        pid);
    return 0;
}

在int bt_log_open()内,return err;的下方添加:
    if (log_file == NULL){
        GetLogFileName(szFileName, sizeof(szFileName));
        log_file = fopen(szFileName, "wb+");
    }

在int bt_log_vprintf()内,len = strlen(str);的下方添加:
    if(log_file != NULL){
        str[len] = '\n';
        fwrite(str, 1, len + 1, log_file);
        fflush(log_file);
    }

此时再次启动蓝牙,就可以看到打印的日志了;如下图所示。
蓝牙进程杀掉后还会自动启动,需要中止服务才可以 [sudo systemctl disable bluetooth],想要恢复自动启动的状态再enable即可。

如何让内核”日记“一条不丢?

当我们想要查看内核相关信息的时候,经常会用到dmesg命令,但是我们知道dmesg是从环形缓冲区内得到这些消息的,而这个缓冲区通常只有128KB的大小;假如内核打印的消息过多,就难免会“丢掉”前面的消息,那么如何保证让内核信息一条也不丢失呢?
可以参考文章:https://www.nanocode.cn/wiki/docs/gdk8_primer/primer_gdk8_buflen

结果

蓝牙无法搜索到设备是固件的问题,到官网的软件更新上下载新版本的镜像之后,即可解决问题。

作者:admin  创建时间:2021-12-03 17:45
最后编辑:Taylor  更新时间:2024-09-14 11:25