【发布时间】:2022-01-15 11:51:54
【问题描述】:
我正在尝试使用 bluetooth_utils.py 运行 python 程序,以从温度/湿度设备读取蓝牙通知。它在以 root 权限 (sudo) 运行时工作正常,但不能以普通用户运行(在这种情况下是 Raspberry 上的 pi)。我尝试使用以下代码行将错误语句与我的程序和库隔离:
from __future__ import absolute_import
import sys
import struct
import fcntl
import array
import socket
from errno import EALREADY
import bluetooth._bluetooth as bluez
from bluetooth_utils import (toggle_device, enable_le_scan,
parse_le_advertising_events,
disable_le_scan, raw_packet_to_str)
dev_id = 0
enable = True
hci_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id))
req_str = struct.pack("H", dev_id)
request = array.array("b", req_str)
hci_sock.fileno()
bluez.HCIDEVUP
bluez.HCIDEVDOWN
fcntl.ioctl(hci_sock.fileno(), bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN, request[0])
toggle_device(dev_id, False)
以下是使用 pi 运行时的结果(只是在解释器中复制/粘贴):
pi@raspberrypi:~ $ python
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import absolute_import
>>> import sys
>>> import struct
>>> import fcntl
>>> import array
>>> import socket
>>> from errno import EALREADY
>>> import bluetooth._bluetooth as bluez
>>> from bluetooth_utils import (toggle_device, enable_le_scan,
... parse_le_advertising_events,
... disable_le_scan, raw_packet_to_str)
>>> dev_id = 0
>>> enable = True
>>> hci_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
>>> print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id))
Power ON bluetooth device 0
>>> req_str = struct.pack("H", dev_id)
>>> request = array.array("b", req_str)
>>> hci_sock.fileno()
3
>>> bluez.HCIDEVUP
1074022601
>>> bluez.HCIDEVDOWN
1074022602
>>> fcntl.ioctl(hci_sock.fileno(), bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN, request[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 1] Operation not permitted
>>>
当我使用 sudo (sudo python) 运行程序时,它运行良好。
>>> from __future__ import absolute_import
>>> import sys
>>> import struct
>>> import fcntl
>>> import array
>>> import socket
>>> from errno import EALREADY
>>> import bluetooth._bluetooth as bluez
>>> from bluetooth_utils import (toggle_device, enable_le_scan,
... parse_le_advertising_events,
... disable_le_scan, raw_packet_to_str)
>>> dev_id = 0
>>> enable = True
>>> hci_sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_RAW, socket.BTPROTO_HCI)
>>> print("Power %s bluetooth device %d" % ('ON' if enable else 'OFF', dev_id))
Power ON bluetooth device 0
>>> req_str = struct.pack("H", dev_id)
>>> request = array.array("b", req_str)
>>> hci_sock.fileno()
3
>>> bluez.HCIDEVUP
1074022601
>>> bluez.HCIDEVDOWN
1074022602
>>> fcntl.ioctl(hci_sock.fileno(), bluez.HCIDEVUP if enable else bluez.HCIDEVDOWN, request[0])
0
>>> toggle_device(dev_id, False)
Power OFF bluetooth device 0
>>>
试图在某些资源上查找有关底层权限的信息,但除了控制台等通信端口之外找不到任何其他内容...
非常感谢您的帮助。
丹尼尔。
【问题讨论】: