| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser, make_option |
| 7 |
import sys |
| 8 |
import time |
| 9 |
import dbus |
| 10 |
import bluezutils |
| 11 |
|
| 12 |
bus = dbus.SystemBus() |
| 13 |
|
| 14 |
manager = dbus.Interface(bus.get_object("org.bluez", "/"), |
| 15 |
"org.bluez.Manager") |
| 16 |
|
| 17 |
option_list = [ |
| 18 |
make_option("-i", "--device", action="store", |
| 19 |
type="string", dest="dev_id"), |
| 20 |
] |
| 21 |
parser = OptionParser(option_list=option_list) |
| 22 |
|
| 23 |
(options, args) = parser.parse_args() |
| 24 |
|
| 25 |
if (len(args) < 1): |
| 26 |
print("Usage: %s <address> [service]" % (sys.argv[0])) |
| 27 |
sys.exit(1) |
| 28 |
|
| 29 |
|
| 30 |
if args[0] == "connect": |
| 31 |
del args[:1] |
| 32 |
|
| 33 |
if (len(args) < 2): |
| 34 |
service = "panu" |
| 35 |
else: |
| 36 |
service = args[1] |
| 37 |
|
| 38 |
device = bluezutils.find_device(args[0], options.dev_id) |
| 39 |
|
| 40 |
network = dbus.Interface(bus.get_object("org.bluez", device.object_path), |
| 41 |
"org.bluez.Network1") |
| 42 |
|
| 43 |
iface = network.Connect(service) |
| 44 |
|
| 45 |
print("Connected to %s service %s, interface %s" % (args[0], service, iface)) |
| 46 |
|
| 47 |
print("Press CTRL-C to disconnect") |
| 48 |
|
| 49 |
try: |
| 50 |
time.sleep(1000) |
| 51 |
print("Terminating connection") |
| 52 |
except: |
| 53 |
pass |
| 54 |
|
| 55 |
network.Disconnect() |