我想在我的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:
...
有人能帮我吗
我有完全相同的问题。我需要使用树莓pi来检查附近是否有特定的蓝牙设备,并向监控服务发送心跳信号
在像
* * * * * sudo btmgmt find > /tmp/ble_devices.txt
这样的cron作业中执行命令时,我没有从sudo btmgmt find
获得任何输出,如果我使用python捕获Popen调用的输出也是如此。所以我问自己是否可以在另一个屏幕上执行它,结果它成功了我的解决方案相当老套。我做了以下工作:
sudo apt install screen
screen -S blescan
ctrl
+a
+d
中分离我自己/home/pi/scan_job
中创建了一个shell脚本,内容如下:chmod +x /home/pi/scan_job
如果您使用bluezdbusapi来获取信息,那么就不需要使用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库对于访问bluezdbusapi非常有用:https://pypi.org/project/pydbus/
要开始学习,需要了解一些有用的事情:
我编写了以下脚本来测试这个想法:
相关问题 更多 >
编程相关推荐