2

I'm trying to automate a task to configure a bluetooth device when it's connected to my raspberry pi 3 running raspbian.

Assuming $x is the address of the device, I need to execute the following commands after it connects:

hcitool enc $x off
hcitool sr $x slave

The initial bluetooth connection info (from running hcitool con) looks like this:

Connections:
    > ACL $x handle 11 state 1 1m MASTER AUTH ENCRYPT

And after running the above two commands, hcitool con shows the desired state:

Connections:
    > ACL $x handle 11 state 1 1m SLAVE AUTH

I was able to get a basic udev rule to execute a script, but it doesn't have the desired effect.

Here's the udev rule at /etc/udev/rules.d/99-slave.rules

SUBSYSTEM=="hidraw", RUN+="/home/pi/fix-bluetooth.bash"

And the script:

#!/bin/bash
hcitool con >> /tmp/hello_world

Then I tail -f /tmp/hello_world and plug/unplug my bluetooth device and this is what I see:

Connections:
Connections:
Connections:
...

etc.

I've tried adding sleep for a few seconds. I've tried adding sudo. I've tried it with multiple bluetooth devices attached. I can never get hcitool con to output anything when executed as part of a udev rule.

2
  • Did you resolve the problem? I have same issue – Dcow Sep 23 '19 at 18:02
  • I ended up with a cron job to do the same instead of a udev script. – stevesdj Sep 24 '19 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.