我试图获取列表中的USB设备在Ubuntu 10.10当前插入和监控发生变化,比如或缩小使用udev和d-BUS被插入的设备。我对使用D-BUS编程相当陌生。我看到一个例子:Linux : How to detect is usb keyboard is plugged and unplugged只有一个使用HAL,我知道HAL已被弃用。我发现了一些工作代码,稍加修改,只适用于任何设备,如USB棒,媒体播放器或光盘设备。我希望整个事情鼠标,键盘,USB相机充电器任何插入到USB我想我的程序知道它。这基本上就是我所拥有的(http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html):USB设备udev和d-BUS
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罗密欧
供未来参考(我正在寻找这个代码,而不是通用的USB处理程序),您需要更改: iface = dbus.Interface(proxy,“org.freedesktop.UDisks.Device”) 至少在使用udisks 1.0.4时,iface = dbus.Interface(代理,“org.freedesktop.UDisks”) 到 。 – 2012-11-27 10:26:24