zephyr学习蓝牙部分(五) - Bluetooth tools

This page lists and describes tools that can be used to assist during Bluetooth stack or application development in order to help, simplify and speed up the development process.

此页面列出并描述了可用于在蓝牙堆栈或应用程序开发期间提供帮助的工具,以帮助、简化和加快开发过程。

Mobile applications

It is often useful to make use of existing mobile applications to interact with hardware running Zephyr, to test functionality without having to write any additional code or requiring extra hardware.

利用现有的移动应用程序与运行Zephyr的硬件进行交互、测试功能而不必编写任何额外的代码或需要额外的硬件通常很有用。

The recommended mobile applications for interacting with Zephyr are:

  • Android:
    • nRF Connect for Android
    • nRF Mesh for Android
    • LightBlue for Android
  • iOS:
    • nRF Connect for iOS
    • nRF Mesh for iOS
    • LightBlue for iOS

Using BlueZ with Zephyr

The Linux Bluetooth Protocol Stack, BlueZ, comes with a very useful set of tools that can be used to debug and interact with Zephyr’s BLE Host and Controller. In order to benefit from these tools you will need to make sure that you are running a recent version of the Linux Kernel and BlueZ:

Linux蓝牙协议栈Bluez提供了一套非常有用的工具,可用于调试Zephyr的BLE主机和控制器并与之交互。为了从这些工具中获益,您需要确保运行的是最新版本的Linux内核和Bluez:

  • Linux Kernel 4.10+
  • BlueZ 4.45+

Additionally, some of the BlueZ tools might not be bundled by default by your Linux distribution. If you need to build BlueZ from scratch to update to a recent version or to obtain all of its tools you can follow the steps below:

此外,Linux发行版可能不会默认捆绑某些Bluez工具。如果您需要从头开始构建Bluez以更新到最新版本或获取其所有工具,可以执行以下步骤:

git clone git://git.kernel.org/pub/scm/bluetooth/bluez.git
cd bluez
./bootstrap-configure --disable-android --disable-midi
make

You can then find btattachbtmgt and btproxy in the tools/ folder and btmon in the monitor/ folder.

You’ll need to enable BlueZ’s experimental features so you can access its most recent BLE functionality. Do this by editing the file/lib/systemd/system/bluetooth.service and making sure to include the -E option in the daemon’s execution start line:

ExecStart=/usr/libexec/bluetooth/bluetoothd -E

Finally, reload and restart the daemon:

sudo systemctl daemon-reload
sudo systemctl restart bluetooth

Running on QEMU and Native POSIX

It’s possible to run Bluetooth applications using either the QEMU emulator or Native POSIX. In either case, a Bluetooth controller needs to be exported from the host OS (Linux) to the emulator. For this purpose you will need some tools described in the Using BlueZ with Zephyr section.

可以使用qemu模拟器或本地posix运行蓝牙应用程序。在这两种情况下,都需要将蓝牙控制器从主机OS(Linux)导出到模拟器。为此,您需要一些工具,如“将Bluez与Zephyr结合使用”部分所述。

Using the Host System Bluetooth Controller

The host OS’s Bluetooth controller is connected in the following manner:

  • To the second QEMU serial line using a UNIX socket. This socket gets used with the help of the QEMU option -serial unix:/tmp/bt-server-bredr. This option gets passed to QEMU through QEMU_EXTRA_FLAGS automatically whenever an application has enabled Bluetooth support.
  • To a serial port in Native POSIX through the use of a command-line option passed to the Native POSIX executable: --bt-dev=hci0

On the host side, BlueZ allows you to export its Bluetooth controller through a so-called user channel for QEMU and Native POSIX to use.

Note

You only need to run btproxy when using QEMU. Native POSIX handles the UNIX socket proxying automatically

If you are using QEMU, in order to make the Controller available you will need one additional step using btproxy:

  1. Make sure that the Bluetooth controller is down

  2. Use the btproxy tool to open the listening UNIX socket, type:

    sudo tools/btproxy -u -i 0
    Listening on /tmp/bt-server-bredr
    

    You might need to replace -i 0 with the index of the Controller you wish to proxy.

Once the hardware is connected and ready to use, you can then proceed to building and running a sample:

  • Choose one of the Bluetooth sample applications located in samples/bluetooth.

  • To run a Bluetooth application in QEMU, type:

    cd $ZEPHYR_BASE/samples/bluetooth/
    mkdir build && cd build
    cmake -GNinja -DBOARD=qemu_x86 ..
    ninja run
    

    Running QEMU now results in a connection with the second serial line to the bt-server-bredr UNIX socket, letting the application access the Bluetooth controller.

  • To run a Bluetooth application in Native POSIX, first build it:

    cd $ZEPHYR_BASE/samples/bluetooth/
    mkdir build && cd build
    cmake -GNinja -DBOARD=native_posix ..
    ninja
    

    And then run it with:

    $ sudo zephyr/zephyr.exe --bt-dev=hci0
    

Using a Zephyr-based BLE Controller

Depending on which hardware you have available, you can choose between two transports when building a single-mode, Zephyr-based BLE Controller:

  • UART: Use the hci_uart sample and follow the instructions in Using the controller with QEMU and Native POSIX.
  • USB: Use the hci_usb sample and then treat it as a Host System Bluetooth Controller (see previous section)

HCI Tracing

When running the Host on a computer connected to an external Controller, it is very useful to be able to see the full log of exchanges between the two, in the format of a Host Controller Interface log. In order to see those logs, you can use the built-in btmon tool from BlueZ:

当在连接到外部控制器的计算机上运行主机时,能够以主机控制器接口日志的格式查看两台计算机之间的完整交换日志是非常有用的。要查看这些日志,可以使用Bluez中内置的btmon工具:

$ btmon

Using Zephyr-based Controllers with BlueZ

If you want to test a Zephyr-powered BLE Controller using BlueZ’s Bluetooth Host, you will need a few tools described in the Using BlueZ with Zephyr section. Once you have installed the tools you can then use them to interact with your Zephyr-based controller:

sudo tools/btmgmt --index 0
[hci0]# auto-power
[hci0]# find -l

You might need to replace --index 0 with the index of the Controller you wish to manage. Additional information about btmgmt can be found in its manual pages.

你可能感兴趣的:(zephyr,BLE,Bluetooth开发)