3 from __future__ import absolute_import, print_function, unicode_literals
5 from optparse import OptionParser
9 import dbus.mainloop.glib
11 from gi.repository import GObject
13 import gobject as GObject
15 BUS_NAME='org.bluez.obex'
16 PATH = '/org/bluez/obex'
17 CLIENT_INTERFACE='org.bluez.obex.Client1'
18 SESSION_INTERFACE='org.bluez.obex.Session1'
19 OBJECT_PUSH_INTERFACE='org.bluez.obex.ObjectPush1'
20 TRANSFER_INTERFACE='org.bluez.obex.Transfer1'
23 parser.add_option("-d", "--device", dest="device",
24 help="Device to connect", metavar="DEVICE")
25 parser.add_option("-p", "--pull", dest="pull_to_file",
26 help="Pull vcard and store in FILE", metavar="FILE")
27 parser.add_option("-s", "--send", dest="send_file",
28 help="Send FILE", metavar="FILE")
29 parser.add_option("-v", "--verbose", action="store_true",
32 return parser.parse_args()
35 def __init__(self, session_path, verbose=False):
37 self.transfer_path = None
38 self.verbose = verbose
39 bus = dbus.SessionBus()
40 obj = bus.get_object(BUS_NAME, session_path)
41 self.session = dbus.Interface(obj, SESSION_INTERFACE)
42 self.opp = dbus.Interface(obj, OBJECT_PUSH_INTERFACE)
43 bus.add_signal_receiver(self.properties_changed,
44 dbus_interface="org.freedesktop.DBus.Properties",
45 signal_name="PropertiesChanged",
48 def create_transfer_reply(self, path, properties):
49 self.transfer_path = path
50 self.transfer_size = properties["Size"]
52 print("Transfer created: %s" % path)
58 def properties_changed(self, interface, properties, invalidated, path):
59 if path != self.transfer_path:
62 if "Status" in properties and \
63 (properties["Status"] == "complete" or \
64 properties["Status"] == "error"):
66 print("Transfer %s" % properties["Status"])
70 if "Transferred" not in properties:
73 value = properties["Transferred"]
74 speed = (value - self.transferred) / 1000
75 print("Transfer progress %d/%d at %d kBps" % (value,
78 self.transferred = value
80 def pull_business_card(self, filename):
81 self.opp.PullBusinessCard(os.path.abspath(filename),
82 reply_handler=self.create_transfer_reply,
83 error_handler=self.error)
85 def send_file(self, filename):
86 self.opp.SendFile(os.path.abspath(filename),
87 reply_handler=self.create_transfer_reply,
88 error_handler=self.error)
90 if __name__ == '__main__':
92 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
94 parser = OptionParser()
96 (options, args) = parse_options()
98 if not options.device:
102 bus = dbus.SessionBus()
103 mainloop = GObject.MainLoop()
105 client = dbus.Interface(bus.get_object(BUS_NAME, PATH),
108 print("Creating Session")
109 path = client.CreateSession(options.device, { "Target": "OPP" })
111 opp_client = OppClient(path, options.verbose)
113 if options.pull_to_file:
114 opp_client.pull_business_card(options.pull_to_file)
116 if options.send_file:
117 opp_client.send_file(options.send_file)