python - Global name 'bluetooth' is not defined -
i working bitalino board , wanted print data python when run proper code, shows me message
global name 'bluetooth' not defined
according pc board connected via bluetooth. don't know problem is, me? pd: using mac os x.
this part of code problem may be:
try: import bluetooth bluetooth import discover_devices except importerror: pass import serial serial.tools import list_ports import time import math import numpy class bitalino(object): def __init__(self): """ bitalino class: interface bitalino hardware. """ self.socket = none self.analogchannels = [] self.number_bytes = none self.macaddress = none self.serial = false def find(self, serial=false): """ search bluetooth devices nearby output: tuple name , mac address of each device found """ try: if serial: nearby_devices = list(port[0] 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 bluetooth device mac address provided. configure sampling rate. kwargs: macaddress (string): mac address of bluetooth device samplingrate(int): sampling frequency (hz); values available: 1000, 100, 10 , 1 output: true or -1 (error) """ setup = true while setup: if macaddress != none: try: if ":" in macaddress , 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 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 needed connect" return -1 else: self.macaddress = macaddress return true
hopefully help: http://lightblue.sourceforge.net/
this api python's bluetooth feature
Comments
Post a Comment