您好,登錄后才能下訂單哦!
1、bluepy 簡介
bluepy 是github上一個很好的藍牙開源項目,其地址在 LINK-1, 其主要功能是用python實現linux上BLE的接口。
This is a project to provide an API to allow access to Bluetooth Low Energy devices from Python. At present it runs on Linux only; I've mostly developed it using a Raspberry Pi, but it will also run on x86 Debian Linux.
支持python版本:The code is tested on Python 2.7 and 3.4; it should also work on 3.3.
2、安裝
直接源碼安裝,python3加持:
sudo apt-get install git build-essential libglib2.0-dev git clone https://github.com/IanHarvey/bluepy.git cd bluepy python3 setup.py build sudo python3 setup.py install
注:不要用python2,這輩子都不會用python2!
注:進行到這一步突然驚醒我的臺式機無藍牙,遂開啟我的無屏幕樹莓派,用命令找其ip,并用ssh登錄:
➜ Downloads sudo nmap -sS -p 22 192.168.31.0/24 | grep -B 5 -A 0 "Pi" Nmap scan report for 192.168.31.51 Host is up (0.19s latency). PORT STATE SERVICE 22/tcp open ssh MAC Address: B8:27:EB:71:33:AE (Raspberry Pi Foundation) ➜ Downloads ssh pi@192.168.31.51 pi@192.168.31.51's password: 1234
3、看文檔,玩DEMO
bluepy 的文檔地址 LINK-2
在bluepy中新建一個examples文件夾,用來存放接下來我們的測試DEMO:
3.1 scan devices demo
這里第一個DEMO是BLE設備掃描,這里用到了Scanner對象,該對象可以用來搜索BLE設備的廣播包數據。在大多數情況下該對象將會掃描出周圍所有可連接設備。
下面是我改造為python3的代碼:
➜ examples git:(master) ✗ cat scan.py #!/usr/bin/env python # coding=utf-8 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))
執行效果如下:
注:注意用sudo運行,更詳細的接口見 LINK-3
3.2 get services
bluepy 的DEMO有點少,我又找了個專是DEMO的github項目:LINK-5
將其中的getServices.py改造下:
➜ examples git:(master) ✗ cat get_setvices.py import sys from bluepy.btle import UUID, Peripheral if len(sys.argv) != 2: print("Fatal, must pass device address:", sys.argv[0], "<device address="">") quit() p = Peripheral(sys.argv[1],"public") services=p.getServices() #displays all services for service in services: print(service)
其中Peripheral(sys.argv[1],"public")是用mac地址創建一個連接,由于我們上一步用scan搜索到的mac地址為public類型,因此這里第二個參數為"public",更詳細的介紹見 LINK-6;
其中getServices會返回所連接設備的服務;
執行效果如下:
3.3 get characteristics
同3.2獲取characteristic的代碼如下:
➜ examples git:(master) ✗ cat get_characteristics.py import sys from bluepy.btle import UUID, Peripheral if len(sys.argv) != 2: print("Fatal, must pass device address:", sys.argv[0], "<device address="">") quit() p = Peripheral(sys.argv[1],"public") chList = p.getCharacteristics() print("Handle UUID Properties") print("-------------------------------------------------------") for ch in chList: print(" 0x"+ format(ch.getHandle(),'02X') +" "+str(ch.uuid) +" " + ch.propertiesToString())
執行效果如下:
3.4 get device name
直接上代碼:
➜ examples git:(master) ✗ cat get_device_name.py import sys from bluepy.btle import UUID, Peripheral dev_name_uuid = UUID(0x2A00) if len(sys.argv) != 2: print("Fatal, must pass device address:", sys.argv[0], "<device address="">") quit() p = Peripheral(sys.argv[1],"public") try: ch = p.getCharacteristics(uuid=dev_name_uuid)[0] if (ch.supportsRead()): print(ch.read()) finally: p.disconnect()
運行效果如下:
小結
bluepy 是非常棒的一款藍牙BLE工具,掌握它會為你節省比較多的時間~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。