Bluez-obex 和 python opp 服务器,如何更改文件的存储位置?

Posted

技术标签:

【中文标题】Bluez-obex 和 python opp 服务器,如何更改文件的存储位置?【英文标题】:Bluez-obex and python opp server, how to change where files are stored? 【发布时间】:2021-10-03 17:58:53 【问题描述】:

我正在使用 bluez-obex 创建一个基于 python 的 opp obex 服务器,但是我在更改目录时遇到了问题。我将我的代码基于this,并将其转换为一个以文件路径为输入的类。我用我当前的代码不断收到KeyError: 'object does not export any interfaces; you might need to pass object path as the 2nd argument for get()'。我设置的路径错误吗?我的课程代码和调用函数如下。

from gi.repository import GLib
import pydbus
class opp_server():
    def __init__(self, path):
        self.BUS_NAME = 'org.bluez.obex'
        self.PATH = '/org/bluez/obex'
        self.AGENT_MANAGER_INTERFACE = 'org.bluez.obex.AgentManager1'
        self.AGENT_INTERFACE = 'org.bluez.obex.Agent1'
        self.SESSION_INTERFACE = 'org.bluez.obex.Session1'
        self.TRANSFER_INTERFACE = 'org.bluez.obex.Transfer1'

        self.ses_bus = pydbus.SessionBus()
        self.path = path

    def transfer_status_handler(self, iface, props_changed, props_removed):
        if iface == self.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(self, dbus_path, interfaces):
        if self.SESSION_INTERFACE in interfaces and 'server' in dbus_path:
            print('Server session added')
        elif self.TRANSFER_INTERFACE in interfaces and 'server' in dbus_path:
            print('Transfer started')
            transfer = self.ses_bus.get(self.BUS_NAME, dbus_path)
            transfer.onPropertiesChanged = self.transfer_status_handler


    def AuthorizePush(self):
        print('Authorize Push', self.path)
        transfer = self.ses_bus.get(self.BUS_NAME, self.path)
        props = transfer.GetAll(self.TRANSFER_INTERFACE)
        print(props)
        return props.get('Name')

    def Cancel(self):
        print('Authorization Cancelled')

    def Release(self):
        pass


    def server(self):
        obex_mngr = self.ses_bus.get('org.bluez.obex', '/')
        obex_mngr.onInterfacesAdded = self.iface_added_handler
        mainloop = GLib.MainLoop()
        self.ses_bus.register_object('/test/agent', self.AuthorizePush(), None)
        print('Agent created')
        agnt_mngr = ses_bus.get(self.BUS_NAME, self.PATH)
        agnt_mngr.RegisterAgent('/test/agent')
        print('Agent registered')
        try:
            mainloop.run()
        except KeyboardInterrupt:
            mainloop.quit()

调用类

server = servertest.opp_server('/home/pi/GAMMA/ImageData')
server.server()

【问题讨论】:

【参考方案1】:

您似乎混淆了 DBus 对象路径和文件系统路径。对于AuthorizePush,输入是org.bluez.obex.Transfer1 接口的DBus 对象路径,但您正在尝试使用您希望放置文件的位置的文件系统路径。

当您发布/导出org.bluez.obex.Agent1 接口时,您需要具有相同参数的三个方法。 Exporting own objects 的 pydbus 教程可能值得一看

obexd/usr/share/dbus-1/services/org.bluez.obex.service 中的信息开头,对我来说是:

[D-BUS Service]
Name=org.bluez.obex
Exec=/usr/lib/bluetooth/obexd
SystemdService=obex.service

obexd 可以从帮助中看到的其他选项启动:

$ /usr/lib/bluetooth/obexd -h
Usage:
  obexd [OPTION?]

Help Options:
  -h, --help                  Show help options

Application Options:
  -d, --debug=DEBUG           Enable debug information output
  -p, --plugin=NAME,...       Specify plugins to load
  -P, --noplugin=NAME,...     Specify plugins not to load
  -n, --nodetach              Run with logging in foreground
  -r, --root=PATH             Specify root folder location. Both absolute and relative can be used, but relative paths are assumed to be relative to user $HOME folder. Default $XDG_CACHE_HOME
  -S, --root-setup=SCRIPT     Root folder setup script
  -l, --symlinks              Allow symlinks leading outside of the root folder
  -c, --capability=FILE       Specify capability file, use '!' mark for scripts
  -a, --auto-accept           Automatically accept push requests

因此您可以使用--root=PATH 选项来更改默认位置。

如果您确实想更改代码中的路径,则需要使从AuthorizePush 返回的值具有绝对路径。例如:

    def AuthorizePush(self, path):
        transfer = ses_bus.get(BUS_NAME, path)
        props = transfer.GetAll(TRANSFER_INTERFACE)
        return '/tmp/' + props.get('Name')

这会将文件放在/tmp 目录中。

【讨论】:

以上是关于Bluez-obex 和 python opp 服务器,如何更改文件的存储位置?的主要内容,如果未能解决你的问题,请参考以下文章

Python opp知识整理

Python + kivy文本输入on_text_validate事件

linux opp怎么使用

面向过程编程(OPP) 和面向对象编程(OOP)的关系

OPP

关于 construct object opp