Score:0

How do I listen for bluetooth events using Dbus?

ma flag

I'm developing a bluetooth applet for cinnamon and I want to have a way to toggle the bluetooth and do other basics like pairing, connect/disconnect etc

I've tried exploring the dbus protocol bluez offers but i couldn't figure out how to listen for power on/off events and how to actually issue commands using dbus, if that's even possible... :(

Score:1
om flag

It's possible.

You can explore the tree using

busctl tree org.bluez

This will show you the node tree. You will have to enumerate the devices, and from that list select status for each one of them, and possibly enable/disable depending on your goals.

The actual endpoints for adapters is listed in the BlueZ DBUS documentation.

Further information can be found at https://ukbaz.github.io/howto/python_gio_1.html#dbus, which is geared towards Python, but should show you the basics. The code is fairly basic, so porting it to any language should be more or less trivial.

samawaat avatar
ma flag
I'm gonna give this a try soon and report back with the results.
Score:-1
it flag

Read man hcitool bluetoothd bluetoothctl;man -k bluetooth;man -k bluez.

If you find one with functions you like, take advantage of the Open Source nature of GNU/Linux!

An example, for hcitool:

# Don't do this in your $HOME, each package should have its own subdirectory
# determine the package containg the executable 
dpkg -S "$(type -p hcitool)"
pkg="$(dpkg -S "$(type -p hcitool) | head -n 1 | cut '-d:' -f1)"
# Retrieve the source
apt-get source "$pkg"
/bin/ls -l "${pkg}*"

Here's an explanation of what can be done with the "source".

In general, this process is called "building from source", and goes like this (assuming you have done sudo apt install build-essential):

  • Download (apt-get source ...) the archive (the .tar.gz). Put the file somewhere you can write, and on a filesystem with enough space. In the end, this file is disposable.
  • Inspect the archive (tar tzvf ..., see man tar). Most unpack into a subdirectory. For the few that don't, mkdir your own subdirectory, mv the .tar.gz into it, and cd there.
  • Unpack the archive (tar xzvf ...).
  • Read the files with all upper case names (README*, INSTALL, ... ). They contain complete instructions, and override anything I type after this. Read the source (usually ./src/*) to understand the program.
  • If there's a configure script, run it (./configure). configure takes many options, see the INSTALL file or ./configure -h. If configure succeeds, it will construct a Makefike.
  • make compiles and links what is necessary. It produces an executable that can be tested before installation.
  • This is the only step requiring root privileges: sudo make install. This command copies the executable, man pages, run-time necessities, to the "right" places.
  • Once sudo make install has worked, the whole source directory is redundant, and may be deleted.

As always, YMMV.

Explore http://www.bluez.org/

vidarlo avatar
om flag
How does this answer the question?
waltinator avatar
it flag
@vidarlo I told OP how to look through existing bluetooth tools, find one that does the function OP is looking for, how to find the Ubuntu package and download the source for the program. I told OP how to unpack the downloaded source, and evrn how to build it. OP can read the source, and see how other programmers did bluetooth things, which libraries it uses. No, it's not a "click here" Answer, rather it's a method that can be used to learn how things work.
vidarlo avatar
om flag
But he's asking about dbus interface, not bluetooth tools. Furthermore, pointing someone to the source, without pointers to relevant parts of source, is next to useless. Usually it's *far* more relevant to read documentation.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.