Guest User

btsend

a guest
Nov 7th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. self="${0##*/}"
  4. dev_mac=
  5. dev_channel=
  6. service="OPUSH"
  7. service_readable="OBEX Object Push"
  8. wait_at_end=
  9.  
  10. find_all()
  11. {
  12.     service=${1:-$service}
  13.     data="$(sdptool search $service)"
  14.     IFS='
  15. '
  16.     dev_mac=
  17.     for line in $data; do
  18.         if echo "$line" | grep -Ei "^Searching for $service" >/dev/null; then
  19.             dev_mac=`echo $line | grep -Eio "([0-9a-f]{2}:){5}[0-9a-f]{2}"`
  20.             dev_channel=
  21.         fi
  22.         if [ -n "$dev_mac" ]; then
  23.             if echo "$line" | grep -E "^[[:space:]]*Channel:" >/dev/null; then
  24.                 dev_channel=`echo $line | grep -Eio "\b[0-9]+\$"`
  25.             fi
  26.         fi
  27.        
  28.         if [ -n "$dev_mac" -a -n "$dev_channel" ]; then
  29.             echo "[$dev_mac]:$dev_channel"
  30.             dev_mac=
  31.         fi
  32.     done
  33. }
  34.  
  35. while [ -n "$1" ]; do
  36. case "$1" in
  37.     -h|--help)  echo "Options:
  38.  -m      MAC address of BT device (omit to search)
  39.  -c      channel of service (omit to autodetect)
  40.  -f      search for devices
  41.  -w      wait on complete"
  42.     exit;;  
  43.     -m) shift; dev_mac="$1";;
  44.     -c) shift; dev_channel="$1";;
  45.     -f) find_all; exit;;
  46.     -w) wait_at_end=1;;
  47.     --) shift; break;;
  48.     -*) false; shift;;
  49.     *)  break;;
  50. esac
  51. shift
  52. done
  53.  
  54.  
  55. if [ -z "$dev_mac" ]; then
  56.     s=`find /var/lib/bluetooth -mindepth 2 -maxdepth 2 -name lastused -print -quit`
  57.     if [ -n "$s" ]
  58.     then
  59.         echo "Known devices:" >&2
  60.         join <(sort /var/lib/bluetooth/*/lastused) <(sort /var/lib/bluetooth/*/names)
  61.     else
  62.         echo "There is not any known device." >&2
  63.     fi
  64.     echo
  65.  
  66.     echo "Scanning devices..." >&2
  67.     oldIFS=$IFS
  68.     IFS=$'\n'
  69.     oldCOLUMNS=$COLUMNS
  70.     COLUMNS=1
  71.    
  72.     scan="$(hcitool scan | sed -e 1d)"
  73.     # scandb=$(echo "$scan" | sed -n \$=)   # [ "0$REPLY" -ge 1 -a "0$REPLY" -le $scandb ]
  74.     # dev_names=$(echo "$scan" | sed -e 's/^\s\+\S\+\s\+//g')
  75.     select mac_and_name in $scan
  76.     do
  77.         if [ -n "$mac_and_name" ]; then
  78.             dev_mac=`echo "$mac_and_name" | grep -Eiom1 "([0-9a-f]{2}:){5}[0-9a-f]{2}"`
  79.             break
  80.         fi
  81.     done
  82.     IFS=$oldIFS
  83.     COLUMNS=$oldCOLUMNS
  84. fi
  85. if [ -z "$dev_channel" -a -n "$dev_mac" ]; then
  86.     echo -n "Detect channel... " >&2
  87.     dev_channel=`sdptool browse $dev_mac | awk "/$service_readable/,/^\$/" | grep -Em1 "Channel" | grep -Eio "\b[0-9]+\$"`
  88.     echo $dev_channel
  89. fi
  90.  
  91. while [ -n "$1" -a -n "$dev_mac" -a -n "$dev_channel" ]; do
  92.     if [ "${1// /}" != "${1}" ]; then
  93.         echo "$self: spaces in filename not allowed" >&2
  94.         exit 1
  95.     fi
  96.     path="$1"
  97.     fname="${1##*/}"
  98.     printf "c\np $path $fname\nd\nq\n" | obex_test -b $dev_mac $dev_channel
  99.     # | awk '{if(/^Made some progress/){printf "!"}else{print}}'
  100.  
  101.     # PUT failed 0x43!  --> megszakította
  102.     # PUT failed 0x60!  --> nincs elég hely
  103.  
  104.     shift
  105. done
  106.  
  107. # [ -n "$DISPLAY" ] && xmessage
  108.  
  109. test "$wait_at_end" = 1 && read -s -p "Terminated. Press Enter. "
  110. exit
Advertisement
Add Comment
Please, Sign In to add comment