gpt4 book ai didi

python - 我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt)

转载 作者:行者123 更新时间:2023-12-03 20:56:16 24 4
gpt4 key购买 nike

我想使用从 cron 脚本调用的 python 脚本来扫描 Raspberry 环境中的 ble 设备。
但是当我在 cron 中执行此操作时(我的意思是我添加到 sudo crontab -e),我总是得到一个空列表。

当我以 pi 用户身份登录时 - btmgmt 可以(仅)在 su 权限下正常工作:

pi@Pluto:~ $ btmgmt find
Unable to start discovery. status 0x14 (Permission Denied)

pi@Pluto:~ $ sudo btmgmt find
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 77:F8:D7:8A:1E:E5 type LE Random rssi -83 flags 0x0000
...

所以在我的python脚本中我写道:
flog.write("P01:\r\n")
out = subprocess.Popen(['sudo', '/usr/bin/btmgmt', 'find'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
flog.write("stderr: " + str(stderr) + "\r\n")
cDvc = stdout.split('\n')
flog.write("Amount of lines = " + str(len(cDvc)) + "\r\n")
for line in cDvc:
line = line + '\r\n'
if debugflag:
print(line)
flog.write(line)
..

从 shell 提示符运行此脚本工作正常.. 在日志文件 (flog) 中我得到:...
P01:
stderr: None
Amount of lines = 40
Discovery started
hci0 type 7 discovering on
hci0 dev_found: 70:D0:FD:74:34:AC type LE Random rssi -59 flags 0x0000
AD flags 0x1a
..

运行与 crontab -e 行相同的脚本:没有设备出现,我找不到原因:
...
P01:
stderr: None
Amount of lines = 1
P02:
...

有人可以帮我从这里出去吗?

最佳答案

如果您使用 BlueZ DBus API 来获取信息,那么您将不需要使用 sudo。它还避免了您必须使用 btmgmt,因为我不确定它是否打算以这种方式编写脚本
DBus API 的文档位于:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
pydbus 库对于访问 BlueZ DBus API 非常有帮助:https://pypi.org/project/pydbus/
一些有用的信息可以帮助您入门:

  • bluez 的 Dbus 服务称为“org.bluez”
  • 默认蓝牙适配器通常将“/org/bluez/hci0”作为其 DBus 对象路径。
  • BlueZ/DBus 有一个对象管理器,用于存储有关设备的信息

  • 我做了以下脚本来测试这个想法:
    from datetime import datetime
    import os
    import pydbus
    from gi.repository import GLib

    discovery_time = 60
    log_file = '/home/pi/device.log'

    # Create an empty log file
    def write_to_log(address, rssi):
    if os.path.exists(log_file):
    open_mode = 'a'
    else:
    open_mode = 'w'

    with open(log_file, open_mode) as dev_log:
    now = datetime.now()
    current_time = now.strftime('%H:%M:%S')
    dev_log.write(f'Device seen[{current_time}]: {address} @ {rssi} dBm\n')

    bus = pydbus.SystemBus()
    mainloop = GLib.MainLoop()

    class DeviceMonitor:
    def __init__(self, path_obj):
    self.device = bus.get('org.bluez', path_obj)
    self.device.onPropertiesChanged = self.prop_changed
    print(f'Device added to monitor {self.device.Address}')

    def prop_changed(self, iface, props_changed, props_removed):
    rssi = props_changed.get('RSSI', None)
    if rssi is not None:
    print(f'\tDevice Seen: {self.device.Address} @ {rssi} dBm')
    write_to_log(self.device.Address, rssi)


    def end_discovery():
    """Handler for end of discovery"""
    mainloop.quit()
    adapter.StopDiscovery()

    def new_iface(path, iface_props):
    """If a new dbus interfaces is a device, add it to be monitored"""
    device_addr = iface_props.get('org.bluez.Device1', {}).get('Address')
    if device_addr:
    DeviceMonitor(path)

    # BlueZ object manager
    mngr = bus.get('org.bluez', '/')
    mngr.onInterfacesAdded = new_iface

    # Connect to the DBus api for the Bluetooth adapter
    adapter = bus.get('org.bluez', '/org/bluez/hci0')
    adapter.DuplicateData = False

    # Iterate around already known devices and add to monitor
    mng_objs = mngr.GetManagedObjects()
    for path in mng_objs:
    device = mng_objs[path].get('org.bluez.Device1', {}).get('Address', [])
    if device:
    DeviceMonitor(path)

    # Run discovery for discovery_time
    adapter.StartDiscovery()
    GLib.timeout_add_seconds(discovery_time, end_discovery)
    print('Finding nearby devices...')
    try:
    mainloop.run()
    except KeyboardInterrupt:
    end_discovery()

    关于python - 我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915339/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com