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手機。 我不明白爲什麼/如何... 幫助表示讚賞!