A code instance in Android that views information about the external devices to which USB is connected
- 2020-05-27 07:09:54
- OfStack
1,USB storage device (e.g., U disk, removable hard disk) :
//USB storage device plug and pull monitor and SD card plug and pull monitor 1.
private USBBroadCastReceiver mBroadcastReceiver;
IntentFilter iFilter = new IntentFilter();
iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
iFilter.addDataScheme("file");
mBroadcastReceiver = new USBBroadCastReceiver();
registerReceiver(mBroadcastReceiver, iFilter);
private class USBBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
//USB Device removed, updated UI
} else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
//USB Device mount, update UI
}
}
}
// Get mounted USB Storage space usage of the device
public static String getUSBStorage(Context context){
// USB Storage
//storage/udisk for USB Equipment in Android The mount path on the device . Different manufacturer Android Device paths are different.
// And that also applies SD Card mount.
File path = new File("/storage/udisk");
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
String usedSize = Formatter.formatFileSize(context, (totalBlocks-availableBlocks) * blockSize);
String availableSize = Formatter.formatFileSize(context, availableBlocks * blockSize);
return usedSize + " / " + availableSize;// space : Has been used / The available
}
2,USB external input device (e.g. : keyboard, mouse, scanning gun)
try {
// For external USB Enter the device information
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();
// Filter each line of device information to get what you want.
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Other: equipment information can also be through adb shell into execution cat proc/bus/input/devices see.
USB external input device information is printed as follows:
I: Bus=0003 Vendor=11c0 Product=0030 Version=0110
N: Name="ACRUX USB Keyboard"
P: Phys=usb-0000:00:04.0-1.3/input1
S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3
U: Uniq=
H: Handlers=mouse1 event3
B: PROP=0
B: EV=17
B: KEY=70000 0 0 0 0 0 0 0 0
B: REL=103
B: MSC=10
I: Bus=0003 Vendor=11c0 Product=0030 Version=0110 this line of information will be used in the data interaction between the Android device and the USB device.
For code writing on how USB external devices interact with Android device data, please refer to:
http://developer.android.com/guide/topics/connectivity/usb/index.html
The line N: Name="ACRUX USB Keyboard" indicates the name of the external USB device.
P: Phys=usb-0000:00:04.0-1.3/input1
S: Sysfs=/devices/pci0000:00/0000:00:04.0/usb1/1-1/1-1.3/1-1.3:1.1/input/input3
These two lines can be used to identify which USB port the USB device is connected to the Android device.