Android 檢測USB 音頻設(shè)備

1.廣播檢測USB 音頻設(shè)備

注冊廣播:

"android.hardware.usb.action.USB_DEVICE_ATTACHED";
"android.hardware.usb.action.USB_DEVICE_DETACHED";

接收廣播:

public class MyUsbDeviceReceiver extends BroadcastReceiver {
    private static final String TAG = MyUsbDeviceReceiver.class.getSimpleName();

    public static final String TAGLISTEN = "android.intent.action.HEADSET_PLUG";
    private final static String TAGUSB = "android.hardware.usb.action.USB_STATE";
    public static final String TAGIN = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
    public static final String TAGOUT = "android.hardware.usb.action.USB_DEVICE_DETACHED";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(TAGLISTEN)) {
            Log.i(TAG, "TAGLISTEN");
        } else if (action.equals(TAGUSB)) {
            Log.i(TAG, "TAGUSB");
        } else if (action.equals(TAGIN)) {
            Log.i(TAG, "TAGIN");
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (null == device) {
                Log.i(TAG, "null usb device");
                return;
            }
            int count = device.getConfigurationCount();
            boolean hasAudio = false;
            for (int i = 0; i < count; i++) {
                UsbConfiguration configuration = device.getConfiguration(i);
                if (null == configuration) {
                    Log.i(TAG, "null usb configuration");
                    return;
                }
                int interfaceCount = configuration.getInterfaceCount();
                for (int j = 0; j < interfaceCount; j++) {
                    UsbInterface usbInterface = configuration.getInterface(j);
                    if (null != usbInterface && usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
                        hasAudio = true;
                    }
                }
            }
            Log.i(TAG, "has audio:" + hasAudio);
            // your operation here
        } else if (action.equals(TAGOUT)) {
            Log.i(TAG, "TAGOUT");
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            if (null == device) {
                Log.i(TAG, "null usb device");
                return;
            }
            int count = device.getConfigurationCount();
            boolean hasAudio = false;
            for (int i = 0; i < count; i++) {
                UsbConfiguration configuration = device.getConfiguration(i);
                if (null == configuration) {
                    Log.i(TAG, "null usb configuration");
                    return;
                }
                int interfaceCount = configuration.getInterfaceCount();
                for (int j = 0; j < interfaceCount; j++) {
                    UsbInterface usbInterface = configuration.getInterface(j);
                    if (null != usbInterface && usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO) {
                        hasAudio = true;
                    }
                }
            }
            // your operation here
        }
    }
}

USB 設(shè)備類型獲取請查看UsbInterface.getInterfaceClass():

/**
 * Returns the interface's class field.
 * Some useful constants for USB classes can be found in {@link UsbConstants}
 *
 * @return the interface's class
 */
public int getInterfaceClass() {
    return mClass;
}

USB 設(shè)備類型定義請查看UsbConstants.java:


    /**
     * USB class indicating that the class is determined on a per-interface basis.
     */
    public static final int USB_CLASS_PER_INTERFACE = 0;
    /**
     * USB class for audio devices.
     */
    public static final int USB_CLASS_AUDIO = 1;
    /**
     * USB class for communication devices.
     */
    public static final int USB_CLASS_COMM = 2;
    /**
     * USB class for human interface devices (for example, mice and keyboards).
     */
    public static final int USB_CLASS_HID = 3;
    /**
     * USB class for physical devices.
     */
    public static final int USB_CLASS_PHYSICA = 5;
    /**
     * USB class for still image devices (digital cameras).
     */
    public static final int USB_CLASS_STILL_IMAGE = 6;
    /**
     * USB class for printers.
     */
    public static final int USB_CLASS_PRINTER = 7;
    /**
     * USB class for mass storage devices.
     */
    public static final int USB_CLASS_MASS_STORAGE = 8;
    /**
     * USB class for USB hubs.
     */
    public static final int USB_CLASS_HUB = 9;
    /**
     * USB class for CDC devices (communications device class).
     */
    public static final int USB_CLASS_CDC_DATA = 0x0a;
    /**
     * USB class for content smart card devices.
     */
    public static final int USB_CLASS_CSCID = 0x0b;
    /**
     * USB class for content security devices.
     */
    public static final int USB_CLASS_CONTENT_SEC = 0x0d;
    /**
     * USB class for video devices.
     */
    public static final int USB_CLASS_VIDEO = 0x0e;
    /**
     * USB class for wireless controller devices.
     */
    public static final int USB_CLASS_WIRELESS_CONTROLLER = 0xe0;
    /**
     * USB class for wireless miscellaneous devices.
     */
    public static final int USB_CLASS_MISC = 0xef;
    /**
     * Application specific USB class.
     */
    public static final int USB_CLASS_APP_SPEC = 0xfe;
    /**
     * Vendor specific USB class.
     */
    public static final int USB_CLASS_VENDOR_SPEC = 0xff;

2.UsbManager檢測

原理大同小異,都是獲取到UsbDevice,然后解析。

private boolean detectUsbAudioDevice() {
        HashMap<String, UsbDevice> deviceHashMap = ((UsbManager) getSystemService(USB_SERVICE)).getDeviceList();
        for (Map.Entry entry : deviceHashMap.entrySet()) {
            UsbDevice device = (UsbDevice) entry.getValue();
            if (null != device) {
                for (int i = 0; i < device.getConfigurationCount(); i++) {
                    UsbConfiguration configuration = device.getConfiguration(i);
                    if (null != configuration) {
                        for (int j = 0; j < configuration.getInterfaceCount(); j++) {
                            UsbInterface usbInterface = configuration.getInterface(j);
                            if (null != usbInterface) {
                                if (UsbConstants.USB_CLASS_AUDIO == usbInterface.getInterfaceClass()) {
                                    Log.i(TAG, "has usb audio device");
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        Log.i(TAG, "have no usb audio device");
        return false;
    }

3.Linux命令檢測

節(jié)點(diǎn):
/proc/bus/input/devices或者/proc/asound/cards滩褥,用cat命令獲取穷躁。
前一個是根據(jù)usb輸入設(shè)備來判斷,后一個是根據(jù)聲卡來獲取愈案。
獲取具體信息要根除命令輸出情況來自行解析,以前一個節(jié)點(diǎn)為例:

private void detectInputDeviceWithShell() {  
    try {  
        //獲得外接USB輸入設(shè)備的信息  
        Process p = Runtime.getRuntime().exec("cat /proc/bus/input/devices");  
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
        String line = null;  
        while ((line = in.readLine()) != null) {  
            String deviceInfo = line.trim();  
            //對獲取的每行的設(shè)備信息進(jìn)行過濾,獲得自己想要的橡羞。  
              if (deviceInfo.contains("Name="))  
                Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo);  
        }  
        Log.d(TAG, "-----------------------");  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市济舆,隨后出現(xiàn)的幾起案子卿泽,更是在濱河造成了極大的恐慌,老刑警劉巖滋觉,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件签夭,死亡現(xiàn)場離奇詭異,居然都是意外死亡椎侠,警方通過查閱死者的電腦和手機(jī)第租,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來我纪,“玉大人慎宾,你說我怎么就攤上這事∏诚ぃ” “怎么了趟据?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長术健。 經(jīng)常有香客問我汹碱,道長,這世上最難降的妖魔是什么荞估? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任咳促,我火速辦了婚禮稚新,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘跪腹。我一直安慰自己褂删,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布尺迂。 她就那樣靜靜地躺著笤妙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪噪裕。 梳的紋絲不亂的頭發(fā)上蹲盘,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機(jī)與錄音膳音,去河邊找鬼召衔。 笑死,一個胖子當(dāng)著我的面吹牛祭陷,可吹牛的內(nèi)容都是我干的苍凛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼兵志,長吁一口氣:“原來是場噩夢啊……” “哼醇蝴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起想罕,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤悠栓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后按价,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惭适,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年楼镐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了癞志。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡框产,死狀恐怖凄杯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情秉宿,我是刑警寧澤戒突,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站蘸鲸,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏窿锉。R本人自食惡果不足惜酌摇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一膝舅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窑多,春花似錦仍稀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至千康,卻和暖如春享幽,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拾弃。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工值桩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人豪椿。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓奔坟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親搭盾。 傳聞我的和親對象是個殘疾皇子咳秉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,303評論 25 707
  • Android 8.0 為用戶和開發(fā)者引入多種新功能。本文重點(diǎn)介紹面向開發(fā)者的新功能鸯隅。 請務(wù)必查閱Android ...
    android之子閱讀 3,437評論 0 74
  • 柔媚河畔澜建,依依情長。 春光難挽滋迈,又見秋涼霎奢。 卷簾心傷,盛衰有常饼灿。 不經(jīng)蕭索幕侠,怎換新妝?
    春來木棉開閱讀 307評論 2 4
  • 關(guān)于娛樂否認(rèn)的思考 無疑碍彭,對于這個詞語晤硕,每個人都有自己的理解。不管是奧托蘭克的死亡否認(rèn)庇忌,還是弗洛伊德生本能...
    詎客閱讀 113評論 0 1
  • ——記賽里木湖國際馬拉松舞箍! 6月24日,在結(jié)束烏馬一個月后的又一次挑戰(zhàn)皆疹,那天早晨疏橄,家里的老人起的格外的早,7...
    XiaoXiao_f96c閱讀 542評論 2 0