bluepy 一款python封裝的BLE利器簡單介紹
1、bluepy 簡介
bluepy 是github上一個很好的藍牙開源項目,其地址在 LINK-1, 其主要功能是用python實現(xiàn)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設備的廣播包數(shù)據(jù)。在大多數(shù)情況下該對象將會掃描出周圍所有可連接設備。
下面是我改造為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))
- 其中Scanner([index=0])用于產(chǎn)生并初始化一個新的scanner對象,index 用來指名哪一個藍牙設備就會被用(默認0表示使用/dev/hci0)。掃描知道調用start或scan函數(shù)之后才會開始;
- 其中withDelegate(delegate)存儲對委托對象的引用,委托對象在接收來自設備的廣播時接收回調。有關詳細信息,請參閱DefaultDelegate的文檔;
- 其中scan([timeout = 10])開始掃描并帶有超時,在此掃描期間掃描到的設備會觸發(fā)Delegate的回調函數(shù),我們可以在其回調函數(shù)中實時獲取并打印。當超時后會返回一個設備列表;
執(zhí)行效果如下:
注:注意用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地址創(chuàng)建一個連接,由于我們上一步用scan搜索到的mac地址為public類型,因此這里第二個參數(shù)為"public",更詳細的介紹見 LINK-6;
其中getServices會返回所連接設備的服務;
執(zhí)行效果如下:
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())
執(zhí)行效果如下:
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工具,掌握它會為你節(jié)省比較多的時間~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
opencv-python+yolov3實現(xiàn)目標檢測
因為最近的任務有用到目標檢測,快速地了解了目標檢測這一任務,并且實現(xiàn)了使用opencv進行目標檢測。感興趣的可以了解一下2021-06-06Python中實現(xiàn)ipaddress網(wǎng)絡地址的處理
ipaddress庫提供了處理IPv4與IPv6網(wǎng)絡地址的類。這些類支持驗證,查找網(wǎng)絡上的地址和主機,以及其他常見的操作,本文就來介紹一下這些方法的使用,感興趣的一起來了解一下2021-06-06python3定位并識別圖片驗證碼實現(xiàn)自動登錄功能
這篇文章主要介紹了python3定位并識別圖片驗證碼實現(xiàn)自動登錄功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01