python实现蓝牙通信
2023-09-14 08:56:57 时间
安装和示例
linux下安装
sudo apt-get install python-pip libglib2.0-dev sudo pip install bluepy
官方示例
import btle class MyDelegate(btle.DefaultDelegate): def __init__(self, params): btle.DefaultDelegate.__init__(self) # ... initialise here def handleNotification(self, cHandle, data): # ... perhaps check cHandle # ... process 'data' # Initialisation ------- p = btle.Peripheral( address ) p.setDelegate( MyDelegate(params) ) # Setup to turn notifications on, e.g. # svc = p.getServiceByUUID( service_uuid ) # ch = svc.getCharacteristics( char_uuid )[0] # ch.write( setup_data ) # Main loop -------- while True: if p.waitForNotifications(1.0): # handleNotification() was called continue print "Waiting..." # Perhaps do something else here
蓝牙通信模块pybluez的使用
选择蓝牙通信对象
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("")
通过RFCOMM方式进行通信
采用类似于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方式进行通信
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 )
refer:
An Introduction to Bluetooth Programming
相关文章
- Win10配置Airsim环境并设置Python通信
- Python多线程通信_python socket多线程
- pycharm打包python项目_Python怎么打包
- python截图识别文字_Python文字截图识别OCR工具实例解析
- python基础系列教程——Python的安装与测试:python解释器、PyDev编辑器、pycharm编译器
- Python元组_python输出元组
- python全局变量赋值_Python全局变量和局部变量[通俗易懂]
- python表情代码_Python实现表情包的代码实例[通俗易懂]
- 【说站】python字典中键值对的操作
- python udp通信
- Python抛出异常_python抛出异常的作用
- python判断文件后缀_Python 判断文件后缀是否被篡改
- Python将数据写入txt文件_python将内容写入txt文件
- Python输入输出函数_Python自学第四节
- Python 计算文件中总行数
- 搭建python开发环境-c++教程 如何搭建Python开发环境?
- 【测试开发】python系列教程:Python注释和解释器
- Python break 语句
- 软件测试|Python基础之面向对象(二)
- Python – 打包为exe
- python-Python与MySQL数据库-使用Python执行MySQL查询
- python-Python与SQLite数据库-SQLite数据库的基本知识(二)
- python-Python与SQLite数据库-处理SQLite查询结果(一)
- 学习Python精通SQL Server操作技巧(python操作sqlserver)
- Python Queue队列实现线程通信
- Python Event实现线程通信
- Python结合MySQL实现信息交互(python与mysql交互)
- Python操作Redis实现数据持久化(python操作redis)
- Python脚本实现Linux系统管理及自动化部署(python写linux)
- Python实现Oracle数据库连接(python连接oracle数据库)
- 从 Python 连接到 MySQL:实现更多强大的数据库应用(python和mysql)
- Python脚本实现Linux命令快捷控制(python执行linux命令)
- Python与MySQL实现数据分析的完美组合(mysql中python)
- python用Redis与Python实现大数据收集与分析(redis 联合)