2017-02-26 123 views
1

代碼如下:拋出藍牙上下文中的Windows 3.5上的Python 3.5。請求的地址不在其上下文中有效

from bluetooth import * 
import sys 

if sys.version < '3': 
    input = raw_input 

addr = None 

if len(sys.argv) < 2: 
    print("no device specified. Searching all nearby bluetooth devices for") 
    print("the SampleServer service") 
else: 
    addr = sys.argv[1] 
    print("Searching for SampleServer on %s" % addr) 

# search for the SampleServer service 
addr = "CC:79:4A:4B:35:85" 
service_matches = find_service(address = addr) 

if len(service_matches) == 0: 
    print("couldn't find the SampleServer service =(") 
    sys.exit(0) 

first_match = service_matches[0] 
port = first_match["port"] 
name = first_match["name"] 
host = first_match["host"] 

print("connecting to \"%s\" at Address - %s on Port %d" % (name, host, port)) 

# Create the client socket 
sock=BluetoothSocket(RFCOMM) 
sock.connect((host, port)) 

print("connected. type stuff") 
while True: 
    data = input() 
    if len(data) == 0: break 
    sock.send(data) 

sock.close() 

運行時錯誤如下:

no device specified. Searching all nearby bluetooth devices for 
the SampleServer service 
connecting to "None" at Address - CC:79:4A:4B:35:85 on Port 31 
Traceback (most recent call last): 
    File "T_C_1.py", line 40, in <module> 
    sock.connect((host, port)) 
    File "C:\Python 3.5\lib\site-packages\bluetooth\msbt.py", line 72, in connect 
    bt.connect (self._sockfd, addr, port) 
OSError: The requested address is not valid in its context. 

我無法找出原因。有些網站告訴我,我的主機地址需要在本地機器上。...... 此代碼駐留在我的WIndows計算機上,並嘗試通過藍牙連接到Android手機。 我不明白爲什麼/如何... 幫助表示讚賞!

回答

0

我在Windows上使用PyBluez運行相同的錯誤,因爲我必須在收到綁定錯誤後離開我的Mac環境。我發現當你綁定到一個外部IP地址時,或者你的系統不知道的地址(例如,在公共IP之外)時會出現問題。嘗試使用''的地址在PyBluez中創建服務器並嘗試連接到該服務器。如果這不起作用,我們可以從那裏進一步診斷您的問題。對不起,它不是一個堅實的答案,我是新來的StackOverflow,我不能完全評論,但我想提供一些幫助。