6

I'm trying to run a script automatically right after a certain bluetooth device is connected. To do so, I tried to create a new UDEV rule. The problem I encounter is that my rule doesn't execute upon establishing the connection.

As of now, my rule contains only the MAC address of the device.

ACTION=="add", SUBSYSTEM=="bluetooth", ATTRS{address}=="a0:b1:c2:e3:f4:56", RUN+="/home/myhome/my_script"

How can I find more device details in order to add them to the rule?

When the device is connected, I can see the following UDEV event:

$ udevadm monitor
UDEV  [7704849.324977] add      /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.7/2-1.7:1.0/bluetooth/hci0/hci0:12 (bluetooth)

Is there any information in the above event that I can leverage?

I place the rule in both UDEV rules places, /etc/udev/rules.d/ and /lib/udev/rules.d/, but still it doesn't run. I'm on a Linux Mint system.

The rule has same permissions as all the other rules in the directory:

-rw-r--r-- 1 root root  369 Oct 17 13:07 50-my_bluetooth_rule.rules

I also played around with the rule number between 50 and 90. Still, no success.

  • Have you run udevadm control --reload-rules && udevadm trigger after creating that file? – Ipor Sircer Oct 17 '18 at 19:56
  • @IporSircer Yes I did. Nothing happens. – slybloty Oct 17 '18 at 20:01
  • Your script should start with #!/bin/bash – GAD3R Oct 17 '18 at 20:18
  • @GAD3R yes the script is as such. But the problem is the rule doesn't get run at all. – slybloty Oct 17 '18 at 20:23
1

Use sudo tail -f /var/log/syslog to get the idVendor and the idProduct the write the udev rule to /etc/udev/rules.d/50-my_bluetooth_rule.rules as follows:

ACTION=="add" , SUBSYSTEM=="usb", ATTR{idVendor}=="xxx", ATTR{idProduct}=="yyy", RUN+="/home/myhome/my_script"

Run:

sudo service udev restart
sudo tail -f /var/log/syslog

Reconnect your bluetooth device. You can use logger in your bash script to write messages to syslog.

| improve this answer | |
  • 1
    Your answer guided me to the root problem. It wasn't the inability to pick up the device when connected. The issue was that my script had to be run as my user not root in order to render the desired output. – slybloty Oct 18 '18 at 19:57
0

In my case, the issue was that I had to use ATTRS{name} rather than ATTRS{address}. I found this out by issuing:

udevadm info -ap /devices/virtual/input/input160/event6

More info here.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.