| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser |
| 7 |
import os.path |
| 8 |
import sys |
| 9 |
import dbus |
| 10 |
import dbus.mainloop.glib |
| 11 |
try: |
| 12 |
from gi.repository import GObject |
| 13 |
except ImportError: |
| 14 |
import gobject as GObject |
| 15 |
|
| 16 |
BUS_NAME='org.bluez.obex' |
| 17 |
PATH = '/org/bluez/obex' |
| 18 |
CLIENT_INTERFACE='org.bluez.obex.Client1' |
| 19 |
SESSION_INTERFACE='org.bluez.obex.Session1' |
| 20 |
OBJECT_PUSH_INTERFACE='org.bluez.obex.ObjectPush1' |
| 21 |
TRANSFER_INTERFACE='org.bluez.obex.Transfer1' |
| 22 |
|
| 23 |
def parse_options(): |
| 24 |
parser.add_option("-d", "--device", dest="device", |
| 25 |
help="Device to connect", metavar="DEVICE") |
| 26 |
parser.add_option("-p", "--pull", dest="pull_to_file", |
| 27 |
help="Pull vcard and store in FILE", metavar="FILE") |
| 28 |
parser.add_option("-s", "--send", dest="send_file", |
| 29 |
help="Send FILE", metavar="FILE") |
| 30 |
parser.add_option("-v", "--verbose", action="store_true", |
| 31 |
dest="verbose") |
| 32 |
|
| 33 |
return parser.parse_args() |
| 34 |
|
| 35 |
class OppClient: |
| 36 |
def __init__(self, session_path, verbose=False): |
| 37 |
self.transferred = 0 |
| 38 |
self.transfer_path = None |
| 39 |
self.verbose = verbose |
| 40 |
bus = dbus.SessionBus() |
| 41 |
obj = bus.get_object(BUS_NAME, session_path) |
| 42 |
self.session = dbus.Interface(obj, SESSION_INTERFACE) |
| 43 |
self.opp = dbus.Interface(obj, OBJECT_PUSH_INTERFACE) |
| 44 |
bus.add_signal_receiver(self.properties_changed, |
| 45 |
dbus_interface="org.freedesktop.DBus.Properties", |
| 46 |
signal_name="PropertiesChanged", |
| 47 |
path_keyword="path") |
| 48 |
|
| 49 |
def create_transfer_reply(self, path, properties): |
| 50 |
self.transfer_path = path |
| 51 |
self.transfer_size = properties["Size"] |
| 52 |
if self.verbose: |
| 53 |
print("Transfer created: %s" % path) |
| 54 |
|
| 55 |
def error(self, err): |
| 56 |
print(err) |
| 57 |
mainloop.quit() |
| 58 |
|
| 59 |
def properties_changed(self, interface, properties, invalidated, path): |
| 60 |
if path != self.transfer_path: |
| 61 |
return |
| 62 |
|
| 63 |
if "Status" in properties and \ |
| 64 |
(properties["Status"] == "complete" or \ |
| 65 |
properties["Status"] == "error"): |
| 66 |
if self.verbose: |
| 67 |
print("Transfer %s" % properties["Status"]) |
| 68 |
mainloop.quit() |
| 69 |
return |
| 70 |
|
| 71 |
if "Transferred" not in properties: |
| 72 |
return |
| 73 |
|
| 74 |
value = properties["Transferred"] |
| 75 |
speed = (value - self.transferred) / 1000 |
| 76 |
print("Transfer progress %d/%d at %d kBps" % (value, |
| 77 |
self.transfer_size, |
| 78 |
speed)) |
| 79 |
self.transferred = value |
| 80 |
|
| 81 |
def pull_business_card(self, filename): |
| 82 |
self.opp.PullBusinessCard(os.path.abspath(filename), |
| 83 |
reply_handler=self.create_transfer_reply, |
| 84 |
error_handler=self.error) |
| 85 |
|
| 86 |
def send_file(self, filename): |
| 87 |
self.opp.SendFile(os.path.abspath(filename), |
| 88 |
reply_handler=self.create_transfer_reply, |
| 89 |
error_handler=self.error) |
| 90 |
|
| 91 |
if __name__ == '__main__': |
| 92 |
|
| 93 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 94 |
|
| 95 |
parser = OptionParser() |
| 96 |
|
| 97 |
(options, args) = parse_options() |
| 98 |
|
| 99 |
if not options.device: |
| 100 |
parser.print_help() |
| 101 |
sys.exit(0) |
| 102 |
|
| 103 |
bus = dbus.SessionBus() |
| 104 |
mainloop = GObject.MainLoop() |
| 105 |
|
| 106 |
client = dbus.Interface(bus.get_object(BUS_NAME, PATH), |
| 107 |
CLIENT_INTERFACE) |
| 108 |
|
| 109 |
print("Creating Session") |
| 110 |
path = client.CreateSession(options.device, { "Target": "OPP" }) |
| 111 |
|
| 112 |
opp_client = OppClient(path, options.verbose) |
| 113 |
|
| 114 |
if options.pull_to_file: |
| 115 |
opp_client.pull_business_card(options.pull_to_file) |
| 116 |
|
| 117 |
if options.send_file: |
| 118 |
opp_client.send_file(options.send_file) |
| 119 |
|
| 120 |
mainloop.run() |