本文介绍在Raspberry Pi 3B上使用Python访问BLE设备。借助于第三方的Python模块bluepy (https://github.com/IanHarvey/bluepy),我们可以很容易地实现Python语言访问BLE设备。
具体的步骤如下:
- 在Raspberry Pi 3B上安装Ubuntu Mate系统(Raspbian也可以)
- 运行以下命令安装bluepy
sudo apt-get update
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev
sudo apt-get install libudev-dev libical-dev libreadline-dev
sudo pip install bluepy
- 以下为sample代码(http://ianharvey.github.io/bluepy-doc/)
from bluepy.btle import Scanner, DefaultDelegate
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
print "Discovered device", dev.addr
elif isNewData:
print "Received new data from", dev.addr
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
for dev in devices:
print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi)
for (adtype, desc, value) in dev.getScanData():
print " %s = %s" % (desc, value)
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148413.htm