的文档SendFile
说返回了一个 DBus 对象路径和一个属性字典。返回的对象的接口是org.bluez.obex.Transfer1
允许Status
监视的。
监视此状态的理想方法是使用 GLib.MainLoop 中的事件循环并观察PropertiesChanged
由SendFile
.
此外,通过获取配对设备信息,有一个 BlueZ DBus API 可用于获取该信息,而无需subprocess
调用bluetoothctl
.
我试图做一个例子:
import pydbus
from gi.repository import GLib
from time import sleep
OBEX_TRANSFER = 'org.bluez.obex.Transfer1'
BLUEZ_DEVICE = 'org.bluez.Device1'
ses_bus = pydbus.SessionBus()
sys_bus = pydbus.SystemBus()
mngr = sys_bus.get('org.bluez', '/')
mainloop = GLib.MainLoop()
def paired_devices():
mngd_objs = mngr.GetManagedObjects()
for path, iface in mngd_objs.items():
if BLUEZ_DEVICE in iface:
addr = iface.get(BLUEZ_DEVICE, {}).get('Address')
name = iface.get(BLUEZ_DEVICE, {}).get('Name', addr)
paired = iface.get(BLUEZ_DEVICE, {}).get('Paired')
if paired:
return addr
def exit_session():
print('Exit session')
mainloop.quit()
obex.RemoveSession(ses1)
def transfer_status_handler(iface, props_changed, props_removed):
if iface == OBEX_TRANSFER:
status = props_changed.get('Status')
if status == 'complete':
print('Transfer complete')
exit_session()
elif status == 'queued':
print('Still queued')
elif status == 'active':
print('transferring')
elif status == 'suspended':
print('Suspended')
elif status == 'error':
print('error')
exit_session()
obex = ses_bus.get('org.bluez.obex', '/org/bluez/obex')
my_addr = paired_devices()
ses1 = obex.CreateSession(my_addr, {'Target': pydbus.Variant('s', 'OPP')})
ses1_dbus = ses_bus.get('org.bluez.obex', ses1)
path, props = ses1_dbus.SendFile('/home/pi/.bashrc')
transfer = ses_bus.get('org.bluez.obex', path)
transfer.onPropertiesChanged = transfer_status_handler
try:
mainloop.run()
except KeyboardInterrupt:
exit_session()