Mr Nice
Mr Nice

Reputation: 524

how to programmatically enable and disable USB in android Application

in my application i want to enable / disable the USB connection in the phone or tab(Android based) programmatically.I googled , but did not find any solution.If it is possible give me some ideas to do this.or provide some useful code for the same.

Upvotes: 4

Views: 6202

Answers (4)

Rehan P Zavi
Rehan P Zavi

Reputation: 1

[https://developer.android.com/reference/android/app/admin/DevicePolicyManager#setUsbDataSignalingEnabled(boolean)]

This api can be used to disable usb functions other than charging for Android 11+ but requires additional permission and need device or profile owner privileges.

Upvotes: 0

This is possible, but your application should own the device, by owning the device you can use the DevicePolicyManager.addUserRestriction method, as follows:

DevicePolicyManager devicePolicyManager =
            (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);

if (devicePolicyManager != null && isDeviceOwnerApp(context)) {
   devicePolicyManager.addUserRestriction(componentName, UserManager.DISALLOW_USB_FILE_TRANSFER);
}

Upvotes: 2

Devraj
Devraj

Reputation: 1531

I know it is too late but it would be helpful to others.. In API 12 Android 3.1 or higher , there is a UsbManager class has been introduced,

Check this link. http://developer.android.com/reference/android/hardware/usb/UsbManager.html

Upvotes: 6

CommonsWare
CommonsWare

Reputation: 1007604

This is not possible from the Android SDK.

Upvotes: 1

Related Questions