全局名称 'bluetooth' 未定义

Global name 'bluetooth' is not defined

我正在使用 bitalino 板,我想用 python 打印数据,但是当我 运行 正确的代码时,它显示消息

Global name 'bluetooth' is not defined

根据我的电脑,开发板是通过蓝牙连接的。我不知道问题是什么,你能帮我吗? Pd: 我正在使用 Mac OS X.

这是可能出现问题的部分代码:

try:
    import bluetooth
    from bluetooth import discover_devices
except ImportError:
    pass
import serial
from serial.tools import list_ports
import time
import math
import numpy



class BITalino(object):

    def __init__(self):
        """
        BITalino class: interface to the BITalino hardware.

        """
        self.socket = None
        self.analogChannels = []
        self.number_bytes = None
        self.macAddress = None
        self.serial = False

    def find(self, serial=False):
        """
        Search for bluetooth devices nearby

        Output: tuple with name and mac address of each device found
        """

        try:
            if serial:
                nearby_devices = list(port[0] for port in list_ports.comports() if 'bitalino' or 'COM' in port[0])
            else:
                nearby_devices = discover_devices(lookup_names=True)
            return nearby_devices
        except:
            return -1

    def open(self, macAddress=None, SamplingRate=1000):
        """
        Connect to bluetooth device with the mac address provided. 
        Configure the sampling Rate. 

        Kwargs:

            macAddress (string): MAC address of the bluetooth device
            SamplingRate(int): Sampling frequency (Hz); values available: 1000, 100, 10 and 1

        Output: True or -1 (error)
        """

        Setup = True
        while Setup:
            if macAddress != None:
                try:
                    if ":" in macAddress and len(macAddress) == 17:
                        self.socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
                        self.socket.connect((macAddress, 1))
                    else:
                        self.socket = serial.Serial(macAddress, 115200)
                        self.serial = True
                    time.sleep(2)

                    # Configure sampling rate
                    if SamplingRate == 1000:
                        variableToSend = 0x03
                    elif SamplingRate == 100:
                        variableToSend = 0x02
                    elif SamplingRate == 10:
                        variableToSend = 0x01
                    elif SamplingRate == 1:
                        variableToSend = 0x00
                    else:
                        self.socket.close()
                        raise TypeError,  "The Sampling Rate %s cannot be set in BITalino. Choose 1000, 100, 10 or 1." % SamplingRate
                        return -1

                    variableToSend = int((variableToSend<<6)|0x03)
                    self.write(variableToSend)
                    Setup = False

                except Exception, e:
                    print e
                    return -1
            else:
                raise TypeError, "A MAC address or serial port is needed to connect"
                return -1
        else:
            self.macAddress = macAddress
            return True

希望这会有所帮助: http://lightblue.sourceforge.net/

这是 API python 的蓝牙功能

如果出现问题,让导入失败通常会更好,所以我会删除 try..except 并进行正常导入。

如果您对要使用的库无所谓,那么在导入中使用它的唯一原因是:

try:
    import json
except ImportError:
    import simplejson as json