The problem seems to be more on the lower level:
When I run the script and use inside it a sudo command:
if /usr/bin/sudo /usr/bin/l2ping -c 1 $MAC
to check for the BT devices, I get the response:
sudo: no tty present and no askpass program specified
Any idea what this means?