【问题标题】:Android Kernel OTGAndroid 内核 OTG
【发布时间】:2020-06-19 14:03:45
【问题描述】:

如何使用应用程序检测 otg 电缆何时插入以及何时未插入?

是否有类似以下用于 USB 设备的 otg 电缆的意图:“android.hardware.usb.action.USB_DEVICE_ATTACHED”

我创建了一个应用,但它只检测到闪存驱动器而不是 otg 电缆:

public class MainActivity extends AppCompatActivity
{

    private TextView mInfo;
    private Logger mLogger;
    private HashMap<UsbDevice, UsbDataBinder> mHashMap = new HashMap<UsbDevice, UsbDataBinder>();
    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mInfo = (TextView)findViewById(R.id.log);

        mLogger = new Logger(this);
        mLogger.setMode(Logger.MODE_TOAST);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        usbConnection();
    }

    private void usbConnection() {
        IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        registerReceiver(mUsbAttachReceiver , filter);
        filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbDetachReceiver , filter);

        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);

        showDevices();
    }

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbDetachReceiver);
        unregisterReceiver(mUsbAttachReceiver);
        unregisterReceiver(mUsbReceiver);
    };

    BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    // call your method that cleans up and closes communication with the device
                    UsbDataBinder binder = mHashMap.get(device);
                    if (binder != null) {
                        binder.onDestroy();
                        mHashMap.remove(device);
                        Toast.makeText(MainActivity.this, "Attached!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };

    BroadcastReceiver mUsbAttachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                showDevices();
                Toast.makeText(MainActivity.this, "Detached!", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent
                            .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(
                            UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            // call method to set up device communication
                            UsbDataBinder binder = new UsbDataBinder(mUsbManager, device);
                            mHashMap.put(device, binder);
                            Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        // Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };

    private void showDevices() {
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            UsbDevice device = deviceIterator.next();
            mUsbManager.requestPermission(device, mPermissionIntent);
            //your code
            mLogger.log("usb", "name: " + device.getDeviceName() + ", " +
                    "ID: " + device.getDeviceId());
            mInfo.append(device.getDeviceName() + "\n");
            mInfo.append(device.getDeviceId() + "\n");
            mInfo.append(device.getDeviceProtocol() + "\n");
            mInfo.append(device.getProductId() + "\n");
            mInfo.append(device.getVendorId() + "\n");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }
}

【问题讨论】:

    标签: android usb-otg


    【解决方案1】:

    我从未尝试过 USB OTG,但您可以从 https://github.com/shakalaca/USB-OTG-Manager 使用

    <intent-filter>
        <action android:name="com.sonyericsson.hardware.action.USB_OTG_DEVICE_CONNECTED" />
        <action android:name="com.sonyericsson.hardware.action.USB_OTG_DEVICE_DISCONNECTED" />
        <action android:name="com.sonyericsson.hardware.action.USB_OTG_ERROR" />
        <action android:name="com.sonyericsson.usbotg.ACTION_ERROR_OK" />
    
        <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
    
    </intent-filter>
    

    来自文档:

    USB OTG 管理器

    该软件最初是为索尼爱立信 Xperia Arc S 设计的。 由于内核中启用了 OTG,我想知道为什么我无法访问我的 记忆棒。所以我创建了这个小应用程序来阅读 我拇指盘上的书,从此幸福快乐! :D

    支持的电话: * 索尼爱立信 Xperia Arc S(4.0.2.A.042、4.0.2.A.062) * 索尼爱立信 Xperia Mini Pro(4.0.2.A.042、4.0.2.A.058) * 三星 Galaxy Nexus

    支持的 USB 设备: * FAT USB 记忆棒 * 读卡器

    相关项目:https://github.com/mik3y/usb-serial-for-android

    你现在在自己的船上。

    【讨论】:

    • 如何在我的 s3 neo i9301i 上使用它?
    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 2013-03-12
    • 1970-01-01
    • 2012-03-14
    • 2012-03-10
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多