你如何通过dbus执行蓝牙发现?

时间:2017-11-21 16:21:00

标签: bash dbus bluez

我是初学dbus并试图用bash dbus-send命令执行蓝牙扫描。我用这句话:

$dbus-send --system  --type=method_call --dest=org.bluez --print-reply /org/bluez/hci0 org.bluez.Adapter1.StartDiscovery

但没有发现......

在dbus-monitor中我看到:

method call time=1511273024.833459 sender=:1.55 -> destination=org.freedesktop.DBus serial=1 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=Hello
method return time=1511273024.921444 sender=org.freedesktop.DBus -> destination=:1.55 serial=1 reply_serial=1
   string ":1.55"
signal time=1511273024.923719 sender=org.freedesktop.DBus -> destination=(null destination) serial=101 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
 string ":1.55"
string ""
string ":1.55"
  signal time=1511273024.926411 sender=org.freedesktop.DBus ->  destination=:1.55 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
string ":1.55"
method call time=1511273024.927109 sender=:1.55 -> destination=org.bluez serial=2 path=/org/bluez/hci0; interface=org.bluez.Adapter1; member=StartDiscovery
method call time=1511273024.927628 sender=:1.1 -> destination=org.freedesktop.DBus serial=69 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
string "type='signal',sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0=':1.55'"
method return time=1511273024.928272 sender=org.freedesktop.DBus -> destination=:1.1 serial=43 reply_serial=69
method return time=1511273024.928729 sender=:1.1 -> destination=:1.55 serial=70 reply_serial=2
signal time=1511273024.929236 sender=org.freedesktop.DBus -> destination=:1.55 serial=5 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameLost
string ":1.55"
signal time=1511273024.929945 sender=org.freedesktop.DBus -> destination=(null destination) serial=44 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
string ":1.55"
string ":1.55"
string ""
method call time=1511273024.930956 sender=:1.1 -> destination=org.freedesktop.DBus serial=71 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=RemoveMatch
string "type='signal',sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0=':1.55'"
method return time=1511273024.931635 sender=org.freedesktop.DBus -> destination=:1.1 serial=45 reply_serial=71
signal time=1511273024.932142 sender=:1.1 -> destination=(null destination) serial=72 path=/org/bluez/hci0; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
string "org.bluez.Adapter1"
array [
   dict entry(
      string "Discovering"
      variant             boolean false
   )
]
array [
]

bluetoothctlscan on一起使用时,我可以执行蓝牙扫描。我试图比较bluetoothctl工具和我的bash之间的dbus消息。当我在bash中使用dbus时,我收到很多信号NameOwnerChanged后面跟着RemoveMatch,这引起了我的兴趣....

其他dbus方法有效(ListNamesGetManagedObjectsConnectRemoveDevice) 我看到这篇文章有同样的问题:How to use dbus-send to call org.bluez.Adapter1.StartDiscovery?

但我不知道如何将D-Bus代理对象保持在适配器上。"

你有任何线索吗?

我使用:dbus-daemon 1.10.10,Bluez 5.41

感谢您的时间

2 个答案:

答案 0 :(得分:0)

我很确定你不能用dbus-send或shell来做这件事:在你的例子中,一旦方法调用返回,工具就会断开连接并退出(这在扫描真的开始之前就会发生) )。然后Bluez注意到客户端不再存在并假设没有人对此感兴趣并停止扫描。

我建议使用正确的编程语言:如果您打算使用像Bluez这样复杂的DBus API执行任何非常重要的事情,那么无论如何都需要这样做。像python这样的脚本语言如果有足够好的D-Bus绑定就可以正常工作。 Bash几乎肯定没有它们:尝试在shell中表达复杂的D-Bus签名会非常痛苦,而且可能是不可能的。

答案 1 :(得分:0)

你不能用dbus-send做到这一点。在这种情况下,Bluez在发送方上添加了断开连接观察程序。您的应用程序应该获取DBus连接,并且应该运行以执行发现。

尝试Bluez源目录中的test / test-discovery python脚本。它做你想要的。如果您不想添加任何Discovery过滤器,可以在python脚本的主要行下面注释并运行它。

       adapter.SetDiscoveryFilter(scan_filter)
# after commenting, run it with: python test-discovery