关闭

Android 通过广播监听USB连接状态的改变

标签: androidusb
3254人阅读 评论(0) 收藏 举报
分类:

一、原理讲解
      Android通过广播监听USB连接状态的改变的动作在UsbManager.java文件里,为ACTION_USB_STATE。然而在UsbManager中,该常量的注释中包含{@hide},该注释是控制该API是否开放用的。未开发的API意味着google可以随时调整该API的定义或实现方式,而不用保证向下兼容。
      所以尽量避免调用未开发的API,因为一旦系统升级导致其实现方式改变,程序很有可能就直接崩溃了。
      注意:插拔手机与电脑之间的USB线,并不会出发OTG广播(即ACTION_USB_ACCESSORY_XXX 和 ACTION_USB_DEVICE_XXX,XXX代表了ATTACH 或 DETACH)

二、代码如下
1、MyUsbManager.java

package com.example.testapp;

public class MyUsbManager {

    /**
     * Broadcast Action: A sticky broadcast for USB state change events when in device mode.
     * This is a sticky broadcast for clients that includes USB connected/disconnected state,
     * <ul>
     * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
     * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. currently zero if not configured, one for configured.
     * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the adb function is enabled
     * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the RNDIS ethernet function is enabled
     * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the MTP function is enabled
     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the PTP function is enabled
     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the accessory function is enabled
     * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the audio source function is enabled
     * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the MIDI function is enabled
     * </ul>
     */

    public static final String ACTION_USB_STATE = "android.hardware.usb.action.USB_STATE";

    /**
     * Boolean extra indicating whether USB is connected or disconnected.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_CONNECTED = "connected";

    /**
     * Boolean extra indicating whether USB is configured.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_CONFIGURED = "configured";

    /**
     * Boolean extra indicating whether confidential user data, such as photos, should be
     * made available on the USB connection. This variable will only be set when the user
     * has explicitly asked for this data to be unlocked.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
     */
    public static final String USB_DATA_UNLOCKED = "unlocked";

    /**
     * A placeholder indicating that no USB function is being specified.
     * Used to distinguish between selecting no function vs. the default function in {@link #setCurrentFunction(String)}.
     */
    public static final String USB_FUNCTION_NONE = "none";

    /**
     * Name of the adb USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_ADB = "adb";

    /**
     * Name of the RNDIS ethernet USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_RNDIS = "rndis";

    /**
     * Name of the MTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_MTP = "mtp";

    /**
     * Name of the PTP USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_PTP = "ptp";

    /**
     * Name of the audio source USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_AUDIO_SOURCE = "audio_source";

    /**
     * Name of the MIDI USB function.
     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
     */
    public static final String USB_FUNCTION_MIDI = "midi";
}

2、UsbActivity.java

package com.example.testapp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class UsbActivity extends Activity {

    private TextView textView;
    private UsbManager usbManager;
    private BroadcastReceiver receiver;
    private IntentFilter intentFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView scrollView = new ScrollView(this);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        textView = new TextView(this);
        scrollView.addView(linearLayout);
        linearLayout.addView(textView);

        setContentView(scrollView);

        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                addText("action : " + action);

                if(intent.hasExtra(UsbManager.EXTRA_PERMISSION_GRANTED)) {
                    boolean permissionGranted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
                    addText("permissionGranted : " + permissionGranted);
                }
                switch(action) {
                case UsbManager.ACTION_USB_ACCESSORY_ATTACHED:
                case UsbManager.ACTION_USB_ACCESSORY_DETACHED:
                    //Name of extra for ACTION_USB_ACCESSORY_ATTACHED and ACTION_USB_ACCESSORY_DETACHED broadcasts containing the UsbAccessory object for the accessory.
                    UsbAccessory accessory = intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
                    addText(accessory.toString());
                    break;
                case UsbManager.ACTION_USB_DEVICE_ATTACHED:
                case UsbManager.ACTION_USB_DEVICE_DETACHED:
                    //Name of extra for ACTION_USB_DEVICE_ATTACHED and ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device.
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    addText(device.toString());
                    break;
                case MyUsbManager.ACTION_USB_STATE:
                    /*
                     * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
                     * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured.
                     * currently zero if not configured, one for configured.
                     * <li> {@link #USB_FUNCTION_ADB} boolean extra indicating whether the
                     * adb function is enabled
                     * <li> {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the
                     * RNDIS ethernet function is enabled
                     * <li> {@link #USB_FUNCTION_MTP} boolean extra indicating whether the
                     * MTP function is enabled
                     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
                     * PTP function is enabled
                     * <li> {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
                     * accessory function is enabled
                     * <li> {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
                     * audio source function is enabled
                     * <li> {@link #USB_FUNCTION_MIDI} boolean extra indicating whether the
                     * MIDI function is enabled
                     * </ul>
                     */
                    boolean connected = intent.getBooleanExtra(MyUsbManager.USB_CONNECTED, false);
                    addText("connected : " + connected);
                    boolean configured = intent.getBooleanExtra(MyUsbManager.USB_CONFIGURED, false);
                    addText("configured : " + configured);
                    boolean function_adb = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_ADB, false);
                    addText("function_adb : " + function_adb);
                    boolean function_rndis = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_RNDIS, false);
                    addText("function_rndis : " + function_rndis);
                    boolean function_mtp = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_MTP, false);
                    addText("function_mtp : " + function_mtp);
                    boolean function_ptp = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_PTP, false);
                    addText("usb_function_ptp : " + function_ptp);
                    boolean function_audio_source = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_AUDIO_SOURCE, false);
                    addText("function_audio_source : " + function_audio_source);
                    boolean function_midi = intent.getBooleanExtra(MyUsbManager.USB_FUNCTION_MIDI, false);
                    addText("function_midi : " + function_midi);
                    break;
                }
            }
        };
        intentFilter = new IntentFilter();
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        intentFilter.addAction(MyUsbManager.ACTION_USB_STATE);
        registerReceiver(receiver, intentFilter);

        boolean hasUsbHost = getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_HOST);
        boolean hasUsbAccessory = getPackageManager().hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
        addText("hasUsbHost : " + hasUsbHost);
        addText("hasUsbAccessory : " + hasUsbAccessory);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    private void addText(String str) {
        textView.setText(textView.getText().toString() + str + "\n");
    }
}

三、运行截图
1、手机通过usb线连电脑
这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:457317次
    • 积分:8219
    • 等级:
    • 排名:第2412名
    • 原创:325篇
    • 转载:173篇
    • 译文:25篇
    • 评论:116条