環境
Windows7-64bit + Python2.7.15-64bit + Pybluez
安裝pybluez時會報錯,處理方法~
1.安裝VCForPython27.msi,這是為了提供visual c++ 9.0運行環境
2.C:\Program Files (x86)\Microsoft SDKs\Windows\ 目錄中 V7.0A 改為 V6.0A
重新pip2 install pybluez,很快就完成環境安裝
PyBluez
A Python Bluetooth library for the Windows and GNU/Linux operating systems. Mac OSX and Linux Python are supported by LightBlue, a number of cell phones running the Symbian OS are supported under Python. The following examples use the PyBluez bluetooth library.
翻譯成中文意思大概就是:Linux和Windows系統都能使用這個python藍牙庫,其中Mac和Linux還可以支持LightBlue這個庫,部分塞班(Symbian)系統也支持藍牙庫pybluez
查看周圍藍牙設備
from bluetooth import * print "performing inquiry..." nearby_devices = discover_devices(lookup_names = True) print "found %d devices" % len(nearby_devices) for name, addr in nearby_devices: print " %s - %s" % (addr, name)
設備連接
from bluetooth import * # Create the client socket client_socket=BluetoothSocket( RFCOMM ) client_socket.connect(("30:21:88:CD:4E:08", 3)) #client_socket.send("Hello World") print "Finished"
#進程一結束意味着連接斷開,這里為了不斷開用一個while循環來占用CPU while True: time.sleep(0.001) continue client_socket.close()
手動部分
- 第一次連接設備時應該要手動配置PC和藍牙設備,我這里因為是音樂盒子,所以只需要按照提示配置PC就OK
- Windows7發現就算連接上了,外部藍牙設備依舊沒反應,將程序打包成exe放到Windows8,沒毛病(暫時不清楚是什么原因,知道的大神望告知!!!)
參考:
http://pages.iu.edu/~rwisman/c490/html/pythonandbluetooth.htm