Skip to content

Instantly share code, notes, and snippets.

@geakstr
Created December 8, 2015 17:06
Show Gist options
  • Save geakstr/e09d5c037e639cacf031 to your computer and use it in GitHub Desktop.
Save geakstr/e09d5c037e639cacf031 to your computer and use it in GitHub Desktop.
Send data to bluetooth through serial port [OSX]
#!/bin/bash
### Usage ###
# ./osxbt.sh → Start screen session with `btserialbash` name (this is necessarily before work)
# ./osxbt.sh -s → Stop screen session
# ./osxbt.sh -d "HELLO WORLD" → Send HELLO WORLD through bluetooth
args=("$@")
function start {
screen -d -m -S btserialbash /dev/cu.Bluetooth-Incoming-Port 9600
screen -r btserialbash
}
function stop {
screen -wipe
pids=$(screen -ls | awk '/\.btserialbash\t/ {print $1}' | sed 's/[^0-9]*//g')
while read -r pid; do
if [ ${#pid} -ge 1 ]; then
kill -9 "${pid}"
fi
done <<< "$pids"
screen -wipe
}
function restart {
stop
start
}
function send {
screen -S btserialbash -X stuff "$1"
}
if [ "${args[0]}" = '-d' ]; then
send "${args[1]}"
elif [ "${args[0]}" = '-s' ]; then
stop
else
restart
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment