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

我无法列出我的Raspberry Pi(python、btmgmt)附近的BLE设备,python,cron,raspberry-pi,bluez,Python,Cron,Raspberry Pi,Bluez,我想在我的Raspberry环境中扫描ble设备,方法是使用从cron脚本调用的python脚本。 但是当我在cron中这样做时(我的意思是我添加到sudocrontab-e中),我总是得到一个空列表 当我以pi用户身份登录时-btmgmt(仅限)使用su权限正常工作: pi@Pluto:~ $ btmgmt find Unable to start discovery. status 0x14 (Permission Denied) pi@Pluto:~ $ sudo btmgmt find

我想在我的Raspberry环境中扫描ble设备,方法是使用从cron脚本调用的python脚本。 但是当我在cron中这样做时(我的意思是我添加到sudocrontab-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的文档可从以下网址获得:

pydbus库对于访问BlueZ DBus API非常有用:

要开始学习,需要了解一些有用的事情:

  • bluez的Dbus服务称为“org.bluez”
  • 默认蓝牙适配器通常将“/org/bluez/hci0”作为其DBus对象路径
  • BlueZ/DBus有一个存储设备信息的对象管理器
  • 我编写了以下脚本来测试这个想法:

    从日期时间导入日期时间
    导入操作系统
    导入pydbus
    从gi.repository导入GLib
    发现时间=60
    log_file='/home/pi/device.log'
    #创建一个空日志文件
    def写入日志(地址,rssi):
    如果os.path.存在(日志文件):
    打开模式='a'
    其他:
    打开模式='w'
    打开(日志文件,打开模式)作为开发日志:
    now=datetime.now()
    当前时间=现在。strftime(“%H:%M:%S”)
    dev_log.write(f'Device seen[{current_time}]:{address}@{rssi}dBm\n')
    总线=pydbus.SystemBus()
    mainloop=GLib.mainloop()
    类别设备监视器:
    定义初始化(自我,路径对象):
    self.device=bus.get('org.bluez',path_obj)
    self.device.onPropertiesChanged=self.prop\u已更改
    打印(f'Device added to monitor{self.Device.Address}')
    def道具更改(自身、iface、道具更改、道具移除):
    rssi=props\u changed.get('rssi',None)
    如果rssi不是无:
    打印(f'\t看到的设备:{self.device.Address}@{rssi}dBm')
    写入日志(self.device.Address,rssi)
    def end_discovery():
    “”“查找结束的处理程序”“”
    mainloop.quit()
    adapter.StopDiscovery()
    def新iface(路径、iface道具):
    “”“如果新的dbus接口是设备,请将其添加到要监视的设备”“”
    device_addr=iface_props.get('org.bluez.Device1',{}.get('Address'))
    如果设备地址:
    设备监视器(路径)
    #BlueZ对象管理器
    mngr=bus.get('org.bluez','/'))
    mngr.onInterfacesAdded=新
    #连接到蓝牙适配器的DBus api
    adapter=bus.get('org.bluez','/org/bluez/hci0')
    adapter.DuplicateData=False
    #迭代已知设备并添加到监视器
    mng_objs=mngr.GetManagedObjects()
    对于mng_objs中的路径:
    device=mng_objs[path].get('org.bluez.Device1',{}).get('Address',[])
    如果设备:
    设备监视器(路径)
    #在发现时间运行发现
    适配器。StartDiscovery()
    GLib.timeout\u add\u seconds(发现时间,结束发现)
    打印('查找附近的设备…')
    尝试:
    mainloop.run()
    除键盘中断外:
    (完)
    
    我也有同样的问题。我需要使用树莓pi来检查附近是否有特定的蓝牙设备,并向监控服务发送心跳信号

    在类似于
    ***sudo btmgmt find>/tmp/ble_devices.txt的cron作业中执行命令时,我没有从
    sudo btmgmt find
    获得任何输出,如果我使用python捕获Popen调用的输出,也是如此。所以我问自己是否可以在另一个屏幕上执行它,结果它成功了

    我的解决方案相当老套。我做了以下工作:

  • 在raspberry pi上安装了屏幕工具:
    sudo-apt-install-screen
  • 创建了用于运行扫描命令的屏幕:
    screen-S blescan
  • 将自己从屏幕
    ctrl
    +
    a
    +
    d
  • /home/pi/scan_job
    中创建了一个shell脚本,内容如下:

  • 你找到解决办法了吗?我正试图从bash中执行类似的操作,如果从cron调用btmgmt,我将无法从中获得任何输出
       #!/bin/bash 
       cd <to python project> && ./<file to be executed>
    
    */10 * * * * screen -S blescan -X screen '/home/pi/scan_job'