1

I have an application with a translucent activity destroyed with finish() after starting a service that runs on the background and listens to USB_STATE action then executes this command: service call connectivity CODE i32 1 when connected. very simple.

That's to enable USB Tethering (using root of course). But the real problem is CODE since it differs from SDK to another. for example, if I typed service call connectivity 39 i32 1, it'll only run on Samsung Nougat.

So, I thought of two different ways to overcome this

  • 1st: Creating a switch case statement that switches Build.VERSION.SDK_INT and according to the evaluated case, It'll set the service code from the list that I've taken from this answer...

Bug: That list is missing a lot of old & new service codes

Even if I've got them totally! (means all the cases of SDK_INT & service CODEs). Then what about incoming new versions of Android? Who wants to update such a simple App every android release!

  • 2nd: A very bad way like iterating i from 30 to 100 replacing CODE in the command above and executing it then what works, well... just works.

Bug: Bad coding style, consumes more power and the app may stop responding if fired a lot.

I just need my app to dynamically && programmatically get the service CODE so how?

EDIT: I tried using reflection but it doesn't work either.

try {
// Also tried setUsbTethering
// and ConnectivityManager.setUsbTethering
            Method method = this.getClass().getMethod("IConnectivityManager.setUsbTethering", Integer.class);
            method.invoke(null, true);
            Toast.makeText(this, "turned on", Toast.LENGTH_LONG).show();
        }catch (Exception e)
        {
            Log.e("AUTO TETHERING", e.getMessage(), e);
        }

But I get NoSuchMethodException

Beyondo
  • 2,952
  • 1
  • 17
  • 42
  • You can't. The constants are part of the `com.android.server` classpath, which isn't visible to applications. What does code 39 do? There might be a way to do this through the API. – TheWanderer Jan 09 '19 at 16:03
  • @TheWanderer Okay thanks for the info but believe me, there's no way that worked *for me* except the superuser way. – Beyondo Jan 09 '19 at 16:05
  • Can you just say what it is please? – TheWanderer Jan 09 '19 at 16:06
  • @TheWanderer Say what? everything is explained in very very details in the post. Just an app that auto-tethers USB when connected. – Beyondo Jan 09 '19 at 16:07
  • Sorry, I missed that part. One minute. – TheWanderer Jan 09 '19 at 16:11
  • So just to confirm, you want to start USB Tethering? Have you seen [this method](https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/net/ConnectivityManager.java#L2454)? It's hidden, so you'll need reflection, but as long as you have WRITE_SETTINGS access you can use it. – TheWanderer Jan 09 '19 at 16:26
  • 1
    @AlexP I don't think this is a duplicate. OP appears to have an XY problem. – TheWanderer Jan 09 '19 at 16:26
  • @TheWanderer, the OP has quite a few problems, but the one he decided to put in the title is the exact one answered in the duplicate post. – Alex P. Jan 09 '19 at 19:06
  • @AlexP. But it's not the actual question. – TheWanderer Jan 09 '19 at 19:36
  • @TheWanderer Actually not. The mentioned post seeks a *documentation* for the `service call` but here I ask about how to get only the code to exec `call service connectivity` command and also doesn't answer my question since I said `programmatically` (actual code) and not a bash file that I don't know how to run for a newbie like me. In SO, I come across an answer somewhere *before* that's been downvoted a lot because it is linked to a blog or *temporary* sources like that answer in the question you said I duplicated it. Answers need to be in the same post's context or with verified sources. – Beyondo Jan 09 '19 at 20:31
  • 1
    @XStylish I think you wanted to ping Alex. I've voted to reopen your question, but in the meantime, did you take a look at that link? I think it does what you want, and you don't even need root to do it. – TheWanderer Jan 09 '19 at 20:39
  • @TheWanderer Oops... Yes, I've tried it using reflection but unfortunately, it doesn't work... I also tried a lot of apps in the play store, none of them works and most of them require devices to be installed on the PC but I don't want that. So there's only one app named `AUT - Auto USB Tether` that does exactly want I want and it works! It uses root just like I do and I just tried to decompile it to see how it gets the `service code`. But I fail since most of the functions/variables names are encrypted and hard to understand – Beyondo Jan 09 '19 at 20:50
  • @AlexP. Sorry but please reopen the question so once I find an answer.. at least I can post it here. – Beyondo Jan 09 '19 at 20:52
  • @XStylish what does "doesn't work" mean? Does it throw an Exception? – TheWanderer Jan 09 '19 at 20:54
  • @TheWanderer Nope.. I meant it just doesn't change anything or the usb_tethering status – Beyondo Jan 09 '19 at 20:56
  • Can you edit what you have into your question? – TheWanderer Jan 09 '19 at 21:00
  • @TheWanderer Sorry.. after re-coding it again, I noticed that I catched the Exception but I didn't `Log.e`/`Toast` anything. Yes, it throws an exception... I'll post the code now.. – Beyondo Jan 09 '19 at 21:15
  • OK, your reflection is completely wrong. [Try this](https://pastebin.com/H3PHnsF3). Remember, you need WRITE_SETTINGS to be granted to your app for this to work. https://stackoverflow.com/questions/32083410/cant-get-write-settings-permission – TheWanderer Jan 09 '19 at 21:27
  • Sorry if that first sentence sounds rude. It wasn't meant to be. – TheWanderer Jan 09 '19 at 21:43
  • @TheWanderer No, it's true because I stated I'm a newbie in the comment above.. I didn't reply on-time because I tried your code and it doesn't throw the same exception anymore but now I've got a totally different problem so I was trying to solve it as I can do then I was going tell you what I've done so far to solve it if I didn't succeed.. not because I'm mad or anything. BTW that's what happened.. first, I got an exception says: `W/System.err: Caused by: java.lang.SecurityException: ConnectivityService: Neither user 10253 nor current process has android.permission.ACCESS_NETWORK_STATE.` – Beyondo Jan 09 '19 at 21:58
  • Did you grant access to WRITE_SETTINGS? – TheWanderer Jan 09 '19 at 21:58
  • So I've granted `ACCESS_NETWORK_STATE` but then it threw `W/System.err: Caused by: java.lang.SecurityException: You either need MANAGE_USERS or CREATE_USERS permission to: query user`. – Beyondo Jan 09 '19 at 21:58
  • @TheWanderer Yes, I did and made sure using `Settings.System.canWrite(this)` and checked on `ACTION_MANAGE_WRITE_SETTINGS` activity. – Beyondo Jan 09 '19 at 21:59
  • What's your test device and OS? – TheWanderer Jan 09 '19 at 22:00
  • @TheWanderer SM-G610F, J7 Prime, Android Version: 7.0 (Didn't get the latest update since I was rooted) and its service code is **`40`**. – Beyondo Jan 09 '19 at 22:03
  • 1
    Alright I think you'll need to use root then. Implement [libRootJava](https://github.com/Chainfire/librootjava) in your project and follow the instructions for creating and starting the root class, and making a Binder IPC. You'll want to name the AIDL method something like `setUsbTetherMode(boolean enabled)`. Then you'll want to use [this](https://pastebin.com/GrCg4BRq) in your implementation of the AIDL method inside the root class. – TheWanderer Jan 09 '19 at 22:17

0 Answers0