Try to make bluetooth menu connection more reliable
Signed-off-by: Stacy Harper <contact@stacyharper.net> Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
This commit is contained in:
		
							parent
							
								
									fa0d9b6e2d
								
							
						
					
					
						commit
						4bf92406d0
					
				
					 1 changed files with 40 additions and 17 deletions
				
			
		|  | @ -1,16 +1,39 @@ | ||||||
| #!/usr/bin/env sh | #!/usr/bin/env sh | ||||||
| 
 | 
 | ||||||
|  | ispaired() { | ||||||
|  | 	bluetoothctl info "$1" | grep Paired | grep -q yes | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | isconnected() { | ||||||
|  | 	bluetoothctl info "$1" | grep Connected | grep -q yes | ||||||
|  | } | ||||||
|  | 
 | ||||||
| connect() { | connect() { | ||||||
| 	if bluetoothctl info "$1" | grep Connected | grep -q no; then | 	if isconnected "$1"; then | ||||||
|  | 		notify-send "Already connected !" | ||||||
|  | 		return | ||||||
|  | 	fi | ||||||
| 	bluetoothctl discoverable on | 	bluetoothctl discoverable on | ||||||
| 		if bluetoothctl info "$1" | grep Paired | grep -q no; then | 	if ! ispaired "$1"; then | ||||||
|  | 		notify-send "Pairing..." | ||||||
| 		bluetoothctl pairable on | 		bluetoothctl pairable on | ||||||
| 			notify-send "Pairing with $1..." | 		if bluetoothctl --agent=KeyboardDisplay pair "$1"; then | ||||||
| 			bluetoothctl --timeout=10 pair "$1" || notify-send "Pairing failed" && return | 			notify-send "Paired !" | ||||||
| 			bluetoothctl trust "$1" || notify-send "Trusting failed" && return | 			bluetoothctl trust "$1" || notify-send "Trusting failed" | ||||||
|  | 		else | ||||||
|  | 			notify-send "Pairing failed" | ||||||
| 		fi | 		fi | ||||||
| 		bluetoothctl --timeout=5 connect "$1" || notify-send "Connecting failed" && return | 		bluetoothctl pairable off | ||||||
| 	fi | 	fi | ||||||
|  | 	if ispaired "$1"; then | ||||||
|  | 		notify-send "Connecting..." | ||||||
|  | 		if bluetoothctl --agent=KeyboardDisplay connect "$1"; then | ||||||
|  | 			notify-send "Connected !" | ||||||
|  | 		else | ||||||
|  | 			notify-send "Connecting failed" | ||||||
|  | 		fi | ||||||
|  | 	fi | ||||||
|  | 	bluetoothctl discoverable off | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| devicemenu() { | devicemenu() { | ||||||
|  | @ -19,25 +42,25 @@ devicemenu() { | ||||||
| 	fi | 	fi | ||||||
| 	while true; do | 	while true; do | ||||||
| 		DEVICES="$(bluetoothctl devices | awk '{ $1=""; printf $2; $2=""; printf "  ^" $0 "\n" }')" | 		DEVICES="$(bluetoothctl devices | awk '{ $1=""; printf $2; $2=""; printf "  ^" $0 "\n" }')" | ||||||
| 		ENTRIES="$(echo "$DEVICES" | sed 's|.*  ^  ||' | xargs -0 printf "Close Menu\nDisconnect\nScan\n%s")" | 		ENTRIES="$(echo "$DEVICES" | sed 's|.*  ^  ||' | xargs -0 printf "Refresh\nClose Menu\nDisconnect\nScan\n%s")" | ||||||
| 
 | 
 | ||||||
| 		PICKED="$( | 		PICKED="$( | ||||||
| 			echo "$ENTRIES" | | 			echo "$ENTRIES" | | ||||||
| 			sxmo_dmenu_with_kb.sh -i -c -l 10 -p "Devices" | 			dmenu -i -c -l 10 -p "Devices" | ||||||
| 		)" | 		)" | ||||||
| 
 | 
 | ||||||
| 		if echo "$PICKED" | grep -q "Close Menu"; then | 		if echo "$PICKED" | grep -q "Refresh"; then | ||||||
|  | 			continue | ||||||
|  | 		elif echo "$PICKED" | grep -q "Close Menu"; then | ||||||
| 			exit | 			exit | ||||||
| 		elif echo "$PICKED" | grep -q "Disconnect"; then | 		elif echo "$PICKED" | grep -q "Disconnect"; then | ||||||
| 			st -e sh -c "bluetoothctl disconnect; sleep 1" | 			st -e sh -c "bluetoothctl disconnect; sleep 1" | ||||||
| 		elif echo "$PICKED" | grep -q "Scan"; then | 		elif echo "$PICKED" | grep -q "Scan"; then | ||||||
| 			notify-send "Scanning BT devices for 5 seconds..." | 			notify-send "Scanning BT devices for 30 seconds..." | ||||||
| 			bluetoothctl --timeout=5 scan on | 			bluetoothctl --timeout=30 scan on && notify-send "End of scan" & | ||||||
| 		else | 		else | ||||||
| 			devicemac="$(echo "$DEVICES" | grep "  \^  $PICKED$" | sed 's|  ^  .*||'  )" | 			devicemac="$(echo "$DEVICES" | grep "  \^  $PICKED$" | sed 's|  ^  .*||'  )" | ||||||
| 			st -e sh -c "$0 connect $devicemac" | 			st -e sh -c "$0 connect $devicemac; sleep 1" | ||||||
| 			bluetoothctl pairable off |  | ||||||
| 			bluetoothctl discoverable off |  | ||||||
| 		fi | 		fi | ||||||
| 	done | 	done | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue