Giter VIP home page Giter VIP logo

Comments (5)

keaton-freude avatar keaton-freude commented on August 15, 2024

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

from bluez-tools.

PraneetKaur avatar PraneetKaur commented on August 15, 2024

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

from bluez-tools.

keaton-freude avatar keaton-freude commented on August 15, 2024

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

from bluez-tools.

PraneetKaur avatar PraneetKaur commented on August 15, 2024

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

from bluez-tools.

CalvinYou avatar CalvinYou commented on August 15, 2024

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

from bluez-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.