时间:2021-05-23
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-devgit clone https://github.com/IanHarvey/bluepy.gitcd bluepypython3 setup.py buildsudo 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.51Host is up (0.19s latency).PORT STATE SERVICE22/tcp open sshMAC Address: B8:27:EB:71:33:AE (Raspberry Pi Foundation)➜ Downloads ssh pi@192.168.31.51pi@192.168.31.51's password: 12343、看文档,玩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-8from bluepy.btle import Scanner, DefaultDelegateclass 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 sysfrom bluepy.btle import UUID, Peripheralif 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 servicesfor 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 sysfrom bluepy.btle import UUID, Peripheralif 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 sysfrom bluepy.btle import UUID, Peripheraldev_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工具,掌握它会为你节省比较多的时间~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
想学习Python的同学们,在这里隆重介绍一款Python的开发工具pyCharmIDE。这是我最喜欢的Python编辑器利器。而且还是跨平台的,在MACO
首先介绍一款简单利落的分页利器:bootstrap-paginator,可以参考:BootstrapPaginator分页插件使用方法详解这篇文章进行学习。效果
介绍pidcat:pidcat是Android届JakeWharton大神开发的一款命令行工具,堪称Android开发利器,它能方便Android程序猿捕获日志
ngDraggable.js是一款比较简单实用的angularJS拖拽插件,借助于封装好的一些自定义指令,能够快速的进行一些拖拽应用开发。首先先介绍一些基本的概
Retrofit介绍Retrofit是Square开源的一款基于OkHttp(也是他家的)封装的网络请求框架,主要的网络请求还是OkHttp来完成,Retrof