Skip to content

Instantly share code, notes, and snippets.

@vprusa
Last active February 19, 2020 12:10
Show Gist options
  • Save vprusa/3ee0597f21666fa271a1c3c0d50cbbd2 to your computer and use it in GitHub Desktop.
Save vprusa/3ee0597f21666fa271a1c3c0d50cbbd2 to your computer and use it in GitHub Desktop.
This is a snippet for retreiving and storing infromation from bluetoothctl
#!/bin/bash
# this script shows Bluetooth devices information using bluetoothctl and saves it to dir ${B_DIR}
B_DIR=.btop.tmp
shopt -s expand_aliases
alias bl="bluetoothctl"
printf "addr rssi | first last name \n" ;
mkdir -p ${B_DIR} && bl devices > ${B_DIR}/devicesNow && cat ${B_DIR}/devicesNow | \
while read device; do
addr=$(echo $device | grep Device | awk '{print $2}') ;
DEVICE_FILE=${B_DIR}/device-$addr ;
NEW_DEVICE_FILE="${DEVICE_FILE}.new"
nowStr=$(date +%Y-%m-%d.%H:%M:%S) ;
if [ -f ${DEVICE_FILE} ] ; then
hasFirst=$(cat ${DEVICE_FILE} | grep First) ;
if [ -z "${hasFirst}" ] ; then
#echo "Last: $nowStr" > ${DEVICE_FILE} ;
echo "First: $nowStr" > ${NEW_DEVICE_FILE} ;
else
firstVal=$(cat ${DEVICE_FILE} | grep First)
echo "${firstVal}" > ${NEW_DEVICE_FILE} ;
echo "Last: $nowStr" >> ${NEW_DEVICE_FILE} ;
fi
RSSIList=$(cat ${DEVICE_FILE} | grep 'RSSIList' | sed -r 's/RSSIList: //g') ;
RSSIListArr=()
if [ ! -z "${RSSIList}" ] ; then
RSSIListArr=(${RSSIList// / })
if [ ${#RSSIListArr[@]} -gt 4 ]; then
unset 'RSSIListArr[${#RSSIListArr[@]}-1]'
fi
if [[ "${RSSIListArr[0]}" -gt "${RSSIListArr[1]}" ]]; then
RSSIVector='^'
else
RSSIVector='v'
fi
else
RSSIVector=' '
fi
else
echo "First: $nowStr" > ${NEW_DEVICE_FILE} ;
RSSIVector='~'
fi
bl info $addr >> ${NEW_DEVICE_FILE} ;
RSSIList="${rssi} ${RSSIListArr[@]}"
echo "RSSIList: ${RSSIList}" >> ${NEW_DEVICE_FILE} ;
name=$(cat ${NEW_DEVICE_FILE} | grep 'Name' | awk '{print $2}') ;
rssi=$(cat ${NEW_DEVICE_FILE} | grep 'RSSI:' | awk '{print $2}') ;
first=$(cat ${NEW_DEVICE_FILE} | grep 'First:' | awk '{print $2}') ;
last=$(cat ${NEW_DEVICE_FILE} | grep 'Last:' | awk '{print $2}') ;
rssiList=$(cat ${NEW_DEVICE_FILE} | grep 'RSSIList:' | awk '{ s = ""; for (i = 2; i <= NF; i++) s = s $i " "; print s }') ;
[[ -z ${rssi} ]] && rssi=" "
[[ -z ${last} ]] && last=" "
printf "${addr} ${rssi} ${RSSIVector} ${first} ${last} ${name} " ;
#printf "${rssiList}"
printf "\n"
cp -r ${NEW_DEVICE_FILE} ${DEVICE_FILE}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment