我无法在我的Raspberry Pi(python,btmgmt)附近列出BLE设备 [英] 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:\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)中:...
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(python,btmgmt)附近列出BLE设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!