Global name 'bluetooth' is not defined

1.3k views Asked by At

I am working with the bitalino board and I wanted to print the data with python but when I run the proper code, it shows me the message

Global name 'bluetooth' is not defined

According to my pc the board is connected via bluetooth. I don't know what the problem is, could you help me? Pd: I am using Mac OS X.

This is part of the code where the problem may be:

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
2

There are 2 answers

0
ofarooq On BEST ANSWER

Hopefully this will help: http://lightblue.sourceforge.net/

This is an API for python's bluetooth feature

0
Andreas On

It's generally better to let the import fail if something is wrong so I would remove the try..except and just do a normal import.

The only reason to use it in an import if you are indifferent about what library you want to use:

try:
    import json
except ImportError:
    import simplejson as json