溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

bluepy 一款python封裝的BLE利器簡(jiǎn)單介紹

發(fā)布時(shí)間:2020-09-23 00:45:33 來(lái)源:腳本之家 閱讀:254 作者:beautifulzzzz 欄目:開(kāi)發(fā)技術(shù)

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))
  • 其中Scanner([index=0])用于產(chǎn)生并初始化一個(gè)新的scanner對(duì)象,index 用來(lái)指名哪一個(gè)藍(lán)牙設(shè)備就會(huì)被用(默認(rèn)0表示使用/dev/hci0)。掃描知道調(diào)用start或scan函數(shù)之后才會(huì)開(kāi)始;
  • 其中withDelegate(delegate)存儲(chǔ)對(duì)委托對(duì)象的引用,委托對(duì)象在接收來(lái)自設(shè)備的廣播時(shí)接收回調(diào)。有關(guān)詳細(xì)信息,請(qǐng)參閱DefaultDelegate的文檔;
  • 其中scan([timeout = 10])開(kāi)始掃描并帶有超時(shí),在此掃描期間掃描到的設(shè)備會(huì)觸發(fā)Delegate的回調(diào)函數(shù),我們可以在其回調(diào)函數(shù)中實(shí)時(shí)獲取并打印。當(dāng)超時(shí)后會(huì)返回一個(gè)設(shè)備列表;

執(zhí)行效果如下:

bluepy 一款python封裝的BLE利器簡(jiǎn)單介紹

注:注意用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í)行效果如下:

bluepy 一款python封裝的BLE利器簡(jiǎn)單介紹

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í)行效果如下:

bluepy 一款python封裝的BLE利器簡(jiǎn)單介紹

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)行效果如下:

bluepy 一款python封裝的BLE利器簡(jiǎn)單介紹

小結(jié)

bluepy 是非常棒的一款藍(lán)牙BLE工具,掌握它會(huì)為你節(jié)省比較多的時(shí)間~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI