Programatically turn off USB storage

mbisso

Member
Licensed User
Longtime User
Hi All,
We have developped an interface board on a PIC microcontroller that uses the ADB interface to communicate with any android device. My problem is that with the USB connection, the mass usb storage is enabeled by default and we do not have access to the device memory, other than the DirInternal. Normally, this would be ok, but when I try to update the application from an FTP site, I get Package parsing error. This was traced back to the fact that I can't store application.apk and launch an update from the internal memory. Changing the storage to external fixed this. Which bring me back to the original problem, I need to turn (unmount) the USB storage to be able to save my updated application to external memory. I have found these two possibilities to turn off the storage:

MountService.setUsbMassStorageEnabled(false); or

StorageManager.disableUsbMassStorage();

But they are sytem accessible services and I would need to use the reflection library to use them. Can anyone help with the coding of this.

Thanks,
Mike
 

mbisso

Member
Licensed User
Longtime User
Something else I tried with no luck

I also tried to use this:

Dim iIntent As Intent

iIntent.Initialize(iIntent.ACTION_MAIN, "")
iIntent.Flags = 0x800000
iIntent.SetComponent("android/com.android.internal.app.UsbStorageStopActivity")
StartActivity(iIntent)

with no luck, I get a permission denial, component requires null.
 
Upvote 0

mbisso

Member
Licensed User
Longtime User
Hi, I don't think you can mount or unmount USB, unless your application is part of the firmware or maybe rooted

Hi VB,
The device is rooted, and I did use the code snipet I just posted as part of the OS by installing it in the system/app folder and re-installing the ROM (custom). I still got the permission denial. The manifest did include MODIFY_SETTINGS and MODIFY_SECURE_SETTINGS. Unless I am missing...
 
Upvote 0

mbisso

Member
Licensed User
Longtime User
You might want to try sending this command via the shell
to dismount it


echo "" > /sys/devices/platform/usb_mass_storage/lun0/file

No dice, this is the /sys/devices/platform files list from my arnova tablet
power
rockchip_usart.0
rockchip_usart.1
rk2818-fb.4
rk28-camera.33
rk28_sdmmc0
rk28_sdmmc1
rk28_AD_button
rockchip_spi_master
rk28xxnand
rk28_i2c
rockchip-i2s.0
rockchip_battery
android_usb
dwc_otg
rk28_backlight
rk28-dsp.0
android_pmem.0
android_pmem.1
alarm
soc-audio

as you can see, there is no usb_mass_storage subdirectory. also, I have searched the entire file system for lun0/file and did not find anything
 
Upvote 0

mbisso

Member
Licensed User
Longtime User
It's android 2.1. And I believe it's been modified by archos for the arnova 8 tablet I am running it on. Since It does not seem to be possible to turn of the USB storage programatically (unless somebody can prove otherwise) I will send a command through the USB port that will cause the PIC to disconnect the connection prior to updating the app.

But I hate the fact that I can't change this setting from within an app.
 
Upvote 0

pluton

Active Member
Licensed User
Longtime User
Hi mbisso

I searched for that answer also but as it says on Stackoverflow - how-to-turn-off-usb-mass-storage-programmatically-in-android it is impossibile.

Here it is copy-paste:
First, you can't modify Settings.Secure.MASS_STORAGE_ENABLED even you have claimed WRITE_SETTINGS and WRITE_SECURE_SETTINGS in AndroidManifest.xml, because Settings.Secure.* only can be modified by apps in system/app, of course, read operation has not limits.

Second, I have searched the whole source code(both Gingerbread and ICS) and found that Settings.Secure.MASS_STORAGE_ENABLED has not been used currently, I am confused since Settings.Secure.ADB_ENABLED is used. please Google tell me why? will be implemented in next release?

Third, You can force to turn off usb mass storage connection by calling:

B4X:
MountService.setUsbMassStorageEnabled(false);

or

B4X:
StorageManager.disableUsbMassStorage();

but unfortunately, both these API are not public accessible?!
 
Upvote 0

mbisso

Member
Licensed User
Longtime User
Solution for this particular tablet

Ok, this works. The Arnova 8, with android 2.1, has two usb connectors. One for the regular ADB, PC connection, and a second one that can be used as a host. By adding this line of code:

File.WriteString("/sys/bus/platform/drivers/dwc_otg","force_usb_mode","1")

The USB control is transfered to the second port, effectively disconecting the primary USB port and allowing access to the DirRootExternal drive. I can then update my application. On startup, in the create sub, I add this line

File.WriteString("/sys/bus/platform/drivers/dwc_otg","force_usb_mode","0")

which reconnects the primary usb port and my ADB connection is re-established. :sign0060:
 
Upvote 0
Top