您好,登錄后才能下訂單哦!
1、bluepy 簡(jiǎn)介
bluepy 是github上一個(gè)很好的藍(lán)牙開(kāi)源項(xiàng)目,其地址在 LINK-1, 其主要功能是用python實(shí)現(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,這輩子都不會(huì)用python2!
注:進(jìn)行到這一步突然驚醒我的臺(tái)式機(jī)無(wú)藍(lán)牙,遂開(kāi)啟我的無(wú)屏幕樹(shù)莓派,用命令找其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中新建一個(gè)examples文件夾,用來(lái)存放接下來(lái)我們的測(cè)試DEMO:
3.1 scan devices demo
這里第一個(gè)DEMO是BLE設(shè)備掃描,這里用到了Scanner對(duì)象,該對(duì)象可以用來(lái)搜索BLE設(shè)備的廣播包數(shù)據(jù)。在大多數(shù)情況下該對(duì)象將會(huì)掃描出周?chē)锌蛇B接設(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))
執(zhí)行效果如下:
注:注意用sudo運(yùn)行,更詳細(xì)的接口見(jiàn) LINK-3
3.2 get services
bluepy 的DEMO有點(diǎn)少,我又找了個(gè)專(zhuān)是DEMO的github項(xiàng)目: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)建一個(gè)連接,由于我們上一步用scan搜索到的mac地址為public類(lèi)型,因此這里第二個(gè)參數(shù)為"public",更詳細(xì)的介紹見(jiàn) LINK-6;
其中g(shù)etServices會(huì)返回所連接設(shè)備的服務(wù);
執(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()
運(yùn)行效果如下:
小結(jié)
bluepy 是非常棒的一款藍(lán)牙BLE工具,掌握它會(huì)為你節(jié)省比較多的時(shí)間~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。