bluez python 'import bluetooth_constants' 不工作,常量不可用
bluez python 'import bluetooth_constants' not working, constants not available
我在 运行 python 代码时收到此错误:
ModuleNotFoundError: No module named 'bluetooth_constants'.
我希望有一组常量可用于我的代码。
Jetson Nano Ubuntu Linux 18.04
我已经安装了我在网上看到的所有模块:
:~$ sudo apt-get install libbluetooth-dev
:~$ sudo apt-get install bluetooth
:~$ sudo python3 -m pip install pybluez
:~$ sudo apt-get install blueman -y && blueman-manager
我正在使用 https://www.bluetooth.com/bluetooth-resources/bluetooth-for-linux/ 学习指南中的 server_advertising.py 代码,使用 Python 开发 LE 外围设备。显示在本题末尾。
我把它减到最小以显示错误。
代码:test_import_constants.py:
#!/usr/bin/python3
import bluetooth
import bluetooth_constants
import bluetooth_exceptions
import dbus
import dbus.exceptions
import dbus.service
import dbus.mainloop.glib
import sys
from gi.repository import GLib
sys.path.insert(0, '.')
#ADAPTER_NAME = "hci0"
#BLUEZ_NAMESPACE = "/org/bluez/"
adapter_path = bluetooth_constants.BLUEZ_NAMESPACE + bluetooth_constants.ADAPTER_NAME <br />
print("Adapter Path: " + adapter_path) <br />
错误:
steven@DEVELOPMENT-JETSON:~$ ./test_import_constants.py
Traceback (most recent call last):
File "./test_import_constants.py", line 4, in <module>
import bluetooth_constants
ModuleNotFoundError: No module named 'bluetooth_constants'
我得到的代码找不到 bluetooth_constants 模块。由于代码行位于 bluetooth.org 的当前代码中,我认为该用法并未被弃用。我一定是缺少模块安装,或者可能需要实例化来设置常量对象。必须有这些常量的列表加上实例化的方法。有任何想法吗?谢谢!
server_advertising.py的完整代码有错误供参考:
代码:server_advertising.py
#!/usr/bin/python3
# Broadcasts connectable advertising packets
import bluetooth_constants
import bluetooth_exceptions
import dbus
import dbus.exceptions
import dbus.service
import dbus.mainloop.glib
import sys
from gi.repository import GLib
sys.path.insert(0, '.')
bus = None
adapter_path = None
adv_mgr_interface = None
# much of this code was copied or inspired by test\example-advertisement in the BlueZ source
class Advertisement(dbus.service.Object):
PATH_BASE = '/org/bluez/ldsg/advertisement'
def __init__(self, bus, index, advertising_type):
self.path = self.PATH_BASE + str(index)
self.bus = bus
self.ad_type = advertising_type
self.service_uuids = None
self.manufacturer_data = None
self.solicit_uuids = None
self.service_data = None
self.local_name = 'Hello'
self.include_tx_power = False
self.data = None
self.discoverable = True
dbus.service.Object.__init__(self, bus, self.path)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# we're assuming the adapter supports advertising
adapter_path = bluetooth_constants.BLUEZ_NAMESPACE + bluetooth_constants.ADAPTER_NAME
print(adapter_path)
错误:
steven@DEVELOPMENT-JETSON:~/Projects/bluetooth$ ./server_advertising.py
Traceback (most recent call last):
File "./server_advertising.py", line 4, in <module>
import bluetooth_constants
ModuleNotFoundError: No module named 'bluetooth_constants'
名为bluetooth_constants
的模块由学习指南提供。
在 A1 Bluetooth Linux Study Guide - Installation and Configuration.pdf
文件中说:
Create a directory in which to create your own scripts. Copy into this
folder the Python files packaged in the code/solutions directory of
the study guide and whose names start with bluetooth_.
您的 bluetooth_*
文件似乎与您的 test_import_constants.py
文件不在同一目录中
我在 运行 python 代码时收到此错误:
ModuleNotFoundError: No module named 'bluetooth_constants'.
我希望有一组常量可用于我的代码。
Jetson Nano Ubuntu Linux 18.04 我已经安装了我在网上看到的所有模块:
:~$ sudo apt-get install libbluetooth-dev
:~$ sudo apt-get install bluetooth
:~$ sudo python3 -m pip install pybluez
:~$ sudo apt-get install blueman -y && blueman-manager
我正在使用 https://www.bluetooth.com/bluetooth-resources/bluetooth-for-linux/ 学习指南中的 server_advertising.py 代码,使用 Python 开发 LE 外围设备。显示在本题末尾。
我把它减到最小以显示错误。
代码:test_import_constants.py:
#!/usr/bin/python3
import bluetooth
import bluetooth_constants
import bluetooth_exceptions
import dbus
import dbus.exceptions
import dbus.service
import dbus.mainloop.glib
import sys
from gi.repository import GLib
sys.path.insert(0, '.')
#ADAPTER_NAME = "hci0"
#BLUEZ_NAMESPACE = "/org/bluez/"
adapter_path = bluetooth_constants.BLUEZ_NAMESPACE + bluetooth_constants.ADAPTER_NAME <br />
print("Adapter Path: " + adapter_path) <br />
错误:
steven@DEVELOPMENT-JETSON:~$ ./test_import_constants.py
Traceback (most recent call last):
File "./test_import_constants.py", line 4, in <module>
import bluetooth_constants
ModuleNotFoundError: No module named 'bluetooth_constants'
我得到的代码找不到 bluetooth_constants 模块。由于代码行位于 bluetooth.org 的当前代码中,我认为该用法并未被弃用。我一定是缺少模块安装,或者可能需要实例化来设置常量对象。必须有这些常量的列表加上实例化的方法。有任何想法吗?谢谢!
server_advertising.py的完整代码有错误供参考: 代码:server_advertising.py
#!/usr/bin/python3
# Broadcasts connectable advertising packets
import bluetooth_constants
import bluetooth_exceptions
import dbus
import dbus.exceptions
import dbus.service
import dbus.mainloop.glib
import sys
from gi.repository import GLib
sys.path.insert(0, '.')
bus = None
adapter_path = None
adv_mgr_interface = None
# much of this code was copied or inspired by test\example-advertisement in the BlueZ source
class Advertisement(dbus.service.Object):
PATH_BASE = '/org/bluez/ldsg/advertisement'
def __init__(self, bus, index, advertising_type):
self.path = self.PATH_BASE + str(index)
self.bus = bus
self.ad_type = advertising_type
self.service_uuids = None
self.manufacturer_data = None
self.solicit_uuids = None
self.service_data = None
self.local_name = 'Hello'
self.include_tx_power = False
self.data = None
self.discoverable = True
dbus.service.Object.__init__(self, bus, self.path)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()
# we're assuming the adapter supports advertising
adapter_path = bluetooth_constants.BLUEZ_NAMESPACE + bluetooth_constants.ADAPTER_NAME
print(adapter_path)
错误:
steven@DEVELOPMENT-JETSON:~/Projects/bluetooth$ ./server_advertising.py
Traceback (most recent call last):
File "./server_advertising.py", line 4, in <module>
import bluetooth_constants
ModuleNotFoundError: No module named 'bluetooth_constants'
名为bluetooth_constants
的模块由学习指南提供。
在 A1 Bluetooth Linux Study Guide - Installation and Configuration.pdf
文件中说:
Create a directory in which to create your own scripts. Copy into this folder the Python files packaged in the code/solutions directory of the study guide and whose names start with bluetooth_.
您的 bluetooth_*
文件似乎与您的 test_import_constants.py
文件不在同一目录中