Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bt-agent in Bluez-5.37 #22

Open
PraneetKaur opened this issue Jan 16, 2018 · 5 comments
Open

bt-agent in Bluez-5.37 #22

PraneetKaur opened this issue Jan 16, 2018 · 5 comments

Comments

@PraneetKaur
Copy link

Hi,
I am using Bluez 5.37 and I would like to use agent for setting the capability and authorizing the device.
For example: "agent -c NoInputNoOutput 0000 &" in Bluez 4.101.
However, bluetoothctl is providing the NoInputNoOutput capability using the agent option but the pin as well is needed. I came across bt-agent functionality in Bluez 5.37 and it is same as agent in Bluez 4.101.
I would like to know how to generate the executable for the bt-agent since my end application is on OpenWRT platform. I have been analysing the Makefile for Bluez 5.37 and blutoothd-agent is being enables at several places.
However, I am unable to get clarity on this issue. Any inputs on the same would be appreciated.

--
Thanks

@keaton-freude
Copy link

keaton-freude commented Jan 16, 2018

Hi @PraneetKaur

I have had to do a very similar thing. Some info you may find helpful:

You can get the "NoInputNoOutput" behavior manually through bluetoothctl using:
$ bluetoothctl
agent NoInputNoOutput
default-agent

While bluetoothctl is open, all incoming pair requests will be auto approved. However, when you close bluetoothctl the agent will be unregistered (all pair requests will fail with a connection error).

The solution is to register an agent outside of bluetoothctl. If you are familiar with dbus, the API is located here:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/agent-api.txt

The bt-agent executable out of this project can provide this for you.

$ bt-agent --capability=NoInputNoOutput &
This will run the bt-agent in background and auto-accept all pairing requests.

This tool is not provided out of BlueZ, but will work with most (probably all?) versions of BlueZ 5 or higher.

If you have working build tools on your system, you can simply build and install this project according to instructions. I am doing so with:
$ git clone https://github.com/khvzak/bluez-tools.git
$ cd bluez-tools
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

If not, you could always compile on another system if the architecture is the same, or cross compile to correct architecture.

If compiling is not an option and are willing to write the agent yourself, there is a great example out of the bluez source tree located here: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/simple-agent

You can adapt that script to auto-accept pairing requests, return a hard-coded PIN and anything else really.

If you have further questions, just let me know. I know BlueZ 5+ information can be hard to find :)

Keaton

@PraneetKaur
Copy link
Author

Hi Keaton,
Thank you so much for the well thought solution.
With the agent capability using bluetoothctl, I am able to connect but the concern is that I need to authorise using the yes/no prompt upon connection request. The requirement is to connect to bluetooth without any interruption at the host side.
I was able to compile and run bt-agent in my Ubuntu machine. However, now I want to port this functionality to OpenWRT platform. It would like to cross compile for target-arm_cortex-a7_uClibc-1.0.14_eabi architecture specifically. I am facing issues with libraries while trying to compile from the OpnWRT makefile. Can you please guide me for cross compilation since I am somewhat new to this.
I am also attaching two images of agent in bluez 4.101 with OpneWRT and bt-agent as suggested by you on linux platform.
Thanks again for offering to support.

bluez4_agent
bluez5_agent

@keaton-freude
Copy link

keaton-freude commented Jan 17, 2018

Hi @PraneetKaur ,

Can you clarify if you are targeting BlueZ 4 or are you targeting BlueZ 5?

The methods you use to interact with BlueZ 5 are entirely different than BlueZ 4.

If you are targeting BlueZ 5+ on your OpenWRT platform, then read on. If not, then I'm afraid I do not know how to help with BlueZ 4. A big part of my day job is automating BlueZ 5+, so I can definitely help there.

BlueZ 5 Information
Setting up a cross compiler can be a tricky thing, and unless someone has created a cross compiler toolchain you can just pull down from somewhere, you normally need to be willing to dive deep on the whole thing. There is an easier solution, which I'll mention later in this response.

When a remote device is asking to connect to a given service, the default agent behavior is to ask the user to authorize the service. This is typically done with a yes/no prompt in bluetoothctl. If you have trusted the remote device, then your host will automatically accept service-level connections.

To automate this you have two straight forward options:

  1. You can Trust the device after pairing has completed. If you just have one or two devices and don't plan on adding more you can do this manually through bluetoothctl (and never remove the pairing).
  2. Register an agent with BlueZ which auto-accepts service connection authentication requests (along with pairing requests).

Automation is fun, so lets do option 2.

Basically we will need to implement the org.bluez.Agent1 interface and register our dbus implementation with the org.bluez.AgentManager1 interface.

An easy way to implement dbus interfaces is with python. The BlueZ project has a very useful implementation of this in their tests. https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test/simple-agent

What we can do, is take this python file and modify it for our needs. We will basically be removing all of the user input requests and just automatically agree to all requests.

Here is a gist that should work: https://gist.github.com/keaton-freude/acf87f4b8635d5e3db65cbe01a25e9a2

WARNING: This sets up your Bluetooth host machine to auto accept all pairing and service connections, removing a good deal of security surrounding Bluetooth & BlueZ.

Simply create a file on your device, copy the text in. Save file as something like 'auto-agent.py' (name is whatever you want).

Run the program with:
$ python /path/to/file &
This will run it in the background. You will still see a good amount of output you can always redirect this away.
You can also make the file executable with chmod and execute it like any other program.

By the way, there is some additional stuff and side effects in this python file that I haven't fully tested. We can strip it down much further to only do the pairing and service auto-accepts, but wasn't comfortable removing lots of code without having a machine to run it on :)

NOTE: I haven't fully tested this as I modified it just now and am away from my test devices. I will test in the morning (PST time) and update with anything else. There may be additional packages which must be installed. If there are additional packages python will complain about not being able to import something or another and these errors are typically googlable. If you have a package manager on your system you can normally find these python packages there.


If you are on BlueZ 4, then my knowledge is rather limited. But, typically, BlueZ4 info is much easier to google, as BlueZ 5 is a good deal newer and significantly changed how users interact with Bluetooth on their system.

Hope this helps,
Keaton

@PraneetKaur
Copy link
Author

PraneetKaur commented Jan 17, 2018

Hi,
Just for clarification, I am targeting Bluez5. Bluez 4 snapshot was attached for reference since I was able to get the agent functionality working on OpenWRT with bluez4 by making use of makefile configurations. The cross compilation for Bluez 4 for agent functionality is quite straight forward but for Bluez5 there are some challenges. In the Bluez 4 snippet shared, I am running the agent process in background to automate the connection process.

Mainly I am looking for a way to solve cross compilation issues in Bluez 5 but I will also look into the other approach suggested and keep you posted.

..
Thanks
Praneet

@CalvinYou
Copy link

CalvinYou commented Mar 31, 2018

Hi,

  1. I want to put the simple-agent to my arm-based target to control pairing options. There is simple-agent in bluez5 source in test folder but it's not compiled. How to include it to compile? I looked in bluez5 make files and so on but it's too complicated for me. (I'm using yocto build system)
  2. If the simple-agent embedded into my target system, how to execute the agent from QT Bluetooth app? Is it possible to execute shell command to start simple-agent from QT app?

Thanks,
Calvin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants