Home > database >  bluez python 'import bluetooth_constants' not working, constants not available
bluez python 'import bluetooth_constants' not working, constants not available

Time:05-27

I get this error when running python code:

ModuleNotFoundError: No module named 'bluetooth_constants'.

I expect to have a set of constants available for use in my code.

on Jetson Nano Ubuntu Linux 18.04 I have installed all modules I see talked about on the 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

I'm using server_advertising.py code from the study guide at https://www.bluetooth.com/bluetooth-resources/bluetooth-for-linux/, Developing LE Peripheral Devices using Python. Displayed at the end of this question.

I cut it down to the minimal to show the error.

Code: 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 />

Error:

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'

I get the code can't find the bluetooth_constants modules. Since the code lines are in the current code from bluetooth.org i don't think the use has been deprecated. I just must be missing a module installation, or maybe an instantiation is needed to set constant objects. There must be a list of these constants plus a way to instantiate. Any ideas? Thanks!

Complete code of server_advertising.py with error for reference: Code: 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)

Error:

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'

CodePudding user response:

The module named bluetooth_constants is provided by the study guide.

In the A1 Bluetooth Linux Study Guide - Installation and Configuration.pdf document it says:

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_.

It looks like you may not have the bluetooth_* files in the same directory as your test_import_constants.py file

  • Related