bluez调试笔记

蓝牙系列:

bluez调试笔记_weixin_41069709的博客-CSDN博客_bluezbluez移植https://blog.csdn.net/weixin_41069709/article/details/125168114?spm=1001.2014.3001.5502

bluetoothd源码剖析(一)启动流程_weixin_41069709的博客-CSDN博客bluetoothd启动流程详解https://blog.csdn.net/weixin_41069709/article/details/125404935?spm=1001.2014.3001.5502

一.移植

1.下载bluez源码包

源码下载路径

2.下载相关依赖库(这个就不贴路径了):

依赖库版本
libical1.0
readline6.3
libffi3.2.1
libpcre
ncurses6.3(5.9版本可能会因为工具链太新导致编译失败)
glib2.50.3
expat      2.4.8
dbus1.12.10

3.依赖库编译

#libical
path=`pwd`
./bootstrap
./configure --host=arm-linux --prefix=$path/../install CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
make
make install
#readline

#configure文件中修改如下
#  <   if test "$cross_compiling" = yes; then :                                                                                                                                                 
#  >   if test "$cross_compiling" = no; then :

path=`pwd`
./configure --host=arm-linux --prefix=$path/../install --with-shared CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CPPFLAGS="-P"
make
make install
#libpcre
path=`pwd`
./configure --host=arm-linux --prefix=$path/../install --bindir=$path/../install/usr/bin  --enable-unicode-properties --enable-utf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
make
make install
#libffi
path=`pwd`
./configure --prefix=$path/../install/ --host=arm-linux CC=arm-linux-gnueabihf-gcc
make
make install
#ncurses
path=`pwd`
./configure --host=arm-linux --prefix=$path/../install --bindir=$path/../install/usr/bin --with-shared --disable-db-install --disable-stripping --enable-pc-files --with-pkg-config-libdir=$path/../install/lib/pkgconfig CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ CPPFLAGS="-P"
make
make install
#glib
path=`pwd`
./configure --prefix=$path/../install --enable-libmount=no --host=arm-linux --bindir=$path/../install/usr/bin PKG_CONFIG_PATH=$path/../install/lib/pkgconfig \
    PCRE_CFLAGS="-I $path/../install/include/" PCRE_LIBS="-lpcre -lpcrecpp -L$path/../install/lib" ZLIB_CFLAGS="-I $path/../install/include/" ZLIB_LIBS="-lz -L$path/../install/lib" \ 
    LIBFFI_CFLAGS="-I $path/../install/lib/libffi-3.2.1/include/" LIBFFI_LIBS="-lffi -L$path/../install/lib/" \ 
    glib_cv_stack_grows=no glib_cv_uscore=yes ac_cv_func_posix_getpwuid_r=yes ac_cv_func_posix_getgrgid_r=yes CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
make
make install
#dbus
path=`pwd`
./configure --prefix="" --host=arm-linux --datadir="/etc" --libexecdir="/usr" --runstatedir="/var/run" --bindir="/usr/bin" --with-dbus-user="dbus" \
    EXPANDED_LOCALSTATEDIR="/var" PKG_CONFIG="$path/../install/lib/pkgconfig" CC=arm-linux-gnueabihf-gcc EXPAT_CFLAGS="-I$path/../install/include" \ 
    EXPAT_LIBS="-L$path/../install/lib/ -lexpat"
make DESTDIR="$path/../install"
make install DESTDIR="$path/../install"
#bluez

path=`pwd`
./configure --prefix="" --host=arm-linux --bindir="/usr/bin" --sbindir="/usr/sbin" \
    --disable-udev --with-dbusconfdir="/etc" --with-dbussystembusdir=/usr/share/dbus-1/system-services --with-dbussessionbusdir=/usr/share/dbus-1/services \
    --disable-systemd --enable-shared=yes --enable-library --enable-static=no --enable-test --enable-testing --enable-deprecated \
    --enable-experimental --disable-cups --disable-client --libexecdir="/usr/bin"  \
    CC=arm-linux-gnueabihf-gcc GLIB_CFLAGS="-I$path/../include/glib-2.0  -I$path/../install/include/glib-2.0 -I$path/../install/lib/glib-2.0/include/" \
    GLIB_LIBS="-L$path/../install/lib -lreadline -lglib-2.0" \
    DBUS_CFLAGS="-I$path/../install/include -I$path/../install/include/dbus-1.0 -I$path/../install/lib/dbus-1.0/include" \
    DBUS_LIBS="-L$path/../install/lib -lreadline -lglib-2.0 -ldbus-1" ICAL_CFLAGS="-I$path/../install/include" ICAL_LIBS="-I$path/../install/lib -lical -licalvcal"
make DESTDIR="$path/../install"
make install DESTDIR="$path/../install"

注意,dbus和bluez中最好设置prefix为空,具体的install路径设置在make和make install时加上。否则应用会在开发板上运行时从指定的prefix中找相关文件,导致查找失败而无法启动

二.配置

1.交叉编译后的库放到开发板/lib/目录下,应用程序放到/usr/bin目录下

2.dbus与bluez会有一些配置文件,默认放到/etc/dbus-1/中,bluez中会有一个main.conf文件可以自动考虑放到哪个目录

#ls /etc/dbus-1
session.conf  session.d/    system.conf   system.d/
#ls /etc/dbus-1/system.d/
bluetooth.conf

三.运行

1. dbus-daemon --system

2. bluetoothd -n --compat -f main.conf &

四.测试

1. dbus测试

参考链接:

linux 进程间通信 dbus-glib【实例】详解二(上) 消息和消息总线(附代码)_Dontla的博客-CSDN博客_dbus实例讲解https://dontla.blog.csdn.net/article/details/122579082

1)  检测dbus基本接口

dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.DBus / org.freedesktop.DBus.Introspectable.Introspect
method return sender=org.freedesktop.DBus -> dest=:1.2 reply_serial=2
string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="org.freedesktop.DBus">
    <method name="Hello">
      <arg direction="out" type="s"/>
    </method>
    <method name="RequestName">
      <arg direction="in" type="s"/>
      <arg direction="in" type="u"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="ReleaseName">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="StartServiceByName">
      <arg direction="in" type="s"/>
      <arg direction="in" type="u"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="UpdateActivationEnvironment">
      <arg direction="in" type="a{ss}"/>
    </method>
    <method name="NameHasOwner">
      <arg direction="in" type="s"/>
      <arg direction="out" type="b"/>
    </method>
    <method name="ListNames">
      <arg direction="out" type="as"/>
    </method>
    <method name="ListActivatableNames">
      <arg direction="out" type="as"/>
    </method>
    <method name="AddMatch">
      <arg direction="in" type="s"/>
    </method>
    <method name="RemoveMatch">
      <arg direction="in" type="s"/>
    </method>
    <method name="GetNameOwner">
      <arg direction="in" type="s"/>
      <arg direction="out" type="s"/>
    </method>
    <method name="ListQueuedOwners">
      <arg direction="in" type="s"/>
      <arg direction="out" type="as"/>
    </method>
    <method name="GetConnectionUnixUser">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="GetConnectionUnixProcessID">
      <arg direction="in" type="s"/>
      <arg direction="out" type="u"/>
    </method>
    <method name="GetAdtAuditSessionData">
      <arg direction="in" type="s"/>
      <arg direction="out" type="ay"/>
    </method>
    <method name="GetConnectionSELinuxSecurityContext">
      <arg direction="in" type="s"/>
      <arg direction="out" type="ay"/>
    </method>
    <method name="ReloadConfig">
    </method>
    <method name="GetId">
      <arg direction="out" type="s"/>
    </method>
    <method name="GetConnectionCredentials">
      <arg direction="in" type="s"/>
      <arg direction="out" type="a{sv}"/>
    </method>
    <property name="Features" type="as" access="read">
      <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/>
    </property>
    <property name="Interfaces" type="as" access="read">
      <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/>
    </property>
    <signal name="NameOwnerChanged">
      <arg type="s"/>
      <arg type="s"/>
      <arg type="s"/>
    </signal>
    <signal name="NameLost">
      <arg type="s"/>
    </signal>
    <signal name="NameAcquired">
      <arg type="s"/>
    </signal>
  </interface>
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg direction="out" type="s"/>
    </method>
  </interface>
  <interface name="org.freedesktop.DBus.Peer">
    <method name="GetMachineId">
      <arg direction="out" type="s"/>
    </method>
    <method name="Ping">
    </method>
  </interface>
  <node name="org/freedesktop/DBus"/>
</node>
"

2) 查询当前dbus注册对象

dbus-send --system --type=method_call --print-reply --dest=org.freedesktop.DBus / org.freedesktop.DBus.ListNames
method return sender=org.freedesktop.DBus -> dest=:1.3 reply_serial=2
   array [
      string "org.freedesktop.DBus"
      string ":1.3"
      string "org.bluez"
      string ":1.1"
   ]

3) 查询bluez接口(bluetoothd启动后)

dbus-send --system --type=method_call --print-reply --dest=org.bluez / org.freedesktop.DBus.Introspectable.Introspect
method return sender=:1.1 -> dest=:1.4 reply_serial=2
   string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.freedesktop.DBus.ObjectManager"><method name="GetManagedObjects"><arg name="objects" type="a{oa{sa{sv}}}" direction="out"/>
</method><signal name="InterfacesAdded"><arg name="object" type="o"/>
<arg name="interfaces" type="a{sa{sv}}"/>
</signal>
<signal name="InterfacesRemoved"><arg name="object" type="o"/>
<arg name="interfaces" type="as"/>
</signal>
</interface><node name="org"/></node>"

dbus-send --system --type=method_call --print-reply --dest=org.bluez /org org.freedesktop.DBus.Introspectable.Introspect
method return sender=:1.1 -> dest=:1.5 reply_serial=2
   string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <node name="bluez"/>
</node>
"

dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez org.freedesktop.DBus.Introspectable.Introspect
method return sender=:1.6 -> dest=:1.17 reply_serial=2
   string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.bluez.AgentManager1"><method name="RegisterAgent"><arg name="agent" type="o" direction="in"/>
<arg name="capability" type="s" direction="in"/>
</method><method name="UnregisterAgent"><arg name="agent" type="o" direction="in"/>
</method><method name="RequestDefaultAgent"><arg name="agent" type="o" direction="in"/>
</method></interface><interface name="org.bluez.ProfileManager1"><method name="RegisterProfile"><arg name="profile" type="o" direction="in"/>
<arg name="UUID" type="s" direction="in"/>
<arg name="options" type="a{sv}" direction="in"/>
</method><method name="UnregisterProfile"><arg name="profile" type="o" direction="in"/>
</method></interface><node name="hci0"/></node>"

dbus-send --system --type=method_call --print-reply --dest=org.bluez /org/bluez/hci0 org.freedesktop.DBus.Introspectable.Introspect
method return sender=:1.6 -> dest=:1.22 reply_serial=2
   string "<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.bluez.Adapter1"><method name="StartDiscovery"></method><method name="SetDiscoveryFilter"><arg name="properties" type="a{sv}" direction="in"/>
</method><method name="StopDiscovery"></method><method name="RemoveDevice"><arg name="device" type="o" direction="in"/>
</method><method name="GetDiscoveryFilters"><arg name="filters" type="as" direction="out"/>
</method><property name="Address" type="s" access="read"></property><property name="AddressType" type="s" access="read"></property><property name="Name" type="s" access="read"></property><property name="Alias" type="s" access="readwrite"></property><property name="Class" type="u" access="read"></property><property name="Powered" type="b" access="readwrite"></property><property name="Discoverable" type="b" access="readwrite"></property><property name="DiscoverableTimeout" type="u" access="readwrite"></property><property name="Pairable" type="b" access="readwrite"></property><property name="PairableTimeout" type="u" access="readwrite"></property><property name="Discovering" type="b" access="read"></property><property name="UUIDs" type="as" access="read"></property><property name="Modalias" type="s" access="read"></property></interface><interface name="org.freedesktop.DBus.Properties"><method name="Get"><arg name="interface" type="s" direction="in"/>
<arg name="name" type="s" direction="in"/>
<arg name="value" type="v" direction="out"/>
</method><method name="Set"><arg name="interface" type="s" direction="in"/>
<arg name="name" type="s" direction="in"/>
<arg name="value" type="v" direction="in"/>
</method><method name="GetAll"><arg name="interface" type="s" direction="in"/>
<arg name="properties" type="a{sv}" direction="out"/>
</method><signal name="PropertiesChanged"><arg name="interface" type="s"/>
<arg name="changed_properties" type="a{sv}"/>
<arg name="invalidated_properties" type="as"/>
</signal>
</interface><interface name="org.bluez.GattManager1"><method name="RegisterApplication"><arg name="application" type="o" direction="in"/>
<arg name="options" type="a{sv}" direction="in"/>
</method><method name="UnregisterApplication"><arg name="application" type="o" direction="in"/>
</method></interface><interface name="org.bluez.Media1"><method name="RegisterEndpoint"><arg name="endpoint" type="o" direction="in"/>
<arg name="properties" type="a{sv}" direction="in"/>
</method><method name="UnregisterEndpoint"><arg name="endpoint" type="o" direction="in"/>
</method><method name="RegisterPlayer"><arg name="player" type="o" direction="in"/>
<arg name="properties" type="a{sv}" direction="in"/>
</method><method name="UnregisterPlayer"><arg name="player" type="o" direction="in"/>
</method></interface><interface name="org.bluez.NetworkServer1"><method name="Register"><arg name="uuid" type="s" direction="in"/>
<arg name="bridge" type="s" direction="in"/>
</method><method name="Unregister"><arg name="uuid" type="s" direction="in"/>
</method></interface><interface name="org.bluez.LEAdvertisingManager1"><method name="RegisterAdvertisement"><arg name="advertisement" type="o" direction="in"/>
<arg name="options" type="a{sv}" direction="in"/>
</method><method name="UnregisterAdvertisement"><arg name="service" type="o" direction="in"/>
</method><property name="ActiveInstances" type="y" access="read"></property><property name="SupportedInstances" type="y" access="read"></property><property name="SupportedIncludes" type="as" access="read"></property></interface></node>"

4) dbus调试bluez

4.1)获取蓝牙状态

dbus-send --system --print-reply --type=method_call --dest=org.bluez / org.freedesktop.DBus.ObjectManager.GetManagedObjects
method return sender=:1.99 -> dest=:1.125 reply_serial=2
   array [
      dict entry(
         object path "/org/bluez"
         array [
            dict entry(
               string "org.freedesktop.DBus.Introspectable"
               array [
               ]
            )
            dict entry(
               string "org.bluez.AgentManager1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.ProfileManager1"
               array [
               ]
            )
         ]
      )
      dict entry(
         object path "/org/bluez/hci0"
         array [
            dict entry(
               string "org.freedesktop.DBus.Introspectable"
               array [
               ]
            )
            dict entry(
               string "org.bluez.Adapter1"
               array [
                  dict entry(
                     string "Address"
                     variant                         string "CC:4B:73:F8:5B:15"
                  )
                  dict entry(
                     string "AddressType"
                     variant                         string "public"
                  )
                  dict entry(
                     string "Name"
                     variant                         string "%h-%d"
                  )
                  dict entry(
                     string "Alias"
                     variant                         string "ILSA_GW_106011111111111111"
                  )
                  dict entry(
                     string "Class"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Powered"
                     variant                         boolean false
                  )
                  dict entry(
                     string "Discoverable"
                     variant                         boolean false
                  )
                  dict entry(
                     string "DiscoverableTimeout"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Pairable"
                     variant                         boolean false
                  )
                  dict entry(
                     string "PairableTimeout"
                     variant                         uint32 0
                  )
                  dict entry(
                     string "Discovering"
                     variant                         boolean false
                  )
                  dict entry(
                     string "UUIDs"
                     variant                         array [
                           string "00001800-0000-1000-8000-00805f9b34fb"
                           string "00001801-0000-1000-8000-00805f9b34fb"
                           string "0000110e-0000-1000-8000-00805f9b34fb"
                           string "00001200-0000-1000-8000-00805f9b34fb"
                           string "0000110c-0000-1000-8000-00805f9b34fb"
                           string "000000ee-0000-1000-8000-00805f9b34fb"
                        ]
                  )
                  dict entry(
                     string "Modalias"
                     variant                         string "usb:v1D6Bp0246d0530"
                  )
               ]
            )
            dict entry(
               string "org.freedesktop.DBus.Properties"
               array [
               ]
            )
            dict entry(
               string "org.bluez.GattManager1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.Media1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.NetworkServer1"
               array [
               ]
            )
            dict entry(
               string "org.bluez.LEAdvertisingManager1"
               array [
                  dict entry(
                     string "ActiveInstances"
                     variant                         byte 0
                  )
                  dict entry(
                     string "SupportedInstances"
                     variant                         byte 1
                  )
                  dict entry(
                     string "SupportedIncludes"
                     variant                         array [
                           string "local-name"
                        ]
                  )
               ]
            )
         ]
      )
   ]

4.2) 启动蓝牙

dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0 org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Powered variant:boolean:true
method return sender=:1.99 -> dest=:1.126 reply_serial=2
interface org.bluez.Adapter1, name Powered[CHG] Controller CC:4B:73:F8:5B:15 Powered: yes

4.3)获取MAC地址

dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0 org.freedesktop.DBus.Properties.Get string:org.bluez.Adapter1 string:Address
method return sender=:1.99 -> dest=:1.127 reply_serial=2
   variant       string "CC:4B:73:F8:5B:15"

4.4)设置蓝牙名

dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0 org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Alias variant:string:tttttee
eeesssstttt
method return sender=:1.99 -> dest=:1.128 reply_serial=2
interface org.bluez.Adapter1, name Alias[CHG] Controller CC:4B:73:F8:5B:15 Alias: ttttteeeeesssstttt

4.5)开始扫描

dbus-send --system --print-reply --type=method_call --dest=org.bluez /org/bluez/hci0 org.bluez.Adapter1.StartDiscovery
method return sender=:1.147 -> dest=:1.154 reply_serial=2

5.补充

dbus提供的借口很不全面,尤其是没有切换蓝牙工作模式(BLE与BREDR切换),所以很多时候还是需要使用mgmt_send命令进行开发

  • 12
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值