我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt) [英] I cannot list up the BLE devices in the neighbourhood of my Raspberry Pi (python, btmgmt)
问题描述
我想使用从 cron 脚本调用的 python 脚本来扫描 Raspberry 环境中的 ble 设备.但是当我在 cron 中执行此操作时(我的意思是我添加到 sudo crontab -e),我总是得到一个空列表.
I want to scan the ble devices in the environment of my Raspberry, by using a python script called from a cron script. But when I do this in cron (I mean I added to sudo crontab -e), I allways end up with an empty list.
当我以 pi 用户身份登录时 - btmgmt(仅)在 su 权限下正常工作:
when I am logged in as pi user - btmgmt works (only) fine with su permissions:
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脚本中我写道:
so in my python script I wrote:
flog.write("P01:
")
out = subprocess.Popen(['sudo', '/usr/bin/btmgmt', 'find'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
flog.write("stderr: " + str(stderr) + "
")
cDvc = stdout.split('
')
flog.write("Amount of lines = " + str(len(cDvc)) + "
")
for line in cDvc:
line = line + '
'
if debugflag:
print(line)
flog.write(line)
..
从 shell 提示运行这个脚本工作正常.. 在日志文件 (flog) 中我得到:...
Running this script from the shell prompt works fine.. in the log-file (flog) I get: ...
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 行相同的脚本:没有设备出现 &我找不到原因:
Running this same script as a crontab -e line: no devices show up & I cannot find cause:
...
P01:
stderr: None
Amount of lines = 1
P02:
...
有人可以帮我吗?
推荐答案
我有完全相同的问题.我需要使用树莓派来检查附近是否有某些特定的蓝牙设备处于活动状态,并向监控服务发送心跳.
I have exact the same issue. I need to use a raspberry pi to check if some specific bluetooth devices are alive nearby and to send a heartbeat to monitoring service.
当命令在像 * * * * * sudo btmgmt find > 这样的 cron 作业中执行时,我没有从
,如果我使用 python 来捕获来自 Popen 调用的输出,也不会.所以我问自己是否可以在另一个屏幕上执行它,它奏效了.sudo btmgmt find
得到任何输出./tmp/ble_devices.txt
I was not getting any output from sudo btmgmt find
when the command was executed in a cron job like * * * * * sudo btmgmt find > /tmp/ble_devices.txt
, neither if I was using python to capture the output from Popen call. So I asked myself if I could executed it into another screen, and it worked.
我的解决方案很黑.我做了以下事情:
My solution is quite hackish. I did the following:
- 在树莓派上安装屏幕工具:
sudo apt install screen
- 创建一个用于运行扫描命令的屏幕:
screen -S blescan
- 将自己从屏幕上分离出来
ctrl
+a
+d
- 在
/home/pi/scan_job
中创建了一个 shell 脚本,内容如下:
- Installed the screen tool on raspberry pi:
sudo apt install screen
- Created a screen for running the scan command:
screen -S blescan
- Detached myself from screen
ctrl
+a
+d
- Created a shell script in
/home/pi/scan_job
with the content:
#!/bin/bash
cd <to python project> && ./<file to be executed>
- 使其可执行
chmod +x/home/pi/scan_job
- 设置 cronjob 以在 blescan 屏幕中执行文件:
- Made it executable
chmod +x /home/pi/scan_job
- Set cronjob to execute the file in blescan screen:
*/10 * * * * screen -S blescan -X screen '/home/pi/scan_job'
这篇关于我无法列出 Raspberry Pi 附近的 BLE 设备(python、btmgmt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!