安卓内核OTG



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

是否有 otg 电缆的意图,如下所示的 USB 设备:"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();
    }
}

我从未尝试过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) *三星银河连结

支持的 USB 设备: * 胖 U 盘 *读卡器

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

你现在在自己的船上。

相关内容

  • 没有找到相关文章

最新更新