首頁 > 軟體

在Raspberry Pi 3B上使用Python存取BLE裝置

2020-06-16 17:02:25

本文介紹在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
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


IT145.com E-mail:sddin#qq.com