注:本文的开发环境为Windows10+Python3.7.3
pip install pybluez
产生这种问题的原因是windows系统没有安装相应的SDK,没有 Microsoft SDKs这个文件夹,解决办法如下:
windows7安装文件的下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=8279
windows10安装文件的下载地址:https://developer.microsoft.com/zh-cn/windows/downloads/windows-10-sdk
安装对应系统的SDK之前,最好先卸载系统里的Visual C++ 2010 Redistributable和.NET.Framework再安装对应的SDK
再次安装 pybluez还是有错误,查看安装目录,按照setup.py要求MS_SDK = r'Microsoft SDKs\Windows\v6.0A' 目录
windows7需要手动修改Microsoft SDKs下文件夹v7.1为v6.0A
windows10需要手动修改Microsoft SDKs下文件夹v10.0A为v7.0A
完成后再安装新的Visual C++ 2010 Redistributable和.NET.Framework
windows10经过以上步骤后,依然会出现如下的问题:
这种问题最简单的方法是装个visual studio2017以上的软件的C++编译环境,再次安装即可成功
安装pybluez模块之前先要安装libbluetooth-dev,否则会报错。
通过用户友好的名字来寻找通信对象
import bluetooth
target_name = "My Device"
target_address = None
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print("found target bluetooth device with address ", target_address)
else:
print("could not find target bluetooth device nearby")
查询设备服务
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(" %s - %s" % (addr, name))
services = bluetooth.find_service(address=addr)
for svc in services:
print("Service Name: %s" % svc["name"])
print(" Host: %s" % svc["host"])
print(" Description: %s" % svc["description"])
print(" Provided By: %s" % svc["provider"])
print(" Protocol: %s" % svc["protocol"])
print(" channel/PSM: %s" % svc["port"])
print(" svc classes: %s "% svc["service-classes"])
print(" profiles: %s "% svc["profiles"])
print(" service id: %s "% svc["service-id"])
print("")
采用类似于socket编程模型的方式进行蓝牙通信的编程
1.服务器端程序
import bluetooth
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
server_sock.bind(("",port))
server_sock.listen(1)
client_sock,address = server_sock.accept()
print "Accepted connection from ",address
data = client_sock.recv(1024)
print "received [%s]" % data
client_sock.close()
server_sock.close()
2. 客户端程序
import bluetooth
bd_addr = "01:23:45:67:89:AB"
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("hello!!")
sock.close()
L2CAP的sockets方式几乎等同于RFCOMM的sockets方式,唯一的不同是通过L2CAP的方式,并且端口是0x1001到0x8FFF之间的奇数端口。默认的连接可以传送的可靠报文是672个字节。
1.服务器端程序
import bluetooth
server_sock=bluetooth.BluetoothSocket( bluetooth.L2CAP )
port = 0x1001
server_sock.bind(("",port))
server_sock.listen(1)
client_sock,address = server_sock.accept()
print "Accepted connection from ",address
data = client_sock.recv(1024)
print "received [%s]" % data
client_sock.close()
server_sock.close()
2.客户端程序
import bluetooth
sock=bluetooth.BluetoothSocket(bluetooth.L2CAP)
bd_addr = "01:23:45:67:89:AB"
port = 0x1001
sock.connect((bd_addr, port))
sock.send("hello!!")
sock.close()
调整MTU大小
l2cap_sock = bluetooth.BluetoothSocket( bluetooth.L2CAP )
.
. # connect the socket
.
bluetooth.set_l2cap_mtu( l2cap_sock, 65535 )
http://people.csail.mit.edu/albert/bluez-intro/c212.html