Android code instance to view external device information for a USB connection _android

Source: Internet
Author: User

1,USB Storage equipment (such as: U disk, Mobile hard disk):

USB storage Device Plug monitoring is consistent with the SD card Plug and unplug monitor.

Copy Code code as follows:

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 removal, updating UI
else if (action.equals (intent.action_media_mounted)) {

USB device mount, updating UI
}
}
}

Get the storage space usage of the mounted USB device

public static String Getusbstorage (context context) {
USB Storage

Storage/udisk is the mount path for the USB device on the Android device. Different vendors have different Android device paths.

This writing is also suitable for the 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: Used/Available
}


2,USB external input devices (such as: keyboard, mouse, scan gun)
Copy Code code as follows:

try {

Obtaining information about an external USB input device
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 the device information for each line you get and get what you want.

}

catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}


Another: Device information can also be seen through the ADB shell into executive cat/proc/bus/input/devices.

USB external input device information is printed as follows:

Copy Code code 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 is used when the Android device interacts with the USB device data.

Code writing about how a USB add-in device interacts with an Android device data data can refer to:

Http://developer.android.com/guide/topics/connectivity/usb/index.html

N:name= "Acrux USB Keyboard" describes the name of an 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 on the Android device.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.