- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用从 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
...
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)
..
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
..
...
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/
一些有用的信息可以帮助您入门:
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/
我无法使用 btmgmt add-adv 命令。我已经尝试了建议的示例,但它也不起作用。我的目标是使用 BlueZ 5.x 堆栈(我使用的是 5.31 版)使用我自己的自定义负载设置广告和扫描响应负载
我想使用从 cron 脚本调用的 python 脚本来扫描 Raspberry 环境中的 ble 设备。 但是当我在 cron 中执行此操作时(我的意思是我添加到 sudo crontab -e),我
我正在尝试使用btmgmt rust库读取连接的BLE设备的RSSI值,但总是得到127。。我确信该设备已连接,并已解决服务问题,下面是BluToothctl输出:。我尝试使用CLI呼叫btmgmt,
我是一名优秀的程序员,十分优秀!