Here's the next iteration of the bt-pair.sh script.  Now using two
different tracks, one for a player and one for a speaker.  Also switched
to continuing the script on power on status and exiting immediately on
power off instead of the other way around.  For speakers, I'm using an
rssi filter setting to reduce the number of devices that show up in a
scan.  Weaker devices are filtered out so you have to have the speaker
close to the device for initial pairing to be successful.  Trying to
keep it as "hands off" as possible.  I know it's hacky.


Code:
--------------------
    
  #!/bin/sh
  
  if [ $1 -eq 0 ]
  then
  exit
  fi
  
  uptime=$(cat /proc/uptime | cut -d'.' -f1)
  
  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
  btplayer="bluetooth-player"
  btspeaker="bluetooth-speaker"
  rssi="-60"
  
  post=$(cat <<END_HEREDOC
  POST /jsonrpc.js HTTP/1.1
  HOST: ${lmsip}:9000
  Content-Type: application/json; charset=utf-8
  Content-Length: 97
  
  
{"id":1,"method":"slim.request","params":["${mac}",["status","-","1","tags:acgltys"]]}
  END_HEREDOC
  )
  
  if (echo -e "${post}"; sleep .2) | nc ${lmsip} 9000 | grep "${btplayer}"
  then
  i=0
  bluetoothctl discoverable on
  prepair=$(bluetoothctl paired-devices | cut -d' ' -f2)
  while [ $i -le 30 ]
  do
  newpair=$(bluetoothctl devices | cut -d' ' -f2)
  newdev=$(printf "${prepair}\n${newpair}\n" | sort | uniq -u)
  if [ "${newdev}" ]
  then
  bluetoothctl pair ${newdev}
  bluetoothctl trust ${newdev}
  bluetoothctl connect ${newdev}
  break
  else
  i=$(( $i +1 ))
  sleep 2
  fi
  done
  bluetoothctl discoverable off
  elif (echo -e "${post}"; sleep .2) | nc ${lmsip} 9000 | grep "${btspeaker}"
  then
  i=0
  prescan=$(bluetoothctl devices)
  while [ $i -le 6 ]
  do
  (printf "scan.rssi ${rssi}\nscan.rssi\nscan on\n"; sleep 10) | 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
  fi
  
--------------------


------------------------------------------------------------------------
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