android usb monitor,How to Monitor USB Events on Android

How to Monitor USB Events on Android

Comparing to iOS, one of my favorite Android features is that Android supports USB peripherals. It’s fairly convenient to connect USB devices to Android smartphones via OTG cables. Therefore, no matter whether your Android phones, which probably only have 16G built-in storage, have SD card slot or not, it’s still capable of extending storage spaces with the power of accessing USB flash drive on Android operating system. In this post, I’d like to share what I’ve learned from Android developer website about how to monitor the events of attaching and detaching USB devices.

aee82c3ebc5f4f7f7d14cbabc5cba3a3.png

Android USB Host

Create an Android project, and add the following line to AndroidManifest.xml:

Use broadcast receivers to register the events of attaching and detaching USB devices:

BroadcastReceiver mUsbAttachReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {

showDevices();

}

}

};

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);

}

}

}

}

};

Bind corresponding intent filters with broadcast receivers when application is created:

IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);

registerReceiver(mUsbAttachReceiver , filter);

filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);

registerReceiver(mUsbDetachReceiver , filter);

Remember to unbind broadcast receivers when application is destroyed:

unregisterReceiver(mUsbDetachReceiver);

unregisterReceiver(mUsbAttachReceiver);

Once USB devices connected to Android system, all available information could be displayed by enumerating:

HashMap deviceList = mUsbManager.getDeviceList();

Iterator deviceIterator = deviceList.values().iterator();

while(deviceIterator.hasNext()){

UsbDevice device = deviceIterator.next();

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");

}

If you want to communicate with USB devices, you need to obtain the permission via broadcast receiver:

private static final String ACTION_USB_PERMISSION = "com.dynamsoft.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);

}

} else {

// Log.d(TAG, "permission denied for device " + device);

}

}

}

}

};

The broadcast receiver will be triggered after calling requestPermission ():

mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

mUsbManager.requestPermission(device, mPermissionIntent);

Finally, the connection will be built successfully:

mUsbManager = manager;

mDevice = device;

mIntf = mDevice.getInterface(0);

mEndpoint = mIntf.getEndpoint(0);

mConnection = mUsbManager.openDevice(mDevice);

So far, we have made a basic Android USB application. What’s next step? If you are a document imaging developer, perhaps you would like to develop a more sophisticated Android app to communicate with a variety of USB devices, such as scanner, Webcam and etc.

References

Source Code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值