Cleaned up the script a little more.  Switched over to a case statement
from the if/elif, moved some variables to the top and got rid of another
useless cat.

I'm still a little surprised at how well this is working.  I only have
one bluetooth device though (a VModa headset which is really nice BTW),
I'd like to test with more devices.


Code:
--------------------
    
  #!/bin/sh
  
  lcard=15000
  lbt=15000
  lstep=1500
  btplayer="bluetooth-player"
  btspeaker="bluetooth-speaker"
  btsync="bluetooth-sync"
  rssi="-60"
  
  __start_cards()
  {
  if [ $1 ]
  then
  lcard=$1
  fi
  for card in $(cut -d' ' -f3 /proc/asound/cards | tr -d '[' | grep -v Loopback 
| tr -s '\n')
  do
  for i in $(seq 0 7)
  do
  if ! ps | grep -q "[L]oopback,1,${i}"
  then
  break
  fi
  done
  alsaloop -d -C hw:Loopback,1,${i} -P "hw:${card},0" -S5 -l ${lcard}
  done
  }
  
  __watch_bluetooth()
  {
  while :
  do
  if inotifywait -qq -r -e create /dev/input/
  then
  pdevs=$(bluetoothctl paired-devices | cut -d' ' -f2)
  for pdev in ${pdevs}
  do
  if bluetoothctl info "${pdev}" | tr -d '\n' | grep -Eq 'Connected: yes.*Audio 
Sink'
  then
  if ! ps | grep "[D]EV=${pdev}"
  then
  for i in $(seq 0 7)
  do
  if ! ps | grep -q "[L]oopback,1,${i}"
  then
  break
  fi
  done
  alsaloop -d -C hw:Loopback,1,${i} -P 
bluealsa:SRV=org.bluealsa,DEV=${pdev},PROFILE=a2dp -S5 -l ${lbt}
  fi
  fi
  done
  fi
  done
  }
  
  if [ "$1" = "boot" ]
  then
  bluetoothctl discoverable off
  bluealsa-aplay 00:00:00:00:00:00 &
  __start_cards
  __watch_bluetooth
  elif [ $1 -eq 0 ]
  then
  exit
  fi
  
  uptime=$(cut -d'.' -f1 /proc/uptime)
  
  if [ ${uptime} -le 60 ]
  then
  exit
  fi
  
  lmsip=$(netstat -tn | grep 3483 | tr -s ' ' | cut -d' ' -f5 | cut -d ':' -f1)
  iface=$(arp -an | grep ${lmsip} | tr -s ' ' | cut -d' ' -f7)
  read mac < /sys/class/net/${iface}/address
  
  post=$(cat <<END_HEREDOC
  POST /jsonrpc.js HTTP/1.1
  HOST: ${lmsip}:9000
  Content-Type: application/json; charset=utf-8
  Content-Length: 77
  
  {"id":1,"method":"slim.request","params":["${mac}",["title","?"]]}
  END_HEREDOC
  )
  
  response=$( (echo -e "${post}"; sleep .2) | nc ${lmsip} 9000 | grep -o 
'\w*bluetooth-\w*' )
  i=0
  
  case "${response}" in
  "${btplayer}" )
  bluetoothctl discoverable on
  prepair=$(bluetoothctl paired-devices | cut -d' ' -f2)
  while [ $i -le 30 ]
  do
  sleep 2
  newpair=$(bluetoothctl paired-devices | cut -d' ' -f2)
  newdev=$(printf "${prepair}\n${newpair}\n" | sort | uniq -u)
  if [ "${newdev}" ]
  then
  bluetoothctl trust ${newdev}
  bluetoothctl connect ${newdev}
  break
  fi  
  i=$(( $i +1 ))
  done
  bluetoothctl discoverable off ;;
  "${btspeaker}" )
  prescan=$(bluetoothctl devices)
  while [ $i -le 6 ]
  do
  (printf "scan.rssi ${rssi}\nscan on\n"; sleep 5) | bluetoothctl
  sleep .2
  newscan=$(bluetoothctl devices)
  newdev=$(printf "${prescan}\n${newscan}\n" | sort | uniq -u | cut -d' ' -f2)
  if [ "${newdev}" ]
  then
  bluetoothctl pair ${newdev}
  bluetoothctl trust ${newdev}
  bluetoothctl connect ${newdev}
  break
  fi
  i=$(( $i +1 ))
  done ;;
  "${btsync}" )
  lcardtmp=$(pgrep -fa hw:Loopback | grep -m1 -v bluealsa | rev | cut -d' ' -f1 
| rev)
  lcard=$(( ${lcardtmp} + ${lstep} ))
  pids=$(pgrep -fa hw:Loopback | grep -v bluealsa | cut -d' ' -f1 | tr '\n' ' ')
  kill ${pids}
  __start_cards ${lcard} ;;
  esac
  
--------------------


------------------------------------------------------------------------
sodface's Profile: http://forums.slimdevices.com/member.php?userid=19057
View this thread: http://forums.slimdevices.com/showthread.php?t=112536

_______________________________________________
unix mailing list
[email protected]
http://lists.slimdevices.com/mailman/listinfo/unix

Reply via email to