2010-10-01 15:31:21 +08:00
|
|
|
#!/usr/bin/python
|
2012-05-25 13:44:25 +08:00
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2010-10-01 15:31:21 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import gobject
|
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
import sys
|
|
|
|
|
|
|
|
DBusGMainLoop(set_as_default=True)
|
|
|
|
loop = gobject.MainLoop()
|
|
|
|
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
|
|
|
|
hdp_manager = dbus.Interface(bus.get_object("org.bluez", "/org/bluez"),
|
|
|
|
"org.bluez.HealthManager")
|
|
|
|
app_path = hdp_manager.CreateApplication({"DataType": dbus.types.UInt16(4103),
|
|
|
|
"Role": "sink"})
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print(app_path)
|
2010-10-01 15:31:21 +08:00
|
|
|
|
|
|
|
manager = dbus.Interface(bus.get_object("org.bluez", "/"),
|
2012-12-05 20:51:33 +08:00
|
|
|
"org.freedesktop.DBus.ObjectManager")
|
|
|
|
|
|
|
|
objects = manager.GetManagedObjects()
|
|
|
|
adapters = []
|
|
|
|
|
|
|
|
for path, ifaces in objects.iteritems():
|
|
|
|
if ifaces.has_key("org.bluez.Adapter"):
|
|
|
|
adapters.append(path)
|
2010-10-01 15:31:21 +08:00
|
|
|
|
|
|
|
i = 1
|
|
|
|
for ad in adapters:
|
2012-05-25 13:44:25 +08:00
|
|
|
print("%d. %s" % (i, ad))
|
2010-10-01 15:31:21 +08:00
|
|
|
i = i + 1
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Select an adapter: ",)
|
2010-10-01 15:31:21 +08:00
|
|
|
select = None
|
|
|
|
while select == None:
|
|
|
|
try:
|
|
|
|
pos = int(sys.stdin.readline()) - 1
|
|
|
|
if pos < 0:
|
|
|
|
raise TypeError
|
|
|
|
select = adapters[pos]
|
|
|
|
except (TypeError, IndexError, ValueError):
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Wrong selection, try again: ",)
|
2010-10-01 15:31:21 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
adapter = dbus.Interface(bus.get_object("org.bluez", select),
|
|
|
|
"org.bluez.Adapter")
|
|
|
|
|
2012-07-04 20:31:00 +08:00
|
|
|
devices = adapter.GetProperties()["Devices"]
|
2010-10-01 15:31:21 +08:00
|
|
|
|
|
|
|
if len(devices) == 0:
|
2012-05-25 13:44:25 +08:00
|
|
|
print("No devices available")
|
2010-10-01 15:31:21 +08:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
i = 1
|
|
|
|
for dev in devices:
|
2012-05-25 13:44:25 +08:00
|
|
|
print("%d. %s" % (i, dev))
|
2010-10-01 15:31:21 +08:00
|
|
|
i = i + 1
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Select a device: ",)
|
2010-10-01 15:31:21 +08:00
|
|
|
select = None
|
|
|
|
while select == None:
|
|
|
|
try:
|
|
|
|
pos = int(sys.stdin.readline()) - 1
|
|
|
|
if pos < 0:
|
|
|
|
raise TypeError
|
|
|
|
select = devices[pos]
|
|
|
|
except (TypeError, IndexError, ValueError):
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Wrong selection, try again: ",)
|
2010-10-01 15:31:21 +08:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Connecting to %s" % (select))
|
2010-10-01 15:31:21 +08:00
|
|
|
device = dbus.Interface(bus.get_object("org.bluez", select),
|
|
|
|
"org.bluez.HealthDevice")
|
|
|
|
|
|
|
|
chan = device.CreateChannel(app_path, "Any")
|
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print(chan)
|
2010-10-01 15:31:21 +08:00
|
|
|
|
2012-05-25 13:44:25 +08:00
|
|
|
print("Push Enter for finishing")
|
2010-10-01 15:31:21 +08:00
|
|
|
sys.stdin.readline()
|
|
|
|
|
|
|
|
hdp_manager.DestroyApplication(app_path)
|