how to use org.bluez.AudioSource GetProperty - c

how can i check weather the given device is connected. using org.bluez.AudioSource GetProperty
I am using c and DBus,can anybody help me to get out of this...

I assume you've been looking into the BlueZ D-BUS API which is a precious documentation for everything related to listening on BlueZ's signals and query information. You can find it in any BlueZ source in the doc/ folder.
To check if a device is connected, you first need to get all the Bluetooth devices on your computer and their paths using D-BUS, I will not provide any C documentation as you can easily find plenty of examples about this using Google. I will instead show you what D-BUS calls you can make via dbus-send to get such an information.
Get Devices list:
dbus-send --system \
--dest=org.bluez \
--print-reply / \
org.bluez.Manager.GetProperties
This returns an array of adapters with their paths.
Once you have these path(s) you can retrieve the list of all the Bluetooth devices paired with your adapter(s).
Get paired devices:
dbus-send --system \
--print-reply \
--dest=org.bluez \
/org/bluez/{pid}/hci0 \
org.bluez.Adapter.GetProperties
This gives you the list of paired devices within the Devices array field.
Once you have the list of devices paired to your Bluetooth Adapter, you can know if it is connected to the AudioSource interface.
Get the devices connected to the AudioSource interface :
dbus-send --system \
--print-reply \
--dest=org.bluez \
/org/bluez/{pid}/hci0/dev_XX_XX_XX_XX_XX_XX \
org.bluez.AudioSource.GetProperties
I find it more to convenient to first try d-bus calls using dbus-send because the D-BUS C API is a bit confusing and inconvenient to use.

Related

Bluez: where can i find all the definitions documented in /doc?

i want to learn about bluetooth programming in C.
After some research i found this linked on the Bluez official website. https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/
since iam not familiar with this linux type of documentation i cant really get much out of it but i made some assumptions.
my first try was to look at the first function in doc/adapter-api.txt and there is no mention where the functions are located.
so i searched for it in every /lib file and could not find anything.
so where can i find all the definitions documented in /doc?
BlueZ is the Bluetooth stack for Linux and it has a few API's for people to use. Below is a list of the possible API’s starting from lowest level and going to the highest. For most people, the higher the better.
Raw HCI
This bypasses the BlueZ bluetoothd that is running on a Linux system that is used to coordinate interactions with the Bluetooth hardware. This API is outside of the BlueZ infrastructure so is not documented by the BlueZ project. All the information is available in the Bluetooth Core Specification which runs to about 3,256 pages for the 5.2 version of the spec.
If using this level it is a good idea to stop the Bluetooth service from starting the Bluetooth daemon so they are not conflicting. There is no safety net with HCI so you really, really need to know what you are doing.
MGMT Socket
The BlueZ Bluetooth Mamagement API is the next step up and the lowest level that the BlueZ developers recommend. This looks quite "HCI" in its API but it commnicates with the bluetoothd which issues HCI commands to the hardware.
This is documented at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt
D-Bus API
This should be the go to level for most people wanting to interact with the BlueZ API’s. The commands interact with the bluetoothd which issues HCI commands to the hardware.
The good point of a D-Bus API is that it is language agnostic. However, it seems the number of people that are familiar with D-Bus is a relatively small and so it is another level of indirection to learn before using the API.
Most popular programming languages appear to have bindings to D-Bus. Some of the bindings are listed at:
https://www.freedesktop.org/wiki/Software/DBusBindings/
The BlueZ D-Bus API is split across multiple documents depending what piece of functionality is required. For interacting with the Bluetooth Adapter it is documented at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
And the device API is at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
etc...
Before diving in to coding it might be useful to experiment with interacting with the BlueZ bluetoothd through the D-Bus API. This can be done with various command line tools. I'm going to assume that you will be using the gdbus library for your C code so that seems like a good choice to experiment with on the command line.
The BlueZ D-Bus API for the Linux Bluetooth adapter is probably the easiest to get started with.
The documentation for this API is at:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
At the top of the document it says what the D-Bus Service and Interface is. And that the object path can be variable.
Service org.bluez
Interface org.bluez.Adapter1
The bluetoothd is communicating on the D-Bus System bus.
D-Bus has a GetManagedObjects method that we can use to report all the things BlueZ knows about. To list all this information about BlueZ use:
$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects
This is usually a lot of information so let's use grep to find the object path for the adapter:
$ gdbus call --system --dest org.bluez --object-path / --method org.freedesktop.DBus.ObjectManager.GetManagedObjects | grep -Pio "/org/bluez/hci.*Adapter1"
/org/bluez/hci0': {'org.freedesktop.DBus.Introspectable': {}, 'org.bluez.Adapter1
So we can now see that (for me) the D-Bus object path is /org/bluez/hci0. I can introspect this now:
$ gdbus introspect --system --dest org.bluez --object-path /org/bluez/hci0
Now I have the Service, Interface and Object Path I can call methods as documented by BlueZ. For example to find what available filters that can be given to SetDiscoveryFilter:
$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.bluez.Adapter1.GetDiscoveryFilters
(['UUIDs', 'RSSI', 'Pathloss', 'Transport', 'DuplicateData'],)
To get all the properties on the Adapter then we can use the GetAll method (that we can see from the introspection) is on the org.freedesktop.DBus.Properties interface. A call example:
$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.GetAll "org.bluez.Adapter1"
To get the value of one property we use Get:
$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Get "org.bluez.Adapter1" "Powered"
To set the value of a property we use Set:
$ gdbus call --system --dest org.bluez --object-path /org/bluez/hci0 --method org.freedesktop.DBus.Properties.Set "org.bluez.Adapter1" "Powered" "<boolean true>"
The following looks like a useful introduction to doing some of this in C:
https://www.linumiz.com/bluetooth-list-devices-using-gdbus/
There is a talk "Doing Bluetooth Low Energy on Linux" by Szymon Janc that may also be useful background. It is available on YouTube:
https://youtu.be/VMDyebKT5c4
And a PDF of the slides:
https://elinux.org/images/3/32/Doing_Bluetooth_Low_Energy_on_Linux.pdf

How can i receive a pairing request event via dbus, glib?

I'm currently making a Bluetooth app with BlueZ
I need to get a Pairing request.
Using BlueZ DBus API, i've made my Agent into NoInputOutput mode, and i succeed the first pairing with my phone.
However, if i delete the linux device(which uses my bluetooth app that i am developing) from my phone and try to re-connect, i can't pair it together.
I searched Python code examples that...
From BlueZ's DBus API, calling RequestAuthorization method to pair and successes it, then deleting the device and re-pairing works fine.
But in case of smartphone's pairing request, i think i should call RequestAuthorization method WHEN SOME EVENT(S) is received. And i have no idea for that.
Conclusion: How can i get a pairing request event using dbus and glib loop?
I've already checked
bluez pairing before exchange
apparently, gdbus is a combination of dbus and glib.
However, i'm using dbus and glib separated since i can sorted some codes that i do not need.
More, now i think it doesn't have any relations with deleting devices from the list.

Sending Output of Terminal Command via curl using a Slack Webhook

For the sake of context, I am developing a system which would allow "remote" access to an NVidia Xavier. Unfortunately in its current housing, the display port is entirely blocked unless the entire unit is taken apart. A USB port is available, which is being used to connect to a wireless keyboard & mouse. I have already written a Gstreamer pipeline that I can execute via SSH which will capture the display and stream it to another local machine.
The problem I am running in to is, as the unit is portable, the network it is connected to and subsequent IP will be constantly changing, but I need to know it to be able to SSH into the unit.
I am trying to write a (what I thought to be simple) bash script that will send a timestamp and the IP of the unit to a Slack channel on boot via a webhook that is effectively the following command (IP and slack URL generalized):
curl -X POST -H 'Content-type: application/json' --data '{"text":"Tue Jul 14 15:26:50 EDT 2020 IP: XX.X.X.XX"}' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
The JSON payload will be changing each time the script is run, dependent on the results of hostname -I and date.
I have tried creating an array with the desired data in it using:
command=( -X POST -H 'Content-type: application/json' --data \'{\"text\":\")
command+=("$(date)")
command+=("$(hostname -I)")
command+=(\"}\' https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX)
curl command
but receive an error curl: (6) Could not resolve host: command.
I have very little experience writing bash scripts and am likely missing something incredibly obvious to everyone else. However, using echo "${command[*]}" does show an output that matches the working command. Any advice would be greatly appreciated.
This is a common error when using variables in bash - when you set them, you don't prefix them with a $, but when you reference them, they must be prefixed with a $ in order to be expanded. So in your case, you likely want to use: curl $command instead of curl command

Bluetooth Low Energy - Linux with BlueZ

I'm working on a project where I need to establish connection between my computer and a Adafruit feather 32u4 with BLE incorporated.
Due to comments on lots of webs, I decided to use linux to do the job.
I got everything installed and I can connect my BLE with the PC successfully.
----MY CONNECTION PROCEDURE-----
I can even recieve data and send data between them with gatttool.
To connect both devices i use this commands:
sudo hcitool lescan
sudo gatttool -t random -b F6:E5:F4:A7:71:E6 -I
connect
The devices are connected correctly. I can use all the gatttool commands and they respond as expected.
----END OF CONNECTION PROCEDURE----
---MAIN PROBLEM---
I don't know how I could implement all the commands in a packed C program.
I need to be able to manage all the commands in the same program !! without using brute force with system().
It would be great if someone could show me how gatttool do its magic.
I mainly need to know how to get the data from Rx and how to send it to Tx via code not commands.
Note: I can supply any further information if needed.

How to get port information for usb ports using libudev?

For a small project i am using libudev to get notification for USB devices plug-in/plug-out.
Is there any way of knowing which USB port was used to plug in the device via libudev.
Actually there are multiple ports available and it is necessary to know which one was used.
Any hints would be highly appreciated!
Using the lsusb command and doing a grep to find the line with the name of the device. This command will output all sorts of useful information about all connected USB devices. You can also use lsusb -v to get very detailed info.
Check out the manpage for lsusb
http://manpages.ubuntu.com/manpages/hardy/man8/lsusb.8.html
After a bit of a research i found that it is possible using libusb.
From here, libusb can be used to get a list of all devices plugged in , now the devices discovered using libudev can be checked in the list of devices available via libusb_get_device_list.
Further libusb_get_port_number could be used to get the port number for the same device.

Resources