Android 检测USB 音频设备

1.广播检测USB 音频设备

注册广播:

"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 设备类型获取请查看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 设备类型定义请查看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 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命令检测

节点:
/proc/bus/input/devices或者/proc/asound/cards,用cat命令获取。
前一个是根据usb输入设备来判断,后一个是根据声卡来获取。
获取具体信息要根除命令输出情况来自行解析,以前一个节点为例:

private void detectInputDeviceWithShell() {  
    try {  
        //获得外接USB输入设备的信息  
        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();  
            //对获取的每行的设备信息进行过滤,获得自己想要的。  
              if (deviceInfo.contains("Name="))  
                Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo);  
        }  
        Log.d(TAG, "-----------------------");  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  
}  

你可能感兴趣的:(Android 检测USB 音频设备)