Python and Bluetooth

环境

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()

手动部分

  1. 第一次连接设备时应该要手动配置PC和蓝牙设备,我这里因为是音乐盒子,所以只需要按照提示配置PC就OK
  2. Windows7发现就算连接上了,外部蓝牙设备依旧没反应,将程序打包成exe放到Windows8,没毛病(暂时不清楚是什么原因,知道的大神望告知!!!)

参考:

http://pages.iu.edu/~rwisman/c490/html/pythonandbluetooth.htm

转载于:https://www.cnblogs.com/YangARTuan/p/10713086.html

你可能感兴趣的:(Python and Bluetooth)