基于python实现蓝牙通信代码实例

所属分类: 脚本专栏 / python 阅读数: 1394
收藏 0 赞 0 分享

这篇文章主要介绍了基于python实现蓝牙通信代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

安装和示例

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 )

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

深入浅析python3中的unicode和bytes问题

在python3中,有两种字符串类型,默认的就是str,即unicode,也叫做文本类型。这篇文章主要介绍了python3中的unicode和bytes问题,需要的朋友可以参考下
收藏 0 赞 0 分享

python3 自动识别usb连接状态,即对usb重连的判断方法

今天小编就为大家分享一篇python3 自动识别usb连接状态,即对usb重连的判断方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python二进制文件的转译详解

这篇文章主要介绍了python二进制文件的转译详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python字符串中匹配数字的正则表达式

正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。这篇文章主要介绍了python字符串中匹配数字的正则表达式 ,需要的朋友可以参考下
收藏 0 赞 0 分享

在Python中COM口的调用方法

今天小编就为大家分享一篇在Python中COM口的调用方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python read函数按字节(字符)读取文件的实现

这篇文章主要介绍了Python read函数按字节(字符)读取文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python读取图片的方式,以及将图片以三维数组的形式输出方法

今天小编就为大家分享一篇python读取图片的方式,以及将图片以三维数组的形式输出方法,具有好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

在python中利用numpy求解多项式以及多项式拟合的方法

今天小编就为大家分享一篇在python中利用numpy求解多项式以及多项式拟合的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python正则表达式匹配数字和小数的方法

这篇文章主要介绍了Python正则匹配数字和小数的方法,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python读写配置文件操作示例

这篇文章主要介绍了python读写配置文件操作,结合实例形式分析了Python针对ini配置文件的读取、解析、写入等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多