I have a widget that will tell the user the capacity of a connected USB device, as well as the amount of space used. I have gone through the android.hardware.usb API and I see nothing that can accomplish determining the size of a connected USB device and its usage.
Right now I am capturing Intent.ACTION_MEDIA_MOUNTED and searching in the Dir of the mount point for specific files etc... All of that is working fine, but for the life of me I cannot see how to leverage the API to determine the overall size, and usage, of a connected USB device.
Do any of you fine folks know, or have an article for me to read? Thanks all.
It sounds like your device is actually mounting the connected USB device's file system, which seems like it may be a vendor add-on rather than a stock android capability.
If that is indeed the case, whatever other options you might have you can use the df shell command, or more properly duplicate its functionality in java or a native subroutine.
You can view the source of android's df here:
https://github.com/android/platform_system_core/blob/master/toolbox/df.c
It looks to be a fairly standard implementation - read /proc/mounts to find the filesystems, call statfs() on each one. You'd probably want to filter by those of interest.
Related
The question is: how to determine (programmatically) which USB port was used when the USB flash drive was plugged in?
Context:
I am developing an app for an Android TV Box that lets you view files from a Pendrive. My device has 3 USB ports. I would like to know which of the port was used (some kind of ID, port name, whatever that could help me identify it in the code).
What have I done:
I read the android docs regarding USB as a host. None of the classes contains such information. The API is listing only devices which are plugged in but there is no information about the USB sockets. The deviceName is a string containing the path to a device file created in the android system but it is changing every time I plug in a USB flash drive (it looks like this /dev/bus/usb/007/008 the numbers, in the end, are different each time).
Looked into android sys files by adb shell command but couldn't find anything that describes the USB ports and says which one is currently in use.
Maybe someone encountered a similar problem? I'd really appreciate any help. Thanks in advance!
The Android operating system does not expose the information you are looking for.
Only with root access to the underlying Linux system below, can that information be obtained.
Use lsusb to enumerate the usb devices, and dmesg and mount to correlate the drive's mount point.
For some reasons not related to the battery life, I need to disable the battery charging when a USB host is connected to my phone (a rooted Nexus 4).
Taking a look around on the internet (link1, link2), it looks like I should modify a system file.
I have found a series of interesting files located in /sys/class/power_supply/battery and /sys/class/power_supply/usb, that could be the right ones to be modified in order to achieve the usb charging disabling (e.g. /sys/class/power_supply/usb/present 0/1).
Has anyone tried something like that?
Which is the best way to modify those system files? Should I use the adb shell or another tool?
I tried the "root explore" app in order to modify those files, however when I modified a file (after changing its permissions), it was automatically kept back to its original value.
I've got a DSLR camera and Samsung Galaxy Tab running Android Honeycomb. DSLR connected to a tablet using USB-cable (via USB kit enabling host functionality on a tablet). I'd like to being notified when user takes a photo using this external camera, in order to download this image to the tablet or do something else with it like showing Toast notification containing meta-information taken from the image.
As far as I get all of the existing tools (like FileObserver using underlying inotify mechanism, MediaContentProvider etc) allowing to watch for changes, demand a specific file or a filesystem path to be watched. This was good enough till we had a block layer protocol support in 2.x and earlier Android versions - when you connected device it'd been mounted somewhere on the device's filesystem and you was able to use this mountpoint as a watch point for those tools.
Since Honeycomb Google has changed the way of accessing external USB devices to Media Transfer Protocol with PTP as a subset of this. Now when I connect external USB device to an Android device I won't see any mountpoints for it (I'm using adb shell and subsequent mount command for getting them). Moreover, MTP implementation uses storage ids which apparently act as a higher level of abstraction and are just plain integer values. I was hoping there is a way to somehow translate these storage ids to the real paths/mountpoint/whatever but apparently there does not appear to be.
Thinking about Android MediaScanner which is already running on my device I guessed it could manage this issue with a special Intent broadcasted when there're changes in media files accessible from the device, so I started looking for already existing and suitable Intents for being notified, but no luck - I found only ACTION_MEDIA_MOUNTED and ACTION_MEDIA_REMOVED which are broadcasted only when device is connected and disconnected respectively. That means MediaScanner can't notice any changes on the device until you remount it (I've double checked it using stock Gallery app - it doesn't see any newly created images on the camera until you unplug and then plug it into the Android device again).
Trying to get this mount path for external sdcard, I used Environment.getExternalStorageDirectory() API call but it yields emulated Galaxy's sdcard path
which is /mnt/sdcard, not the camera's one. So it doesn't work for me either.
I managed to work out this issue only having launched periodic Timer event with AsyncTask acting as a TimerTask. This task does initialize usb connection, open device,
scan the whole device memory, getting only the last taken photo and then close device descriptor and usb connection.
It doesn't look like the best and efficient way of doing that taking into account it has to do all of these actions every time which could be pretty often, say each 5 or 10 seconds. It definitely quickly drains battery out and produces unnecessary system I/O for only taking last taken photo and comparing it with the previous last taken photo (in 99% it'd the same image), but I haven't found any better working solution for doing this. It'd much better off to have an observer mechanism with event-based notifications.
So my question is there more efficient way of being notified about changes in external USB storage for Honeycomb or later Android versions rather than one described above?
If you would like a more efficient way the camera would have to send out some sort of signal over usb that it has taken a photo. I guess it is not doing that.
Therefore you will have to check manually by doing the way your are discribing:
mount storage --> check for changes --> do your thing with your detected changes.
I dont know what you used to read "the MTP way" but here an example application:
https://github.com/ynakanishi/Honeycomb-MTP-sample
To not scan the entire storage every time you could save the result of read out file names for example every time you check and compare it to find the new ones. Usually the naming of the file also starts with the same number on a camera. So if you start a session with an empty sd card you know already the file name the photo will have. lets say img0001.jpg. So you just need to write a function to grab that file until it succeeds. if you want the next one img0002.jpg you can write a task/service/function to grab that file until successful, and so on.
If you want to save on battery you could implement an additional battery/power source inbetween for powering the usb port.
Instead of an Async task or timerTask you could try a ScheduledExecutorService and see if it uses less power.
Hope that gave you some new thoughts
Yesterday I decided to fix the internal front speakers of my "HTC Desire Z" (aka. G2). While I was able to fix them, I broke the connector for the microSD slot (the left one). After trying to fix it for several hours, I gave up. It isn't in the scope of my hardwareskills :-(
Well, since I have like 600mb left on /data and my device is rooted, I tried to find a software solution i.e. "emulate" a SDcard device (because many applications require one to work properly, e.g. the camera app). I found here http://www.android-x86.org/documents/sdcardhowto a description how to create a vfat image and mount it as loopback.
While this is exactly what I want, I don't get it to work:
I don't know how to modifiy /proc/cmdline, or
how the entry in /etc/vold.fstab should look like.
dev_mount sdcard /mnt/sdcard auto /data/sdcard.img
didn't work for me. according to logcat, I have this version: I/Vold ( 2368): Vold 2.1 (the revenge) firing up
btw, just mounting the loopback device to /mnt/sdcard didn't work too. For example, the camera app still claims there isn't a SD card inserted.
edit: I also asked this on xda -> http://forum.xda-developers.com/showthread.php?t=1393395
There's no way for an Android application (constrained within the application sandbox) to create a loopback device.
Anything outside of this is not a developer question, and should be posted to another forum (e.g. https://superuser.com/).
I don't agree. Hanitaro Tide and PlasmaSoundHD both have created loop devices on my phone, all by themselves. On an x86 device it's even easier to fake an sdcard.img.p
Is it possible to access the USB port on Android phones? (Droid X for example)
Here is my usage case:
Have a USB device attached to the Android phone. The phone listens for data on this USB device. (The USB device is connected to a USB->RS-232 converter that has I/O attached to it)
This would be slick if possible. Does anyone know if this is possible?
Would be slick, yup. Not possible though. There's a feature request for it: http://code.google.com/p/android/issues/detail?id=738
Actually it is possible on a lot of the phones if you are able to install a new kernel with a USB host driver and rig up a custom cabling scheme to provide usb bus power to the device as the phone won't. A few phones even shipped with this capability already live.
I don't know if the Droid X specifically ships with this already, can have it added (if you are able to flash kernels), could have it added but no one has written the host driver yet, or is missing the hardware capability.
You also would need to enable the appropriate usb serial converter device driver (identified by experimenting with the device on a desktop linux box), but that's probably already in the kernel sources and just needs to be selected in the config. You may also need to create a device file for the /dev/ttyUSB0 or whatever and give it permissions appropriate to the application that wants to access it. (This requires root, but if you can reflash the kernel, you can get root)
If you want to pursue this, search the android kernel google group for posts about USB host mode.
One serious downside to putting the USB into host mode is that you loose the adb interface into the phone, which makes working on your projects hard. You'll probably need to either build an adapter for the low voltage debug serial port if there is one (as on G1, mytouch, etc) so you can get a console shell (or just use that instead of USB to talk to your peripheral) or at a minimum set up an ssh and sftp server on the wifi.
Since you want serial anyway, another option people have used is to get a bluetooth-to-serial module from an outfit such as sparkfun.
A bluetooth to serial adapter might solv your problems.
You can find one at https://www.sparkfun.com/products/582. Boards from other companies are also available for example on ebay.