How to start an application at a Bluetooth connection of a device?

I want to use the top button of my Bamboo Ink pen, which triggers a Bluetooth connection when being presses and closes the connection when released. I already got this working, but there are a few issues. I have made a udev rule that starts a script on a bluetooth connection event.

ACTION=="add" \
, SUBSYSTEM=="bluetooth" \
, ATTRS{idProduct}=="0002"\
, ATTRS{idVendor}=="1d6b"\
, ENV{DISPLAY}=":0" \
, ENV{HOME}="/home/user/" \
, RUN+="/home/user/startScript"

This script calls the actual script to prevent queuing of the udev events and sets the right locale (if necessary)

    export LC_ALL=de_DE.UTF-8
    echo /home/user/runXournalpp | at now 

The script starts Xournal++ and maps the Wacom tablet to the Laptop monitor. If called a second time it maximizes Xournal++.


export DISPLAY=":0"

  if pgrep xournalpp
      echo Xournal++ is running
	  wmctrl -x -a com.github.xournalpp.xournalpp
	  wmctrl -x -r com.github.xournalpp.xournalpp -b "add,maximized_vert,maximized_horz"

	  echo Xournal++ is not running
	  xsetwacom --set "Wacom Pen and multitouch sensor Pen stylus" MapToOutput "eDP1"
	  xsetwacom --set "Wacom Pen and multitouch sensor Pen eraser" MapToOutput "eDP1"
	  xsetwacom --set "Wacom Pen and multitouch sensor Finger touch" MapToOutput "eDP1"

Three issues remain:

1. Udev also triggers an event when waking from sleep mode.
2. It causes graphical glitches in Vivaldi, resulting in flickering colors of the tab bar. Haven’t found another app that has these glitches. No idea why this happens.
3. “Recent documents” are not shown in the application. It seems that the app is loaded by a different user.

So I am wondering if there is a better way to realize this. This is a pretty hacky solution. I guess there is some command sent via Bluetooth that I could find out with Wireshark. I don’t know how I would make the OS read and react to that command though.

Seems like you’re on the right track and I don’t know of a “better” way than udev

The only thing I would do differently is that I would set a global environment variable that the script is already running:

If bXJournalppAlreadyRunning exit
export bXJournalppAlreadyRunning = true
export bXJournalppAlreadyRunning = false

But that’s just an opinion:man_shrugging:

The exact same event? :scream:

Firefox with NoScript and all cookies turned off except the sites you allow to set cookies a solution?

Good stuff! :+1:


I’m trying to launch vlc/audacious when my bt speaker connects to automatically play radio playlist. My udev rule is very simillar to the one above and it will launch my application. But that’s where my problems begin. Vlc/Audacious is shown without any theming, network & audio do not work etc.

my udev rule now looks like this:

ACTION==“add”,SUBSYSTEM==“input”,ATTR{phys}==“a0:c5:89:aa:c3:27”,ENV{DISPLAY}=":0",ENV{XAUTHORITY}="/home/xxxx/.Xauthority",ENV{HOME}="/home/xxxx/",RUN+="/bin/su xxxx -c ‘export DISPLAY=:0; export XAUTHORITY=/home/xxxx/.Xauthority; vlc ~/radio.pls’"


How does this help the original poster (OP) :question:

I’m sorry, but I thought the idea of forums is to help community not only some users. And since my problem is exactly what topic title says and my problems are probably somewhat related I posted in this topic. Or do I have to open another topic with exactly the same title?

I guess so. It probably looks for the pen when waking-up. Haven’t investigated further.

I don’t understand. I am not going to change my browser because of this.

You are probably opening vlc as root by using XAUTHORITY and thus the theme is gone. Use a script to start vlc with the locale as in my example. Then call this from udev with ENV{HOME}. This might result in queuing of the events. It is really tricky. I think udev wasn‘t designed for running graphical applications.

I guess my title is too general. I have a specific problem that requires a specific solution. Our problems are similar though. I don’t mind you posting it here as well.

1 Like

My apologies, but that is considered Thread hijacking here and you just:

Note: this is the #support:applications category as you can see in the upper left corner…


I was just asking if FF would be a solution or not. Apparently it isn’t…


@Fabby: I found the solution to the udev triggering on wakeup. Seems I didn’t specify the device enough as you suspected. When you gather device information with udevadm info -ap /dev/path, you need to take the entry at the top. All the other following a parent devices apparently. It though that using ATTRS{idVendor} and ATTRS}idProduct} would be the most specific but I was wrong.

This is my rule now:

ACTION=="add" \
, SUBSYSTEM=="bluetooth" \
, KERNEL=="hci0:3585" \
, ENV{DISPLAY}=":0" \
, ENV{HOME}="/home/user/" \
, RUN+="/home/user/startScript"

Now I only need to find a solution for the graphical glitches in Vivaldi and well a solution to why the “recent documents” are not loaded.