【应用笔记】蓝牙开发的“瑞士军刀”!在Windows 环境下使用nRF52840 BLE Dongle

前言

在 Windows 开发 BLE 时,系统自带的蓝牙栈往往会对硬件进行独占,导致开发者难以直接操控底层 HCI 数据。本文将介绍如何通过 Zephyr 固件将 nRF52840 Dongle 变成一个“透明”的 HCI Controller,并结合 Google 的 Bumble 协议栈实现完全的用户态控制。

软硬件环境

  • 硬件:nRF52840 Dongle
  • 软件
    • Windows 10/11 PC
    • 已配置完成的 Zephyr SDK (建议版本 v3.5 或以上)
    • Python 3.9+ 环境

固件编译:hci_usb 例程

Zephyr 内置了标准化的 hci_usb 示例,能将 SoC 的 BLE Controller 通过 USB 类协议暴露给主机。

west build -p -b nrf52840dongle/nrf52840 samples/bluetooth/hci_usb

固件烧录 (DFU 模式)

nRF52840 Dongle 没有板载调试器,必须通过 USB DFU 模式烧录。

1 . 安装 nrfutil

由于旧版 Python nrfutil 存在兼容性问题,建议直接从官网下载最新的工具nrfutil.exe,不要再使用过时的 pip install nrfutil。然后把下载好的工具放到系统环境目录下面,例如C:/Windows。

查看已安装组件

> nrfutil list
Command  Version  Description
device   2.16.1   Device discovery, programming, and operations such as erase, reset, and recovery.
Found 1 installed command(s)

搜索可用组件列表

> nrfutil search
Command            Installed  Latest  Status
91                            0.5.0   Not installed
ble-sniffer                   0.18.0  Not installed
completion                    1.5.0   Not installed
core               8.1.1      8.1.1   Installed
device             2.16.1     2.17.3  Upgradeable
mcu-manager                   0.9.0   Not installed
npm                           0.3.1   Not installed
nrf5sdk-tools                 1.1.0   Not installed
sdk-manager                   1.11.0  Not installed
suit                          0.9.0   Not installed
toolchain-manager             0.15.0  Not installed
trace                         4.7.1   Not installed

Found 12 installable command(s)

组件安装与更新操作

> nrfutil install device
nrfutil-device already installed - use '--force' to uninstall and reinstall the command 
###### 100% [Install packages] Install packages

> nrfutil install nrf5sdk-tools 
###### 100% [Install packages] Install packages

2. 进入 Bootloader

按下 Dongle 侧边的红色复位按钮,直到 LED 开始红色呼吸闪烁。此时设备管理器中应显示为 Open DFUBootloader。

> nrfutil device list
xxxxxxxxxxxx
Product         Open DFU Bootloader
Ports           COM43
Traits          nordicDfu, nordicUsb, serialPorts, usb

Supported devices found: 1

3. nrfutil 烧录

生成. zip 格式的下载镜像

nrfutil nrf5sdk-tools pkg generate --hw-version 52 --sd-req 0x00 --application build/zephyr/zephyr.hex --application-version 1 app_package.zip

对设备烧录,其中 COMX 使用上一步查找到的串口号,即 COM 43。

nrfutil nrf5sdk-tools dfu usb-serial -pkg app_package.zip -p COMX

替换 USB 驱动

完成程序下载之后,插拔 Dongle,可能显示为 Generic Bluetooth Adapter。我们需要强制将其切换为通用 USB 驱动。

1.  下载并运行 Zadig - USB driver installation made easy
2.  在菜单栏点击 Options -> 勾选 List All Devices。
3.  在下拉列表中找到你的 Generic Bluetooth Adapter(或者显示为 Zephyr USBD BT HCI)。
4.  在右侧的 Target 驱动框中选择 WinUSB 或 libusb-win32。
5.  点击 Replace Driver。
完成后,将会在通用串行总线设备下,显示一个新的设备Zephyr USBD BT HC

右键这个设备,选择属性->事件->信息。找到这个USB设备的VID和PID信息并记下来。

测试HCI指令

由于我们已经替换了 WinUSB 驱动,Bumble 可以跳过 Windows系统的蓝牙协议栈,直接操作 USB 端点(Endpoints)进行 HCI 通信。

使用 python 安装 Bumble,并使用 console 示例应用打开这个设备,其中usb:xxxx: xxxx 填入刚才记下来的 VID 和 PID 信息。

> python -m pip install bumble
> python -m bumble.apps.console usb:xxxx:xxxx

进入 Bumble console 以后,输入 scan on 即可扫描到你周围的广播包了。你也可以利用 Bumble 编写各种测试用例。

posted @ 2026-03-11 14:49  ixbwer  阅读(259)  评论(0)    收藏  举报