Меnu:


One thing that is always missing by default in any GNU/Linux distribution is a quick and easy way for sending and receiving files through bluetooth. In the previous article we saw how to receive files, this time we will see how to send files by bluetooth from the command line.

¿Where did this idea come from?

There is an utility within the ubuntu distro that is used to send files by bluetooth from the command line called bluetooth-sendto , so we will try to emulate this utility.

What do we need?

We need to install PyOBEX in our case in our distro Archlinux we have installed it for python2, there is also this package for python3. once the package is installed. we must have this script (send_fyle.py):

#!/usr/bin/env python
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
uuidv =  sys.argv[2]
filename =  sys.argv[3]
absolute_path =  sys.argv[4]
print("Searching for OBEX service on %s" % addr)

service_matches = find_service(uuid = uuidv, address = addr )

if len(service_matches) == 0:
    print("Couldn't find the service.")
    sys.exit(0)

first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]

print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put(filename, open(absolute_path, "rb").read())
client.disconnect()

and with that we could test it with:

#    python2 ~/dev/python/send_fyle.py $BTAddress $MYUUID $FILENAMEVAR $MYABSOLUTE_PATH
bt-sendto-n900f() {
    bt-sendto 20:D6:07:BF:A2:1E $1
}
bt-sendto() {
    MYUUID="00001105-0000-1000-8000-00805f9b34fb"
    if [ ! -z "$2" ]; then
        BTAddress="$1"
        MYABSOLUTE_PATH=$(readlink -f "$2")
    else
        BTAddress="14:5F:94:AE:86:38"
        MYABSOLUTE_PATH=$(readlink -f "$1")
    fi
    FILENAMEVAR=$(echo $MYABSOLUTE_PATH| rev | awk -v FS='/' '{print $1}' | rev)
    python2 ~/dev/python/send_fyle.py $BTAddress $MYUUID $FILENAMEVAR $MYABSOLUTE_PATH
}

The first line is for testing that it works (remove comment character). Then we have the bt-sendto function to send to devices with specific addresses (bluetooth addresses).

It have been done. You could start sending and receiving files easily, we should remember that we do not use a desktop environment in kipuamutay just Openbox{} as a window manager and that all this article has been written and published in an arm station from an opi+2e.

How to identify the devices where You could send files?

After scanning the bluetooth devices near your station, and to be able to identify the devices that can receive files by bluetooth, we verify the devices that have the "OBEX Object Push" service. hint (info bt-address from the bluetoothctl) on each device.

Something that could be improved

It would be convenient not to depend on python but rather to have a binary compiled directly with the bluetooth libraries that would serve the same purpose.

Last change: 03.07.2018 02:14

blog comments powered by Disqus