增强蓝牙dumpstate log 状态码可读性的python脚本

logcat中,我们经常看到这样的蓝牙log:

01-01 09:48:37.479  1326  1377 D BluetoothManagerService: Sending off request.
01-01 09:48:37.479  6111 16098 D BluetoothAdapterService(312951812): disable() called...
01-01 09:48:37.479  6111  6138 D BluetoothAdapterState: CURRENT_STATE=ON, MSG = USER_TURN_OFF
01-01 09:48:37.479  6111  6138 D BluetoothAdapterProperties: Setting state to 13
01-01 09:48:37.479  6111  6138 I BluetoothAdapterState: Bluetooth adapter state changed: 12-> 13
01-01 09:48:37.479  6111  6138 D BluetoothAdapterService: Bluetooth PBAP supproted is true
01-01 09:48:37.479  6111  6138 D BluetoothAdapterService: updateAdapterState state is 13
01-01 09:48:37.479  6111  6138 D BluetoothAdapterService(312951812): updateAdapterState() - Broadcasting state to 1 receivers.
01-01 09:48:37.479  6111  6111 I BluetoothPbapService: action: android.bluetooth.adapter.action.STATE_CHANGED, state: 13
01-01 09:48:37.489  6111  6138 D BluetoothAdapterService: Autoconnection is available 
01-01 09:48:37.489  6111  6138 D BluetoothAdapterService: updateAdapterState prevState = 12newState = 13
01-01 09:48:37.489  6111  6138 D BluetoothAdapterService: terminating service from this receiver

其中的状态码12 13等可读性十分差,经常忘记其代表的含义。

于是我写了一个python程序,将这些状态码转换成可读性强的表述,如下所示:

原始log:

HeadsetStateMachine: Connected process message: 9
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage: returning 78:F7:BE:01:AA:84
HeadsetStateMachine: mNumActive: 0 mNumHeld: 0 mCallState: 2
HeadsetStateMachine: mNumber:  mType: 128
HeadsetStateMachine: terminateScoUsingVirtualVoiceCall: Received
HeadsetStateMachine: terminateScoUsingVirtualVoiceCall:No present call to terminate
HeadsetStateMachine: PreferredDevice : 78:F7:BE:01:AA:84
HeadsetStateMachine: setAudioParameters
HeadsetStateMachine: Set NREC: 1(ON) for device:78:F7:BE:01:AA:84
HeadsetStateMachine: Set SampleRate: 8000(NB) for device:78:F7:BE:01:AA:84
HeadsetStateMachine: Connected process message: 12
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage(CLCC): No matching device for 104. Returning null
HeadsetStateMachine: checkBlacklistWithId(78:F7:BE:01:AA:84) : false, id : 2
HeadsetStateMachine: processSendClccResponse: Failed to get device for CLCC response. Drop response
HeadsetStateMachine: Connected process message: 12
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage(CLCC): No matching device for 104. Returning null
HeadsetStateMachine: checkBlacklistWithId(78:F7:BE:01:AA:84) : false, id : 2
HeadsetStateMachine: processSendClccResponse: Failed to get device for CLCC response. Drop response
HeadsetStateMachine: Connected process message: 101
HeadsetStateMachine: event type: 17
HeadsetStateMachine: EVENT_TYPE_WBS codec is 1

处理后:

HeadsetStateMachine: Connected process message: 9 (CALL_STATE_CHANGED) 
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage: returning 78:F7:BE:01:AA:84
HeadsetStateMachine: mNumActive: 0 mNumHeld: 0 mCallState: 2 (CALL_STATE_DIALING) 
HeadsetStateMachine: mNumber:  mType: 128
HeadsetStateMachine: terminateScoUsingVirtualVoiceCall: Received
HeadsetStateMachine: terminateScoUsingVirtualVoiceCall:No present call to terminate
HeadsetStateMachine: PreferredDevice : 78:F7:BE:01:AA:84
HeadsetStateMachine: setAudioParameters
HeadsetStateMachine: Set NREC: 1(ON) for device:78:F7:BE:01:AA:84
HeadsetStateMachine: Set SampleRate: 8000(NB) for device:78:F7:BE:01:AA:84
HeadsetStateMachine: Connected process message: 12 (SEND_CCLC_RESPONSE) 
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage(CLCC): No matching device for 104. Returning null
HeadsetStateMachine: checkBlacklistWithId(78:F7:BE:01:AA:84) : false, id : 2
HeadsetStateMachine: processSendClccResponse: Failed to get device for CLCC response. Drop response
HeadsetStateMachine: Connected process message: 12 (SEND_CCLC_RESPONSE) 
HeadsetStateMachine: getDeviceForMessage
HeadsetStateMachine: getDeviceForMessage(CLCC): No matching device for 104. Returning null
HeadsetStateMachine: checkBlacklistWithId(78:F7:BE:01:AA:84) : false, id : 2
HeadsetStateMachine: processSendClccResponse: Failed to get device for CLCC response. Drop response
HeadsetStateMachine: Connected process message: 101 (STACK_EVENT) 
HeadsetStateMachine: event type: 17 (EVENT_TYPE_WBS) 
HeadsetStateMachine: EVENT_TYPE_WBS codec is 1

脚本地址:https://gist.github.com/legendmohe/7c49224da62565d05ca4

定义文件地址:https://gist.github.com/legendmohe/9682ff8d97ae27be8113

使用时,输入以下命令即可:

python readable_state.py [target_file]