蓝牙le和经典蓝牙区别
At Propeller Health we develop products that help people with asthma or COPD manage their condition. One component of our platform is a sensor that attaches to a person’s inhaled medication and sends medication usage data to a connected device over Bluetooth Low Energy (BLE).
在Propeller Health,我们开发可帮助哮喘或COPD患者控制病情的产品。 我们平台的一个组成部分是一个传感器,该传感器附着在一个人的吸入药物上,并通过蓝牙低功耗(BLE)将药物使用数据发送到连接的设备。
BLE communication in Android can be finicky for many reasons, including inconsistencies in hardware such as Bluetooth adapters (radios), software such as the firmware the Bluetooth adapter is programmed with, and vendor implementations of the Android OS.
由于许多原因,Android中的BLE通信可能很挑剔,包括蓝牙适配器(无线电)之类的硬件不一致,蓝牙适配器编程所用的固件之类的软件以及Android OS的供应商实现。
What if you were asked to troubleshoot one of these finicky devices? Wouldn’t it be useful if you could see what is being transmitted between them? Well, thankfully there is packet capture, and Android provides a way to do this for BLE communication. Performing a BLE packet capture is a good way to inspect the data being transferred between central (e.g. an Android phone) and peripheral devices (e.g. a Propeller sensor). This can be useful to verify that the data transmitted between the devices is what is expected.
如果要求您对这些挑剔的设备之一进行故障排除怎么办? 如果您能看到它们之间正在传输的内容,那将不会有用吗? 好了,幸好有数据包捕获,Android提供了一种方法来进行BLE通信。 执行BLE数据包捕获是检查在中央设备(例如Android手机)和外围设备(例如螺旋桨传感器)之间传输的数据的好方法。 这对于验证设备之间传输的数据是否是预期的很有用。
蓝牙数据包捕获如何在Android上工作 (How Bluetooth packet capture works on Android)
你需要什么 (What you’ll need)
- An Android device running Android 4.4 (KitKat) or greater to use as the central device 运行Android 4.4(KitKat)或更高版本的Android设备用作中央设备
- A BLE device to use as the peripheral device 用作外围设备的BLE设备
- A Windows, Linux or Mac workstation with the Android SDK tools installed 安装了Android SDK工具的Windows,Linux或Mac工作站
启用开发者选项 (Enable Developer Options)
As previously mentioned, Developer options need to be enabled in order to intercept and store packets as they are being sent and received. In order to enable Developer options perform the following:
如前所述,需要启用“开发人员”选项,以便在发送和接收数据包时对其进行拦截和存储。 为了启用开发人员选项,请执行以下操作:
- On stock Android open Settings > About phone > Build number 安卓系统上打开打开设置>关于手机>内部版本号
Tap Build number seven times. After the first few taps, you should see the steps counting down until you unlock the developer options. Once activated you’ll see a message that reads You are now a developer!
点击内部版本号七次。 轻按前几下后,您应该看到倒数的步骤,直到解锁开发人员选项为止。 激活后,您将看到一条消息,提示您现在是开发人员!
- Go back to Settings > System > Advanced where you’ll now find a Developer options entry or use the search feature to search for “Developer options” 返回设置>系统>高级,您现在可以在其中找到开发人员选项条目,或使用搜索功能搜索“开发人员选项”
Note, it’s not recommended to leave Developer options enabled longer than necessary as device security is reduced. We are capturing and storing data meant to be transferred between the central and peripheral devices after all and that exposes some risk.
请注意,由于设备安全性降低,因此不建议将开发人员选项的启用时间保持在不必要的时间。 毕竟,我们正在捕获和存储要在中央和外围设备之间传输的数据,这暴露了一些风险。
启用蓝牙HCI监听日志 (Enable Bluetooth HCI snoop log)
Now that we have Developer options enabled, we can enable the Bluetooth HCI snoop log. When the Bluetooth HCI snoop log is enabled, the Android framework will capture Bluetooth packets sent and received between central and peripheral devices and store them as part of a bug report. To enable the Bluetooth HCI snoop log:
现在我们启用了开发人员选项,我们可以启用蓝牙HCI监听日志。 启用蓝牙HCI监听日志后,Android框架将捕获在中央和外围设备之间发送和接收的蓝牙数据包,并将其存储为错误报告的一部分。 要启用蓝牙HCI监听日志:
- On stock Android open Settings > System > Advanced > Developer options 在Android上,打开设置>系统>高级>开发人员选项
- Tap Enable Bluetooth HCI snoop log 点击启用蓝牙HCI监听日志
- Tap the Enable option on the dialog that displays 在显示的对话框中点击“启用”选项
After the Bluetooth HCI snoop log has been enabled you will need to restart the Bluetooth adapter which you can do by turning Bluetooth off and back on. All Bluetooth packets sent and received by the Bluetooth adapter will now be saved to the Bluetooth HCI snoop log.
启用蓝牙HCI监听日志后,您将需要重新启动蓝牙适配器,这可以通过关闭和重新打开蓝牙来完成。 蓝牙适配器发送和接收的所有蓝牙数据包现在都将保存到蓝牙HCI监听日志中。
启动BLE通信 (Initiate BLE communication)
Now that we have our central device configured to capture Bluetooth traffic being transmitted we can initiate communication between the central and peripheral devices. One way to initiate communication for Propeller sensors is by performing a manual sync by pressing the auxiliary button on a sensor. When the auxiliary button is pressed Propeller sensors will send advertising packets at a much higher rate for a short period of time. When the central scanner receives the advertising packets the data will be logged to the Bluetooth HCI snoop log.
现在我们已经将中央设备配置为捕获正在传输的蓝牙流量,我们可以启动中央设备与外围设备之间的通信。 发起螺旋桨传感器通信的一种方法是通过按下传感器上的辅助按钮执行手动同步。 当按下辅助按钮时,螺旋桨传感器将在短时间内以更高的速率发送广告包。 当中央扫描仪收到广告数据包时,数据将被记录到蓝牙HCI监听日志中。
提取蓝牙HCI监听日志 (Extract the Bluetooth HCI snoop log)
In order to extract the Bluetooth HCI snoop log from the Android device you need to connect the Android device to your workstation and use the adb command to generate a bug report:
为了从Android设备提取Bluetooth HCI监听日志,您需要将Android设备连接到工作站并使用adb命令生成错误报告:
adb bugreport bugreport
adb bugreport bugreport
This will extract the bug report to a zip file named bugreport.zip on your workstation. Unzip the bug report:
这会将错误报告解压缩到工作站上名为bugreport.zip的zip文件中。 解压缩错误报告:
unzip bugreport.zip
解压缩bugreport.zip
Locate the file named btsnoop_hci.log in the extracted files, this is the file which contains the packet capture.
在提取的文件中找到名为btsnoop_hci.log的文件,这是包含数据包捕获的文件。
禁用蓝牙HCI监听日志 (Disable Bluetooth HCI snoop log)
After you’ve captured the traffic you’re interested in the Bluetooth HCI snoop log can be disabled. You can disable the Bluetooth HCI snoop log the same way it was enabled:
捕获流量后,可以禁用对Bluetooth HCI监听日志感兴趣的流量。 您可以使用启用蓝牙的方式禁用蓝牙HCI监听日志:
- On stock Android open Settings > System > Advanced > Developer options 在Android上,打开设置>系统>高级>开发人员选项
- Tap Enable Bluetooth HCI snoop log 点击启用蓝牙HCI监听日志
- Tap the Disable option on the dialog that displays 在显示的对话框中点击“禁用”选项
Again, you must also toggle Bluetooth services in order to restart the Bluetooth adapter. Additionally, now may also be a good time to disable Developer options if you are done troubleshooting.
同样,您还必须切换蓝牙服务才能重新启动蓝牙适配器。 此外,如果您已完成疑难解答,那么现在可能也是禁用开发人员选项的好时机。
分析蓝牙HCI监听日志 (Analyze the Bluetooth HCI snoop log)
Now that you have a packet capture you can load it into your favorite network protocol analyzer. A popular and well known network protocol analyzer is Wireshark. Understanding how to configure Wireshark display filters is outside the scope of this blog post, however if you know the mac address of the peripheral device you’ll find the following display filter useful:
现在您有了数据包捕获,可以将其加载到您喜欢的网络协议分析器中。 Wireshark是一种流行且广为人知的网络协议分析器。 了解如何配置Wireshark显示过滤器超出了本博客文章的范围,但是,如果您知道外围设备的mac地址,则会发现以下显示过滤器很有用:
bthci_evt.bd_addr == xx:xx:xx:xx:xx:xx || bthci_cmd.bd_addr == xx:xx:xx:xx:xx:xx || bluetooth.addr == xx:xx:xx:xx:xx:xx
bthci_evt.bd_addr == xx:xx:xx:xx:xx:xx:xx || bthci_cmd.bd_addr == xx:xx:xx:xx:xx:xx:xx || bluetooth.addr == xx:xx:xx:xx:xx:xx:xx
结论 (Conclusion)
Troubleshooting BLE communication on an Android device can feel like working with a black box. You now have another tool in your arsenal to build a deeper understanding of the packets sent and received by peripheral devices. The next time you’re left wondering why your BLE device isn’t communicating properly, fire up the Bluetooth HCI snoop log, capture some packets and take a look at what your central and peripheral devices are saying to each other, you may be surprised at what you find!
对Android设备上的BLE通信进行故障排除可能就像使用黑匣子一样。 现在,您的武库中有了另一个工具,可以更深入地了解外围设备发送和接收的数据包。 下次您不知道为什么BLE设备无法正常通信,启动Bluetooth HCI监听日志,捕获一些数据包并查看您的中央设备和外围设备在互相说什么时,您可能会感到惊讶根据您的发现!
翻译自: https://medium.com/propeller-health-tech-blog/bluetooth-le-packet-capture-on-android-a2109439b2a1
蓝牙le和经典蓝牙区别