Python and Bluetooth

本文详细介绍了在Windows7系统下安装并解决PyBluez蓝牙库遇到的问题,包括必要的VCForPython27.msi安装步骤及目录修改方法。同时,提供了使用PyBluez库查找周围蓝牙设备及建立设备连接的Python代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

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蓝牙库与ESP32交互 对于希望利用Python通过蓝牙协议与ESP32设备通信的开发者而言,`pybluez`是一个常用的库,在Linux平台上表现良好[^1]。然而值得注意的是,由于操作系统差异,Windows用户可能遇到兼容性挑战。 为了实现基本的数据传输功能,下面提供了一个简单的客户端-服务器模型例子。在这个场景下,ESP32扮演服务器端角色等待连接并发送数据;而运行于PC上的Python程序作为客户端负责建立连接接收来自ESP32的消息: #### ESP32 (Arduino IDE) ```cpp #include <BluetoothSerial.h> #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it. #endif BluetoothSerial SerialBT; void setup() { Serial.begin(115200); SerialBT.begin("ESP32Test"); // 设备名称 } void loop() { if(SerialBT.available()){ String value = SerialBT.readString(); Serial.println(value); } } ``` 此部分代码配置了ESP32作为一个可被发现的服务端,并监听任何传入的信息请求。 #### PC Side (Python Script Using PyBluez) 安装PyBluez可以通过pip完成: ```bash pip install pybluez ``` 随后可以编写如下脚本来发起连接并向指定地址的目标设备发送消息: ```python import bluetooth target_name = "ESP32Test" 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(f'Found target device at {target_address}') else: print('Could not find target device.') sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM ) port = 1 sock.connect((target_address, port)) while True: try: message=input("Enter Message:") sock.send(message+"\n") data = sock.recv(1024) print(f"Received: {data}") except KeyboardInterrupt: print("\nClosing socket...") sock.close() break ``` 这段代码实现了扫描附近可用的蓝牙设备列表寻找名为`ESP32Test`的对象,并尝试与其建立RFCOMM通道下的链接用于双向通讯。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值