这篇文章主要介绍了基于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
con