我不熟悉obexpushd
使用obexd
.
我的理解是,您需要创建一个obex-agent来接受/拒绝蓝牙对象推送请求obexd
。
监控InterfacesAdded
DBus 信号应该会为您提供传输开始时需要知道的信息。然后,您可以监视PropertiesChanged
信号以了解状态何时发生变化。
我能够将照片从手机推送到树莓派,并在 RPi 上运行以下命令(我的手机和 RPi 已经配对):
from gi.repository import GLib
import pydbus
BUS_NAME = 'org.bluez.obex'
PATH = '/org/bluez/obex'
AGENT_MANAGER_INTERFACE = 'org.bluez.obex.AgentManager1'
AGENT_INTERFACE = 'org.bluez.obex.Agent1'
SESSION_INTERFACE = 'org.bluez.obex.Session1'
TRANSFER_INTERFACE = 'org.bluez.obex.Transfer1'
ses_bus = pydbus.SessionBus()
def transfer_status_handler(iface, props_changed, props_removed):
if iface == TRANSFER_INTERFACE:
status = props_changed.get('Status')
if status == 'complete':
print('Transfer complete')
elif status == 'queued':
print('Still queued')
elif status == 'active':
print('transferring')
elif status == 'suspended':
print('Suspended')
elif status == 'error':
print('error')
def iface_added_handler(dbus_path, interfaces):
if SESSION_INTERFACE in interfaces and 'server' in dbus_path:
print('Server session added')
elif TRANSFER_INTERFACE in interfaces and 'server' in dbus_path:
print('Transfer started')
transfer = ses_bus.get(BUS_NAME, dbus_path)
transfer.onPropertiesChanged = transfer_status_handler
class Agent:
"""
<node>
<interface name='org.bluez.obex.Agent1'>
<method name='Release'>
</method>
<method name='AuthorizePush'>
<arg type='s' name='path' direction='out'/>
<arg type='o' name='transfer' direction='in'/>
</method>
<method name='Cancel'>
</method>
</interface>
</node>
"""
def AuthorizePush(self, path):
print('Authorize Push', path)
transfer = ses_bus.get(BUS_NAME, path)
props = transfer.GetAll(TRANSFER_INTERFACE)
print(props)
return props.get('Name')
def Cancel(self):
print('Authorization Cancelled')
def Release(self):
pass
if __name__ == '__main__':
obex_mngr = ses_bus.get('org.bluez.obex', '/')
obex_mngr.onInterfacesAdded = iface_added_handler
mainloop = GLib.MainLoop()
ses_bus.register_object('/test/agent', Agent(), None)
print('Agent created')
agnt_mngr = ses_bus.get(BUS_NAME, PATH)
agnt_mngr.RegisterAgent('/test/agent')
print('Agent registered')
try:
mainloop.run()
except KeyboardInterrupt:
mainloop.quit()
此代码只接受由它提出的任何请求。这可能是一件坏事,如果文件应该被接受,人们可能想要添加一些检查(或提示用户)。