Stavo cercando un software per il mio nokia che facesse partire il lettore musicale automaticamente all'aggancio con le mie cuffie o il mio stereo bluetooth.

Beh non l'ho trovato, in compenso ho trovato questo script che permette di bloccare e sbloccare il mio ubuntu a seconda della distanza del mio cellulare. Non c'entra nulla, ma è utile lo stesso :)

Lo script così com'era non mi soddisfaceva così ho deciso di dargli un ritocco: il mio primo ritocco a uno script bash!

Le differenze con lo script originale:

  • Lo script "attiva" l'algoritmo di lock-unlock solo dopo la prima connessione, in questo modo accendendo il pc senza il cellulare vicino non ci si trova subito lockati.
  • Ho aggiunto 2 chiamate a festival in modo che il pc mi accolga e mi saluti parlando (ovviamente richiede festival ).
  • Siccome davanti al pc ho sempre la musica accesa ho aggiunto una chiamata al tasto multimediale play/pause al blocco e allo sblocco (richiede xte dal pacchetto xautomation). NB Teoricamente può dare dei problemi se il player è in pausa mentre ci si allontana, ma sperimentalmente sembrerebbe non darne.
  • Ho rimosso il dialog che usciva allo sblocco del pc.
  • Ho aggiunto un po' di permissività, a volte la connessione a me fallisce anche se telefono e pc sono appiccicati, per cui blocco il pc solo dopo N tentativi falliti.
  • Ho rimosso FAR_CMD e NEAR_CMD in favore di due funzioni far_execution e near_execution, qui è dove ho aggiunto il play/pause e la chiamata a festival. Dovrebbe essere facile ritoccare i metodi in base ai propri gusti.
  • Ho dato un tocco di refactoring...gusti personali :)
Ho aggiunto un singolo parametro di configurazione ( FAILURES_THRESHOLD ) per impostare il numero di tentativi falliti prima di bloccare il pc.

Eccoci finalmente allo script:

#!/bin/bash
#set -o verbose sh -v
# Copied from Steven on http://gentoo-wiki.com/Talk:TIP_Bluetooth_Proximity_Monitor
# Modified By Jamie Paton
# Modified By Michele Marcucci http://www.michelem.org
# Modified By Mone http://www.simonefabiano.com

# These are the sections you'll need to edit

# You'll need to use the MAC address of your phone here
# Use "hcitool scan" to find the MAC of your device
DEVICE="00:00:00:00:00:00"

# How often to check the distance between phone and computer in seconds
CHECK_INTERVAL=5

# The RSSI threshold at which a phone is considered far or near
THRESHOLD=-8

# Number of failed connections before declaring the device "far"
FAILURES_THRESHOLD=2

# The commands to run when your phone gets too far away
function far_execution {
#click the play/pause button
xte "key XF86AudioPlay" > /dev/null 2>&1

#tell me bye bye
festival -b "(SayText \"bye bye master\")" > /dev/null 2>&1

#lock the pc
/usr/bin/gnome-screensaver-command --activate -l > /dev/null 2>&1
}

# The command to run when your phone is close again
function near_execution {
#show the login window
#/opt/gnome/bin/gnome-screensaver-command --poke > /dev/null 2>&1

#unlock the pc
/usr/bin/gnome-screensaver-command --deactivate > /dev/null 2>&1

#tell me welcome
festival -b "(SayText \"Welcome back master\")" > /dev/null 2>&1

#click the play/pause button
xte "key XF86AudioPlay" > /dev/null 2>&1
}



HCITOOL="sudo /usr/bin/hcitool"
DEBUG="/tmp/btproximity.log"

connected=0
state="near"
failures=0



function msg {
echo "$1" >> "$DEBUG"
# Uncomment line below if you want debug on console too instead only to file $DEBUG
#echo "$1"
}

function check_connection {
connected=0;
found=0
for s in `$HCITOOL con`; do
if [[ "$s" == "$DEVICE" ]]; then
found=1;
fi
done
if [[ $found == 1 ]]; then
connected=1;
else
msg 'Attempting connection...'
if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
msg 'Connected'
connected=1;
else
if [ -z "`l2ping -c 1 -t 2 -i $DEVICE 2>&1`" ]; then
if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
msg 'Ping is good!'
connected=1;
else
msg "ERROR: Could not connect to device $DEVICE."
connected=0;
fi
fi
fi
fi
}

function is_far {
if [[ "$state" == "near" ]]; then
let "failures += 1"
msg "*** Device \"$name\" [$DEVICE] has left proximity, failures = $failures"
if [[ $failures -ge FAILURES_THRESHOLD ]]; then
#i had problems because sometimes the connection fails even if the mobile is near. Let the connection fail more times before declaring the mobile as "far"
state="far"
far_execution
fi
fi
}

function is_near {
#reset the number of failures
failures=0
if [[ "$state" == "far" ]]; then
msg "*** Device \"$name\" [$DEVICE] is within proximity"
state="near"
near_execution
fi
}


#don't start the lock-unlock mechanism until we don't connect for the first time
while [[ $connected -eq 0 ]]; do
msg "init loop"
check_connection
sleep $CHECK_INTERVAL
done

#TODO why doesn't always work?
name=`$HCITOOL name $DEVICE`

msg "Monitoring proximity of \"$name\" [$DEVICE]";

while /bin/true; do
msg "execution loop"
check_connection

if [[ $connected -eq 1 ]]; then
#we're connected
rssi=$($HCITOOL rssi $DEVICE | sed -e 's/RSSI return value: //g')

if [[ $rssi -le $THRESHOLD ]]; then
#we're connected but far
is_far
else
#we're connected and near
is_near
fi
msg "state = $state, RSSI = $rssi"
else
#we're no more connected
is_far
msg "not connected"
fi
sleep $CHECK_INTERVAL
done




PS: come detto è la prima volta che ritocco uno script bash e non mi son messo a studiare prima di lavorare per cui potrei avere fatto c*****e... nel caso ne vedeste fatemi un fischio (cmq funziona...)