Cyfell
Cyfell

Reputation: 13

How do you perform bluetooth discovery through dbus?

Im beginner with dbus and trying to perform bluetooth scan with bash dbus-send command. I use this line :

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

but no discovery starts...

In dbus-monitor I see :

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 [
]

When I use bluetoothctl with scan on I can perform bluetooth scan. I tried to compare dbus messages between bluetoothctl tool and my bash. When I use dbus in bash I receive a lot of signals NameOwnerChanged always followed by RemoveMatch which intrigues me....

Other dbus methods works (ListNames, GetManagedObjects, Connect, RemoveDevice) I saw this post with the same problem :How to use dbus-send to call org.bluez.Adapter1.StartDiscovery?

but i don't know how to "keep the D-Bus proxy object to the adapter alive."

do you have any leads ?

I work with : dbus-daemon 1.10.10, Bluez 5.41

Thanks for your time

Upvotes: 1

Views: 4222

Answers (2)

Ravi
Ravi

Reputation: 1066

You can not do this with dbus-send. Bluez adds a disconnect watcher on sender in this case. Your application should acquire a DBus connection and should be running for performing discovery.

Try test/test-discovery python script from Bluez source directory. It does what you want. If you don't want to add any Discovery filters, you can comment below line in the main of the python script and run it.

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

Upvotes: 1

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14607

I'm pretty sure you can't do this with dbus-send or shell in general: In your example the tool will disconnect and exit as soon as the method call returns (and that happens before the scan has really even started). Then Bluez notices that the client is no longer there and assumes no-one is interested anymore and stops the scan.

I suggest using a proper programming language: you'll need to do it anyway if you intend to do anything non-trivial with a complex DBus API like Bluez. A scripting language like python works fine if it has good enough D-Bus bindings. Bash almost certainly does not have them: trying to express a complex D-Bus signature in shell would be extremely painful and probably impossible.

Upvotes: 1

Related Questions