android耳機監(jiān)測
最近在做Marvell的項目,碰到個耳機熱插拔的問題,所以順便看了下相關(guān)代碼,這里來貢獻(xiàn)一下給大家。
本文引用地址:http://m.butianyuan.cn/article/201610/305871.htm我們這里以PXA的代碼為例,整個框架比較的簡單清晰,上層是分別是HeadsetObserver.java以及HookSwitchObserver.java,
它們會直接讀取/sys/class/switch/中某一特定的設(shè)備的state,比如/sys/class/switch/h2w/state,來獲取最新的狀態(tài)。
我們把它分為兩部分來看
kernel/drivers/switch/
這里存放著headset以及hook的驅(qū)動代碼,首先switch_class.c中會先注冊switch這個子類,并提供switch_dev_register這個注冊入口,
對應(yīng)的設(shè)備驅(qū)動調(diào)用switch_dev_register把自己注冊在switch這個子類中,比如:
switch_data_headset->sdev.name = pdata_headset->name; //name is h2w
switch_data_headset->name_on = pdata_headset->name_on;
switch_data_headset->name_off = pdata_headset->name_off;
switch_data_headset->state_on = pdata_headset->state_on;
switch_data_headset->state_off = pdata_headset->state_off;
switch_data_headset->sdev.print_state = switch_headset_print_state;
info->psw_data_headset = switch_data_headset;
ret = switch_dev_register(switch_data_headset->sdev);
那么在/sys/class/switch目錄中就會生成h2w這個子目錄,里面會有state,name等一些成員,而驅(qū)動判斷耳機是否插入則是通過
gpio模擬中斷,在耳機插入的時候產(chǎn)生電平的變化來調(diào)用中斷回調(diào)函數(shù),回調(diào)函數(shù)中會改變state狀態(tài)值,就這么簡單
frameworks/base/services/java/com/android/server/HeadsetObserver.java這是android耳機監(jiān)測的代碼
它會通過讀取/sys/class/switch/h2w/state來判斷此時是否有耳機插入,比如:
private static final String HEADSET_STATE_PATH = /sys/class/switch/h2w/state;
FileReader file = new FileReader(HEADSET_STATE_PATH);
然后通過
public void onUEvent(UEventObserver.UEvent event) {
if (LOG) Slog.v(TAG, Headset UEVENT: + event.toString());
try {
update(event.get(SWITCH_NAME), Integer.parseInt(event.get(SWITCH_STATE)));
} catch (NumberFormatException e) {
Slog.e(TAG, Could not parse switch state from event + event);
}
}
來更新耳機的檢測狀態(tài)。hook監(jiān)測和耳機監(jiān)測類似,這里就不再重復(fù)了,寫得有點亂,希望對朋友們有幫助。
評論