2017-01-29 45 views
0

アンドロイドデバイスからブルートゥース接続を受け入れるラズベリーパイのプログラムを作成しようとしています。しかしビルドは常に失敗します。これは私のコードです:NameError:グローバル名 'BluetoothSocket'が定義されていません

from bluetooth import * 

def initServer(): 
server_sock=BluetoothSocket(RFCOMM) 
server_sock.bind(("",PORT_ANY)) 
server_sock.listen(1) 

uuid = "00001101-0000-1000-8000-00805F9B34FB" 

advertise_service(server_sock, "Echo Server", 
service_id = uuid, 
service_classes = [ uuid, SERIAL_PORT_CLASS ], 
profiles = [ SERIAL_PORT_PROFILE ] 
) 
return server_sock 

def getClientConnection(server_sock): 
print "Waiting for connection" 
client_sock, client_info = server_sock.accept() 
print "accepted connection from ", client_info 
return client_sock 

def manageConnection(socket): 
try: 
    while True: 
    data = socket.recv(1024) 
    if len(data) == 0: break 
    print "received [%s]" % data 
    socket.send("Echo from Pi: [%s]\n" % data) 
except IOError: 
    pass 
server=initServer() 
while True: 
client=getClientConnection(server) 
manageConnection(client) 
client.close() 
server.close() 
print "terminating..." 

これはエラーです:

Traceback (most recent call last): 
    File "bluetooth.py", line 1, in <module> 
    from bluetooth import * 
    File "/home/pi/Desktop/bluetooth.py", line 33, in <module> 
    server=initServer() 
    File "/home/pi/Desktop/bluetooth.py", line 4, in initServer 
    server_sock=BluetoothSocket(RFCOMM) 
NameError: global name 'BluetoothSocket' is not defined 

のpython-ブルートゥースは、すでにインストールされています。しかし、このエラーを引き起こす原因は何か、どうすれば修正できますか?

+0

インデントを修正する – MYGz

答えて

0

ファイルbluetooth.pyを呼び出さないでください。インポートしようとしているモジュールと混同されます。

また、あなたのインデントを見てください。うまくいけば、それはあなたが提出したものとは異なります。

関連する問題