我正在尝试获取Ubuntu 10.10中当前插入的USB设备列表并监视发生的更改,例如使用UDev和D-BUS插入或拔出设备。我相对较新地使用D-BUS进行编程。我看到了一个例子:Linux:如何检测是否插入或拔出USB键盘,但那个例子使用了HAL,而我知道HAL已经不建议使用了。我找到了一些可行的代码,稍微修改了一下,但它只适用于存储设备,例如USB闪存驱动器、媒体播放器或CD-ROM设备。我想要整个系统,包括鼠标、键盘、USB相机、充电器,任何插入USB的设备,我希望我的程序都能获得这些信息。这基本上是我的代码(http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html):
import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop
def device_added_callback(device):
print 'Device %s was added' % (device)
def device_changed_callback(device):
print 'Device %s was changed' % (device)
#must be done before connecting to DBus
DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
proxy = bus.get_object("org.freedesktop.UDisks",
"/org/freedesktop/UDisks")
iface = dbus.Interface(proxy, "org.freedesktop.UDisks.Device")
devices = iface.get_dbus_method('EnumerateDevices')()
print '%s' % (devices)
#addes two signal listeners
iface.connect_to_signal('DeviceAdded', device_added_callback)
iface.connect_to_signal('DeviceChanged', device_changed_callback)
#start the main loop
mainloop = gobject.MainLoop()
mainloop.run()
希望能得到任何帮助。提前感谢您,Calota Romeo