BASH script allows user to work with Bluetooth in CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
clear | |
echo -e "\n\t\t#Bluetooth script by Horloge-Skynet.\n" | |
echo -e "\t\033[1;31m/!\ Be sure that your Hardware Bluetooth is running... /!\ \033[0m \n\n" | |
if [ -d /usr/share/doc/bluez/ ] || [ -d /usr/share/doc/bluez-utils/ ]; then | |
echo -e "Bluetooth of \033[1m$USER\033[0m on \033[1m$HOSTNAME\033[0m with \033[1m$SHELL\033[0m:" | |
while [ true ]; do | |
sleep 1.5 | |
echo -e "\n\n\tMENU:" | |
echo -e "\n1)\t\033[37mService: Status\033[0m" | |
echo -e "2)\t\033[32mService: Start\033[0m" | |
echo -e "3)\t\033[31mService: Stop\033[0m" | |
echo -e "4)\t\033[36mService: Restart\033[0m" | |
echo -e "5)\t\033[32mShow Device.\033[0m" | |
echo -e "6)\t\033[31mHide Device.\033[0m" | |
echo -e "7)\t\033[37mDevice Informations...\033[0m" | |
echo -e "8)\t\033[33mChange Device Name...\033[0m" | |
echo -e "9)\t\033[35mScan for others Devices...\033[0m" | |
echo -e "10)\t\033[32mPair a Device...\033[0m" | |
echo -e "11)\t\033[31mUnpair a Device...\033[0m" | |
echo -e "12)\tExit.\n" | |
read -p "Choice ? " -n 2 choice | |
echo -e "\n" | |
sleep 1 | |
case $choice in | |
'1') | |
/etc/init.d/bluetooth status | |
;; | |
'2') | |
/etc/init.d/bluetooth start | |
/etc/init.d/bluetooth status | |
;; | |
'3') | |
/etc/init.d/bluetooth stop | |
/etc/init.d/bluetooth status | |
;; | |
'4') | |
/etc/init.d/bluetooth restart | |
/etc/init.d/bluetooth status | |
;; | |
'5') | |
hciconfig hci0 piscan | |
sleep 1 | |
echo -e "\033[32mDevice might be Showed.\033[0m" | |
;; | |
'6') | |
hciconfig hci0 noscan | |
sleep 1 | |
echo -e "\033[31mDevice might be Hidden.\033[0m" | |
;; | |
'7') | |
hcitool dev | |
;; | |
'8') | |
read -p "Device Name: " device_name | |
hciconfig hci0 name $device_name | |
/etc/init.d/bluetooth restart | |
/etc/init.d/bluetooth status | |
;; | |
'9') | |
hcitool scan | |
;; | |
"10") | |
hcitool scan | |
read -p "Which Device ? (line number expected) " -n 1 device | |
let "device += 1" | |
echo -e "\n" | |
ADDR=$(hcitool scan | sed -n '/'"$device"'/p' | cut -c 2-19); | |
rfcomm connect hci0 $ADDR | |
;; | |
"11") | |
MAC=$(hcitool dev | grep 'hci' | cut -c 7-); | |
echo -e "\nDevice(s) Paired:" | |
if [ -e /var/lib/bluetooth/$MAC/names ]; then | |
while read line | |
do | |
echo -e "$line" | |
done < /var/lib/bluetooth/$MAC/names | |
echo -e "\n" | |
read -p "Do you want to delete a Device ? (y/n) " -n 1 sure | |
echo -e "\n" | |
if [ $sure = 'y' ] || [ $sure = 'Y' ]; then | |
read -p "Which Device to delete ? (line number expected) " -n 1 deletion | |
echo -e "\n" | |
MAC_deleted=$(sed '/'"$deletion"'/p' /var/lib/bluetooth/$MAC/names | cut -c 19-) | |
touch /var/lib/bluetooth/$MAC/names_temp | |
chmod 777 /var/lib/bluetooth/$MAC/names | |
chmod 777 /var/lib/bluetooth/$MAC/names_temp | |
sed ''"$deletion"'d' /var/lib/bluetooth/$MAC/names > /var/lib/bluetooth/$MAC/names_temp | |
cat /var/lib/bluetooth/$MAC/names_temp > /var/lib/bluetooth/$MAC/names | |
rm /var/lib/bluetooth/$MAC/names_temp | |
echo -e "\033[32m$MAC_deleted might be Unpaired.\033[0m" | |
fi | |
fi | |
;; | |
"12") | |
echo -e "\nThis script will be closed." | |
sleep 1 | |
clear | |
exit 1 | |
;; | |
*) | |
echo -e "Bad Choice.\n" | |
;; | |
esac | |
done | |
else | |
echo "\033[31mThere is no any Bluetooth Support installed, or it is broken." | |
read -p "Do you want to install one ? (y/n) " -n 1 check | |
echo -e "\n" | |
if [ $check = 'y' ] || [ $check = 'Y' ]; then | |
apt-get update && apt-get install bluetooth > /dev/null | |
wait | |
echo -e "\nPlease, consider re-executing this script.\n\n" | |
else | |
echo -e "\nThis script will be closed." | |
sleep 1 | |
clear | |
fi | |
fi | |
echo 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment