我正在尝试获取 Ubuntu 10.10 中当前插入的 USB 设备的列表并监视发生的更改,例如使用 UDev 和 D-BUS 插入或拔出的设备。我对使用 D-BUS 进行编程相当陌生。我看到了一个例子:Linux : How to detect is USB keyboard is plugged and unplugged only that one uses HAL and I know that HAL is deprecated。我找到了一些工作代码,对其进行了一些修改,只是它不适用于任何设备,只有存储设备,如 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()
任何帮助将不胜感激。提前谢谢你,卡洛塔罗密欧