haylocki
Posts: 11
Joined: Tue Nov 11, 2014 7:24 am

Custom bluetooth agents

Thu Jan 05, 2017 5:11 pm

Hi,
I have modified the example bluetooth agent code, and now I wish to use my modified agent rather than the default agent.
How do I do this?

The agent is located at /usr/local/bin/autoAgent
(I know it's not the proper place to put a python script :) ).

I have tried

Code: Select all

bluetoothctl -a /usr/local/bin/autoAgent

but this just gives me the following error:
Failed to register agent: org.bluez.Error.InvalidArguments

Cheers, Ian

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Custom bluetooth agents

Thu Jan 05, 2017 6:25 pm

No idea how (or if) you can get bluetoothctl to run your code. Why not just run it from the command line?

haylocki
Posts: 11
Joined: Tue Nov 11, 2014 7:24 am

Re: Custom bluetooth agents

Thu Jan 05, 2017 6:44 pm

Hi, thanks for the reply.

I thought that as you have the "default-agent" command in bluetoothctl, you could load your own agent.

I am trying to get my Pi to pair with and trust my Android phone automatically.

I have modified the agent thus:

Code: Select all

	def RequestConfirmation(self, device, passkey):
		#### my code
		set_trusted(device)
		return
		#### my code
		print("RequestConfirmation (%s, %06d)" % (device, passkey))
		confirm = ask("Confirm passkey (yes/no): ")
		if (confirm == "yes"):
			set_trusted(device)
			return
		raise Rejected("Passkey doesn't match")

def RequestAuthorization(self, device):
		#### my code
		return
		#### my code
		print("RequestAuthorization (%s)" % (device))
		auth = ask("Authorize? (yes/no): ")
		if (auth == "yes"):
			return
		raise Rejected("Pairing rejected")
Which I believed would remove the necessity for me to enter a pin, and trust the phone manually.
But if I run bluetoothctl and turn on the power, pairing, and discoverability.
Then exit bluetoothctl and run my modified agent.
Then try to pair on the phone I get an incorrect pin number error.

Cheers, Ian

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Custom bluetooth agents

Thu Jan 05, 2017 7:45 pm

I believe you'll need to modify requestPinCode() and/or requestPasskey(). Some dongles use one, some the other.

I have some code that (among other things) acts as a pairing agent and also makes the Pi discoverable: https://github.com/Douglas6/blueplayer/

haylocki
Posts: 11
Joined: Tue Nov 11, 2014 7:24 am

Re: Custom bluetooth agents

Thu Jan 05, 2017 8:50 pm

Sigh....

This used to be so easy.

I have removed my modifications from the agent.
Running the agent causes the terminal to display "Agent registered".

Trying to pair still fails with the pin / passkey error, but unlike the agent built into bluetoothctrl I don't get any messages on the terminal.

Now I would expect the same messages as when using the built in agent. So I'm wondering if my agent is really running, and how to check if it is.

Cheers. Ian

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Custom bluetooth agents

Fri Jan 06, 2017 2:02 am

Not sure what to tell you. Make sure the devices are removed from both sides. When I try to connect from an Android phone, while running the BlueZ simple-agent, and trying to connect from the phone, I get an 'authorization yes/no' on the Pi. Answering 'yes' pairs the devices.

This on a Pi3.

haylocki
Posts: 11
Joined: Tue Nov 11, 2014 7:24 am

Re: Custom bluetooth agents

Fri Jan 06, 2017 7:20 am

Hi, thanks for your help. I will try re-downloading the simple-agent script, and see what happens with that.

I do have some code that uses blutoothctl that I managed to get to pair automatically but still required me to trust manually. If all else fails I'll add the code to automatically trust devices.

Just seems like a lot of work for something so simple.......

[edit]

OK so I tried a fresh copy of simple-agent, and it worked as you said. Go figure.
Then I edited the agent and auto pairing and trusting worked!
I now need to figure out how to force the pairing to use the passcode / pin number option so I can change the code to bypass that check. Think I need to enable legacy pairing.

[edit 2]

Wow! That was easier than I expected.

Code: Select all

hciconfig hci0 sspmode 0
turns on legacy mode.
then I simply edit simple-agent to return whatever passcode I wish to use, rather than asking the user to input a value.

Cheers, Ian

ibrahimuslu
Posts: 2
Joined: Fri Jan 05, 2018 1:13 pm

Re: Custom bluetooth agents

Fri Jan 05, 2018 1:17 pm

I am sorry but i can not find how to change default-agent with simple-agent

do i need to recompile bluez for this? (https://github.com/r10r/bluez/tree/master/test)

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Custom bluetooth agents

Fri Jan 05, 2018 2:31 pm

Simply run simple-agent. It will make itself the default.

ibrahimuslu
Posts: 2
Joined: Fri Jan 05, 2018 1:13 pm

Re: Custom bluetooth agents

Fri Jan 05, 2018 4:08 pm

Thank you.
omg! it is only a python script.
I didnt think that it was simple as this. :)

While i running i faced with some problems, so for those who want to run this script i want to inform that

before running script dont forget to install PyBluez

Code: Select all

 pip install PyBluez 
and

dont forget to download also bluezutils.py to the where you are running simple-agent script

Code: Select all

 wget https://raw.githubusercontent.com/pauloborges/bluez/master/test/bluezutils.py

step by step guide how to use this script is here:
https://www.linuxquestions.org/question ... ost5149524

User avatar
Douglas6
Posts: 4861
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Custom bluetooth agents

Fri Jan 05, 2018 4:24 pm

You don't need PyBluez to run simple-agent. You do need the bluez-utils.py module to run simple-agent and several other of the BlueZ test/ scripts, which are all in Python.

Return to “Advanced users”