3 Star 18 Fork 7

Walkline / MicroPython BLE Library

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

MicroPython BLE Library

项目介绍

根据之前研究学习到的 Micropython BLE 相关知识,将相关代码模块封装成为类库,方便以后使用

BLE 相关名词解释

为了后文便于理解,也避免出现关贸总协定这样怪异的翻译,特此对几个名词做出我个人的理解,如有误导欢迎斧正

Profile

可以理解为 BLE 设备作为某个指定用途的说明文档,或者概述、简介等,比如作为HID设备(通常为键盘鼠标摇杆等外设)时,必须要实现以下三个服务:

  • Battery Service
  • Device Information
  • Human Interface Device

对这 3 个必要服务的说明就是Profile的作用

Services

项目中提供的 Services 以及后面的 Characteristics 和 Descriptors 都是由 Bluetooth® Technology Website 定义的一些统一规范,所有人按照规范进行蓝牙硬件设备开发,在不需要额外开发软件的前提下我们的硬件设备即可以被诸如 PC、手机等中心设备(Central)识别并使用

服务的作用是告知中心设备我们的硬件将会提供什么样的服务,比如上述HID的 Profile 包含了 3 个服务,中心设备就会把它识别为一个键盘鼠标类的外围设备(Peripheral)

Characteristics

名为特征,可以理解为服务的属性参数,就像一个 Profile 可以包含多个服务一样,服务也可以包含多个特征,用于具体描述某个服务的某项特征值

比如Battery Service服务包含了Battery Level特征,向它写入一个 0 到 100 的数值,就可以告知中心设备当前我们的设备电量情况

Descriptors

和服务一样,每个特征还可以包含多个描述符,描述符的作用是对特征的额外设置,比如一些中心设备会向我们的外围设备中的某个特征发送通知(Notify),描述符可以设置是否接收通知,等

如何使用?

下面以main.py为例讲解如何使用类库生成一个 Profile

假设你已经掌握了MicroPython ubluetooth模块的基本使用方法

导入类库文件

import ubluetooth as bt
from ble.const import BLEConst
from ble.tools import BLETools
from ble.profile.manager import ProfileManager
from ble.services import *
from ble.characteristics import *
from ble.descriptors import *

生成 Profile

profile = ProfileManager()

profile.add_services(
    BatteryService().add_characteristics(
        Characteristics(BLEConst.Characteristics.BATTERY_LEVEL, BLEConst.CharacteristicFlags.BATTERY_LEVEL_)
    ),
    HumanInterfaceDevice().add_characteristics(
        Characteristics(BLEConst.Characteristics.REPORT, BLEConst.CharacteristicFlags.REPORT_).add_descriptors(
            ClientCharacteristicConfiguration(),
        )
    ),
)

这里给 Profile 添加了 2 个服务,每个服务添加了 1 个特征和一个额外的描述符

  • Battery Service(服务)
    • Battery Level(特征)
  • Human Interface Device(服务)
    • Report(特征)
      • Client Characteristic Configuration(描述符)

本来计划中应该使用更一目了然更直观的方式生成 Profile 的,例如这样

# 定义 HID 设备必须的 3 个服务
profile.add_services(
    HumanInterfaceDevice().add_characteristics(
        ReportMap(),
        Report(),
        ProtocolMode(),
        HIDInformation(),
        HIDControlPoint(),
        BootMouseInputReport(),
    ),
    DeviceInformation().add_characteristics(
        PNPID(),
    ),
    BatteryService().add_characteristics(
        BatteryLevel(),
    ),
)

但是鉴于官方定义的特征实在太多了,一一简化倒是会方便开发,但是相应的类库文件就会非常巨大(相对于 ESP32 的 4M 存储来说),无奈只能选择使用比较麻烦的方法了,但是服务和描述符相对较少,所以依然使用了简化方式

注册服务

注册服务,获取特征和描述符句柄,用于和中心设备交互

定义句柄的顺序必须和定义 Profile 时候的顺序一致

ble = bt.BLE()
services = profile.get_services()

(
    (
        handle_battery_level,
    ),
    (
        handle_report,
        handle_report_desc,
    ),
) = ble.gatts_register_services(services)

生成广播信息

为避免广播内容太多导致广播失败,建议将设备名称放置到Scan Response

uuids = profile.get_services_uuid()
appearance = BLEConst.Appearance.GENERIC_HEART_RATE_SENSOR

adv_payload = BLETools.advertising_generic_payload(
    services=uuids,
    appearance=appearance
)
resp_payload = BLETools.advertising_resp_payload(
    name="BLELibTest"
)

广播信息

广播 Profile 包含的 Services 信息,中心设备收到信息后经过分析可以展示给用户

ble.gap_advertise(None)
ble.gap_advertise(interval_us=100000, adv_data=adv_payload, resp_data=resp_payload)

向中心设备传递消息通知

先在设备本地写入一个电量值,然后使用Notify方法告知中心设备

import struct

ble.gatts_write(handle_battery_level, struct.pack("<B", int(100)))
ble.gatts_notify(conn_handle, handle_battery_level)

handle_battery_level注册服务时获取的句柄

conn_handle是中心设备连接时的连接句柄

基本使用方法就是如此了,下面看一下中心设备展示给用户的内容吧

查看一下效果吧

因为示例中使用的 Profile 为随机搭配的内容,所以并不能在电脑、手机上直接搜索到,我们需要借助第三方 APP 进行查看,比如 nRF Connect

Screenshot01

Screenshot02

REPL中完整的输出内容如下

activating ble...
I (57150) BTDM_INIT: BT controller compile version [5aed448]
I (60951) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (61051) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
GAP procedure initiated: stop advertising.
ble activated
services registed
GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=7 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=160 adv_itvl_max=160
advertising...
>>> [52:5F:AF:24:C3:38] connected, handle: 0
[52:5F:AF:24:C3:38] disconnected, handle: 0
GAP procedure initiated: advertise; disc_mode=2 adv_channel_map=7 own_addr_type=0 adv_filter_policy=0 adv_itvl_min=160 adv_itvl_max=160
advertising...

下载烧录自定义固件

最简便的使用本类库的方式就是下载并烧录我的自定义固件,免去上传冗长文件的过程

访问 自定义固件下载项目 下载最新的自定义固件,并参考 附录1:如何刷写固件 烧录固件到开发板

合作交流

  • 联系邮箱:walkline@163.com
  • QQ 交流群:
    • 走线物联:163271910
    • 扇贝物联:31324057

走线物联扇贝物联

MIT License Copyright (c) 2020 Walkline Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

根据之前研究学习到的 Micropython BLE 相关知识,将相关代码模块封装成为类库,方便以后使用 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/walkline/micropython-ble-library.git
git@gitee.com:walkline/micropython-ble-library.git
walkline
micropython-ble-library
MicroPython BLE Library
master

搜索帮助