I had a similar problem. I solved it by listening to dbus-monitordbus-monitor and running my script under a particular bluetoothBluetooth event. In particular,
dbus-monitor "interface='org.blueman.Applet',member='MenuChanged'"| while read -r line; do
notify-send 'my bluetooth keyboard has just connected! High time to run my script'
./my_script.sh
done
dbus-monitor "interface='org.blueman.Applet',member='MenuChanged'"| while read -r line; do notify-send 'my bluetooth keyboard has just connected! High time to run my script' ./my_script.sh done