This question already has answers here:
Enable USB debugging (under settings/applications/development) programmatically from within an app
(6 answers)
Closed 5 years ago.
Is there any way to turn USB debugging on/off programmatically on Android devices?
Hi this is my first post on here, and normally I wouldn't bother but I see no one wanted to give you the answer despite there being multiple ways to do so.
This is all from my app, I'm "idone" on xda-dev btw. Also some of this code maybe Samsung MSMxxxx specific
If you have root you can indeed. And here are 3 ways to do so despite other people saying otherwise
Method 1(broadcast secret code)
Method 2(set sys.usb.config)
Method 3(set settings global adb_enabled 1)
public String[] SET_DM_PORT_STATUS_LIST = new String[9];{
SET_DM_PORT_STATUS_LIST[0] = "setMTP";
SET_DM_PORT_STATUS_LIST[1] = "setMTPADB";
SET_DM_PORT_STATUS_LIST[2] = "setPTP";
SET_DM_PORT_STATUS_LIST[3] = "setPTPADB";
SET_DM_PORT_STATUS_LIST[4] = "setRNDISDMMODEM";
SET_DM_PORT_STATUS_LIST[5] = "setRMNETDMMODEM";
SET_DM_PORT_STATUS_LIST[6] = "setDMMODEMADB";
SET_DM_PORT_STATUS_LIST[7] = "setMASSSTORAGE";
SET_DM_PORT_STATUS_LIST[8] = "setMASSSTORAGEADB";}
public String[] SET_DM_PORT_CONFIG_LIST = new String[9];{
SET_DM_PORT_CONFIG_LIST[0] = "mtp";
SET_DM_PORT_CONFIG_LIST[1] = "mtp,adb";
SET_DM_PORT_CONFIG_LIST[2] = "ptp";
SET_DM_PORT_CONFIG_LIST[3] = "ptp,adb";
SET_DM_PORT_CONFIG_LIST[4] = "rndis,acm,diag";
SET_DM_PORT_CONFIG_LIST[5] = "rmnet,acm,diag";
SET_DM_PORT_CONFIG_LIST[6] = "diag,acm,adb";
SET_DM_PORT_CONFIG_LIST[7] = "mass_storage";
SET_DM_PORT_CONFIG_LIST[8] = "mass_storage,adb";}
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("am broadcast -a android.provider.Telephony.SECRET_CODE -d android_secret_code://" + SET_DM_PORT_STATUS_LIST[paramInt]+"\n");
outputStream.writeBytes("setprop sys.usb.config " + SET_DM_PORT_CONFIG_LIST[paramInt]+"\n");
if(SET_DM_PORT_STATUS_LIST[paramInt].contains("adb")){
outputStream.writeBytes("settings put global adb_enabled 1\n");
}
I am in the process of reversing IOTHIDDENMENU.apk and recreating it's methods but without the internal and hidden api it uses.
On a regular device, with a regular app, you can't.
You need a rooted device, with an app in /system/app, then you can.
Anyway, you shouldn't configure such a thing by yourself, the user should be in full control in such a case.
This is not possible in android because for that you have to access Setting.System. For more info take a look here : How can I disable Android USB debugging programmatically
It's not possible without using your own custom firmware that grants access to the security settings. See thread here: http://groups.google.com/group/android-developers/browse_frm/thread/953c6f0eb0fa9bed#
usb debugging is another name for the Android Debug Bridge (ADB). The item you're looking for is here
http://developer.android.com/reference/android/provider/Settings.Secure.html#ADB_ENABLED
great answer :
https://stackoverflow.com/a/17029123/1136074
basicly its:
android.os.Debug.waitForDebugger(); also you can use the following to determine if the debugger is connected:
android.os.Debug.isDebuggerConnected(); //Determine if a debugger is currently att
Related
Question about changing the parameters of the transition to doze mode
I have a non-rooted Android 12 device
There are a number of parameters for changing the transition time in doze mode:
inactive_to
motion_inactive_to
light_after_inactive_to
If you change these parameters through the PC using ADB, then the parameters are set. For instance:
adb shell device_config put device_idle inactive_to 2592000000
The problem is that after a reboot, the settings are reset.
Tried to change them directly in the program
// val command = arrayOf("/system/bin/device_config", "put", "device_idle", "motion_inactive_to", "2592000000")
val command = arrayOf("/system/bin/settings", "put", "global", "device_idle_constants", "light_after_inactive_to", "2592000000")
Log.d("ADB", "Start ${command.joinToString(separator = " ")}")
val process = Runtime.getRuntime().exec(command)
val bufReader = BufferedReader(InputStreamReader(process.errorStream))
val log = StringBuilder()
var line: String?
line = bufReader.readLine()
while (line != null) {
log.append(line + "\n")
line = bufReader.readLine()
}
Log.d("ADB", "Command: $log")
But the first command is answered:
“cmd: Can't find service: "device_config"”
And the second command gives an error:
Permission Denial: getCurrentUser() from pid=28435, uid=10245 requires android.permission.INTERACT_ACROSS_USERS
After searching for information about the INTERACT_ACROSS_USERS permission, I understand that it is necessary for it to make the application system. And for this I need to root the device.
Is there any other way to change the parameters of doze mode or disable it altogether?
It's confusing that you can run a command with adb on a non-rooted device, but you can't directly in the program.
Is there any other way to change the parameters of doze mode or disable it altogether?
No. If there were, it would have been pointless to add Doze mode, as every developer would opt out of it.
It's confusing that you can run a command with adb on a non-rooted device, but you can't directly in the program.
Different users/processes having different privileges has been a common concept in operating systems for at least 30 years.
I've been searching on this for quite some time, and have found lots of solutions that ultimately use the Android GUI to prompt the end-user for connection/confirmation. We would like to present a user with a form and directly connect to the specified network. I understand from a security perspective why this is necessary, but in our case, it is a system app going on a fully managed Android device. It is not intended for general use in the Play store. We are using Xamarin, but I can adapt any Java/Kotlin code.
The closest I've found thus far is the following (using C#):
public void ConnectToWifi(string ssid, string password = "") {
var wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder().SetSsid(ssid);
if (!string.IsNullOrEmpty(password))
wifiNetworkSpecifier.SetWpa2Passphrase(password);
var networkRequest = new NetworkRequest.Builder().AddTransportType(TransportType.Wifi)?
.SetNetworkSpecifier(wifiNetworkSpecifier.Build())?.Build();
if (networkRequest is null)
return;
var theNetworkCallback = new TheNetworkCallback();
var connectivityManager = (ConnectivityManager?)MainActivity.Current.ApplicationContext?
.GetSystemService(Context.ConnectivityService);
connectivityManager?.RequestNetwork(networkRequest, theNetworkCallback);
}
It sorta works, but does prompt the end-user, and my understanding, this approach is deprecated and doesn't work well in newer versions of Android. We're hoping for a solution that works in Android 11.
I'm even fine if there's a solution to write directly to wifi files on the OS. I've seen various solutions to manually populate entries via ADB, but I'm having a tough time adapting that to Xamarin/Java (can't seem to access the /data/misc/wifi directories). Again, this is intended for use exclusively on our own managed devices.
I have a blog post about this topic here: https://blog.ostebaronen.dk/2020/11/android-11-wifi.html
Android Network API is not the greatest thing to work with as there are pitfals depending on the API level the code runs on.
From Android 10 and up a lot of the Network stuff has been restricted for "privacy" reasons, so you cannot work around not asking the user for input, unless the device is rooted or your App is set up as Device Admin.
For Android 11, there is a new API to present a system dialog and allow the user to save and connect to a network. This will look something like:
You can launch this through an Intent called android.settings.WIFI_ADD_NETWORKS:
var intent = new Intent(
"android.settings.WIFI_ADD_NETWORKS");
var bundle = new Bundle();
bundle.PutParcelableArrayList(
"android.provider.extra.WIFI_NETWORK_LIST",
new List<IParcelable>
{
new WifiNetworkSuggestion.Builder()
.SetSsid(ssid)
.SetWpa2Passphrase(password)
.Build()
});
intent.PutExtras(bundle);
StartActivityForResult(intent, AddWifiSettingsRequestCode);
You can then get the result in your Activity overriding OnActivityResult and fetching the result like so:
if (requestCode == AddWifiSettingsRequestCode)
{
if (data != null && data.HasExtra(
"android.provider.extra.WIFI_NETWORK_RESULT_LIST"))
{
var extras =
data.GetIntegerArrayListExtra(
"android.provider.extra.WIFI_NETWORK_RESULT_LIST")
?.Select(i => i.IntValue()).ToArray() ?? new int[0];
if (extras.Length > 0)
{
var ok = extras
.Select(GetResultFromCode)
.All(r => r == Result.Ok);
// if ok is true, BINGO!
return;
}
}
}
I have a repository here with the full sample: https://github.com/Cheesebaron/Android11WiFi
From using this in the wild, I've found that this API does not work nicely with some OEMs such as OnePlus and Huawei. Some of these either restrict or the System Settings App simply crashes due to a misconfiguration on their part. For those I fall back to the API's introduced in Android 10.
I need to set Screen Lock Setting to "None" in the android system.
I am deploying an app that is being sent out to users on pre-configured tablets.
This pre-configuration is done manually by the tablet supplier.
The settings to pre-configure is to set the Screen lock to None when the app is installing.
I don't think this is possible, the user has to do it them self.
Considering all apps (With a similar requirment) I have used so far send me to the "Select lock screen" screen & ask me to disable it manually.
I could be wrong tho!
Try this:
try{
Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor lockPatternUtilsConstructor =
lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(getContext());
Method setLockScreenDisabled = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class ,int.class);
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{true,0});
}
catch(Exception e){
Log.e(this.toString(),e.toString());
}
if you want change lock to swipe:
setLockScreenDisabled.invoke(lockPatternUtils, new Object[]{false,0});
but you need to change your app to system uid:
android:sharedUserId="android.uid.system"
I have an application. It is used to send and receive files using Bluetooth. My problem is, i wish to change the default location to store the file using Bluetooth at runtime. Can you tell me, Is there any possibility for this case?
It's not possible to configure it up to Android 4.1 Jelly Bean:
With the included stock bluetooth receiver you cannot change the location as of now, because it's hard coded. You need a 3rd party app like Bluetooth file transfer which might be configurable here.
In ./packages/apps/Bluetooth/src/com/android/bluetooth/opp/ you can see it.
Constants.java defines this:
public static final String DEFAULT_STORE_SUBDIR = "/bluetooth";
BluetoothOppReceiveFileInfo.java has no switch for the location:
File receiveFile = new File(uniqueFileName);
if (sDesiredStoragePath == null) {
sDesiredStoragePath = Environment.getExternalStorageDirectory().getPath() +
Constants.DEFAULT_STORE_SUBDIR;
}
I feel like I am on crazy pills right now. A specific part of my application has been working just fine for several days, and today it just stopped working and I can not figure out why. This part of my code used to output the total data since boot that each specific app has sent and received. Now, the values always show up as 0.
A couple things that may or may not be affecting this:
1.) My Nexus 4 was just updated to Android 4.3 today, but I doubt this is an issue because this worked just fine right after I updated.
2.) With the Android API 18 update, some methods from the Traffic Stats API are now deprecated, but these are methods I am not even using, so this should have no effect.
http://developer.android.com/reference/android/net/TrafficStats.html
All help is greatly appreciated.
PackageManager packageManager=this.getPackageManager();
List<ApplicationInfo> appList=packageManager.getInstalledApplications(0);
for (ApplicationInfo appInfo : appList) {
String appLabel = (String) packageManager.getApplicationLabel(appInfo);
int uid = appInfo.uid;
Log.d("data", String.valueOf(TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid)));
Update[January 23, 2014]: Testing the getUidRxBytes() and getUidTxBytes() on my Nexus 4 running Android 4.4.2 shows that the values are no longer 0, but are reporting the correct statistics.
I have reported the issue to the AOSP issue tracker: here
I have also created an alternate solution to the problem which I have pasted below:
private Long getTotalBytesManual(int localUid){
File dir = new File("/proc/uid_stat/");
String[] children = dir.list();
if(!Arrays.asList(children).contains(String.valueOf(localUid))){
return 0L;
}
File uidFileDir = new File("/proc/uid_stat/"+String.valueOf(localUid));
File uidActualFileReceived = new File(uidFileDir,"tcp_rcv");
File uidActualFileSent = new File(uidFileDir,"tcp_snd");
String textReceived = "0";
String textSent = "0";
try {
BufferedReader brReceived = new BufferedReader(new FileReader(uidActualFileReceived));
BufferedReader brSent = new BufferedReader(new FileReader(uidActualFileSent));
String receivedLine;
String sentLine;
if ((receivedLine = brReceived.readLine()) != null) {
textReceived = receivedLine;
}
if ((sentLine = brSent.readLine()) != null) {
textSent = sentLine;
}
}
catch (IOException e) {
}
return Long.valueOf(textReceived).longValue() + Long.valueOf(textReceived).longValue();
}
The TrafficStats class get the information about network traffic from the /proc/uid_stat/<uid> directory. This contains information about the tcp, udp bytes and packets sent and received. If the files are not present the TrafficStats class can't get the network stats. You can check if the files are present, If not you are out of luck and should look for other way.
If the files are present you can try to read it yourself.
Also the getUidTxBytes() and getUIDRxBytes() report only the TCP traffic and miss UDP traffic. So if your app is doing lots of UDP traffic (like voip) then you'll not get any info.
There is already a bug filed for this : https://code.google.com/p/android/issues/detail?id=32410
I have done some detailed research about this, and to clarify some details, since Android 4.3 the TrafficStats API has changed in the way it extracts details from the device.
Prior to Android 4.3 the UID traffic stats were available for TCP and UDP and included API for bytes and packets & sent and received. That data was extracted from the /proc/uid_stat/[pid]/* files.
In Android 4.3, the developers has decided to switch to a better and more safe API, using the xt_qtaguid UID statistics, which is part of the netfilter kernel module in Linux.
This API (procfs) allows access based on process UID, and this is why when you try to access to TrafficStats API in Android=>4.3 you will get zero information for not-own UID.
btw, the commit that caused the issue is the following:
https://github.com/android/platform_frameworks_base/commit/92be93a94edafb5906e8bc48e6fee9dd07f5049e
*Improve TrafficStats UID APIs.
Deprecate transport layer statistics, leaving only the summarized
network layer statistics.
Improve documentation to be clear about layers where measurements
occur, and their behavior since boot. Under the hood, move to using
xt_qtaguid UID statistics.
Bug: 6818637, 7013662
Change-Id: I9f26992e5fcdebd88c671e5765bd91229e7b0016*