簡體   English   中英

bluez python 'import bluetooth_constants' 不工作,常量不可用

[英]bluez python 'import bluetooth_constants' not working, constants not available

運行 python 代碼時出現此錯誤:

ModuleNotFoundError:沒有名為“bluetooth_constants”的模塊

我希望有一組常量可用於我的代碼。

在 Jetson Nano Ubuntu Linux 18.04 我已經安裝了我在 .net 上看到的所有模塊:

:~$ 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文件中說:

創建一個目錄,在其中創建您自己的腳本。 將學習指南code/solutions目錄下打包的以bluetooth_開頭的Python文件復制到該文件夾中。

看起來你可能沒有bluetooth_*文件在與你的test_import_constants.py文件相同的目錄中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM