There is a well known way to enable HCI Bluetooth snoop log from Developer options UI.
Is there any way to achieve this programmatically?
1 回答
使用开发者选项:
- 如果您启用开发人员选项,那么您也可以在这些选项下启用蓝牙监听日志。
/data/misc/bluetooth/logs/
重新启动后,您应该在(不确定是否需要 root 才能访问这些文件)下找到您的日志文件,
使用 bt_stack.conf(需要 root)- (针对 Android 8.0+ 更新)
bt_stack.conf
可以在 下找到/system/etc/bluetooth
,现有的 conf 文件也可以在/data/misc/bluedroid
.
在大多数情况下,您必须使用以下步骤禁用验证:
adb -s <DEVICE> root
adb -s <DEVICE> disable-verity
adb -s <DEVICE> reboot
(应用更改)- 接下来再次输入root,重新启动后:
adb -s <DEVICE> root
- 然后重新挂载,
adb -s <DEVICE> remount
- 您也可以使用命令重新挂载
mount -o rw,remount <PARTITION>
- 然后您将能够推送文件,然后您可以进行更改+重新启动。
- 编辑
bt_stack.conf
文件(设置BtSnoopLogOutput=true
) - 禁用然后启用蓝牙 - 这将启动 HCI 监听日志记录
- 当您认为它足够时,在设置
BtSnoopLogOutput=false
和重置蓝牙时再次编辑文件 - 这将停止 HCI 监听记录 - 我通常做的是提取相应的文件,在我的最爱编辑器(通常
vi
或 VSCode)中进行更改,然后使用这些命令将其推回 adb -s <DEVICE> pull /system/etc/bluetooth/bt_stack.conf
adb -s <DEVICE> push bt_stack.conf /system/etc/bluetooth/.
- 除了
BTSnoop logging
,您还可以使用 启用所有堆栈跟踪bt_stack.conf
。
以下是文件在 Android 9.0 r34、MSM Kernel 4.4 上的样子:
root@console:/system/etc/bluetooth# cat bt_stack.conf
# Enable trace level reconfiguration function
# Must be present before any TRC_ trace level settings
TraceConf=true
# Trace level configuration
# BT_TRACE_LEVEL_NONE 0 ( No trace messages to be generated )
# BT_TRACE_LEVEL_ERROR 1 ( Error condition trace messages )
# BT_TRACE_LEVEL_WARNING 2 ( Warning condition trace messages )
# BT_TRACE_LEVEL_API 3 ( API traces )
# BT_TRACE_LEVEL_EVENT 4 ( Debug messages for events )
# BT_TRACE_LEVEL_DEBUG 5 ( Full debug messages )
# BT_TRACE_LEVEL_VERBOSE 6 ( Verbose messages ) - Currently supported for TRC_BTAPP only.
TRC_BTM=2
TRC_HCI=2
TRC_L2CAP=2
TRC_RFCOMM=2
TRC_OBEX=2
TRC_AVCT=2
TRC_AVDT=2
TRC_AVRC=2
TRC_AVDT_SCB=2
TRC_AVDT_CCB=2
TRC_A2D=2
TRC_SDP=2
TRC_SMP=2
TRC_BTAPP=2
TRC_BTIF=2
TRC_BNEP=2
TRC_PAN=2
TRC_HID_HOST=2
TRC_HID_DEV=2
# This is Log configuration for new C++ code using LOG() macros.
# See libchrome/base/logging.h for description on how to configure your logs.
# sample configuration:
#LoggingV=--v=0
#LoggingVModule=--vmodule=*/btm/*=1,btm_ble_multi*=2,btif_*=1
# PTS testing helpers
# Secure connections only mode.
# PTS_SecurePairOnly=true
# Disable LE Connection updates
#PTS_DisableConnUpdates=true
# Disable BR/EDR discovery after LE pairing to avoid cross key derivation errors
#PTS_DisableSDPOnLEPair=true
# SMP Pair options (formatted as hex bytes) auth, io, ikey, rkey, ksize
#PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
# PTS AVRCP Test mode
#PTS_AvrcpTest=true
# SMP Certification Failure Cases
# Set any of the following SMP error values (from smp_api_types.h)
# to induce pairing failues for various PTS SMP test cases.
# Setting PTS_SmpFailureCase to 0 means normal operation.
# Failure modes:
#
# SMP_PASSKEY_ENTRY_FAIL = 1
# SMP_PAIR_AUTH_FAIL = 3
# SMP_CONFIRM_VALUE_ERR = 4
# SMP_PAIR_NOT_SUPPORT = 5
# SMP_PAIR_FAIL_UNKNOWN = 8
# SMP_REPEATED_ATTEMPTS = 9
# SMP_NUMERIC_COMPAR_FAIL = 12
#PTS_SmpFailureCase=0
使用隐藏的 Android API
请注意,此方法将要求您的应用程序具有BLUETOOTH_ADMIN
权限。
如果没问题,您可以使用Android 系统设置应用程序使用的相同代码。
private void writeBtHciSnoopLogOptions() {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.configHciSnoopLog(true);
}
configHciSnoopLog
是BluetoothAdapter
class的隐藏 API 的一部分,因此请确保您知道如何启用它:
使用SL4A
SL4A 允许您直接在 Android 设备上编辑和执行脚本和交互式解释器,从而将脚本语言引入 Android。这些脚本可以访问成熟的 Android 应用程序可用的许多 API,但具有大大简化的界面,可以轻松完成工作。
如果您构建的 Android 映像支持 SL4A,则可以使用以下方法BluetoothFacade
:
@Rpc(description = "Enable or disable the Bluetooth HCI snoop log")
public boolean bluetoothConfigHciSnoopLog(
@RpcParameter(name = "value", description = "enable or disable log")
Boolean value
) {
return mBluetoothAdapter.configHciSnoopLog(value);
}
请注意,Github 上的API 参考已经过时,但您可以在官方 AOSP repo ( Common\src\com\googlecode\android_scripting\facade\bluetooth
)中查看最新的
启用 HCI 侦听日志的 Python 脚本如下所示:
from android import Android
droid = Android()
droid.bluetoothConfigHciSnoopLog(True)
完成日志记录后,您可以找到 HCI Snoop 登录/sdcard/btsnoop_hci.log