1

I have a Windows Form Application. I want to change change Android device mobile data which is connected to the PC with USB.

How can I change the state of Android device mobile data in Windows Form Application by using Xamarin.

Is there any way to change the state of mobile data?

Fatih TAN
  • 778
  • 1
  • 5
  • 23

2 Answers2

1

This should do it:

void SetMobileDataEnabled(bool enabled)
{
    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.L) {
        Console.WriteLine ("Device does not support mobile data toggling.");
        return;
    }

    try {
        if (Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.KitkatWatch 
            && Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Gingerbread) {
            Android.Net.ConnectivityManager conman = (Android.Net.ConnectivityManager)GetSystemService (ConnectivityService);
            Java.Lang.Class conmanClass = Java.Lang.Class.ForName (conman.Class.Name);
            Java.Lang.Reflect.Field iConnectivityManagerField = conmanClass.GetDeclaredField ("mService");
            iConnectivityManagerField.Accessible = true;
            Java.Lang.Object iConnectivityManager = iConnectivityManagerField.Get (conman);
            Java.Lang.Class iConnectivityManagerClass = Java.Lang.Class.ForName (iConnectivityManager.Class.Name);
            Java.Lang.Reflect.Method setMobileDataEnabledMethod = iConnectivityManagerClass.GetDeclaredMethod ("setMobileDataEnabled", Java.Lang.Boolean.Type);
            setMobileDataEnabledMethod.Accessible = true;

            setMobileDataEnabledMethod.Invoke (iConnectivityManager, enabled);
        }

        if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Gingerbread) {

            TelephonyManager tm = (TelephonyManager)GetSystemService (Context.TelephonyService);

            Java.Lang.Class telephonyClass = Java.Lang.Class.ForName (tm.Class.Name);
            Java.Lang.Reflect.Method getITelephonyMethod = telephonyClass.GetDeclaredMethod ("getITelephony");
            getITelephonyMethod.Accessible = true;

            Java.Lang.Object stub = getITelephonyMethod.Invoke (tm);
            Java.Lang.Class ITelephonyClass = Java.Lang.Class.ForName (stub.Class.Name);

            Java.Lang.Reflect.Method dataConnSwitchMethod = null;
            if (enabled) {
                dataConnSwitchMethod = ITelephonyClass
                    .GetDeclaredMethod ("disableDataConnectivity");
            } else {
                dataConnSwitchMethod = ITelephonyClass
                    .GetDeclaredMethod ("enableDataConnectivity");   
            }

            dataConnSwitchMethod.Accessible = true;
            dataConnSwitchMethod.Invoke (stub);
        } 
    } catch (Exception ex) {
        Console.WriteLine ("Device does not support mobile data toggling.");
    }
}

Enable the ChangeNetworkState and ModifyPhoneState permissions in your manifest.

Android L currently has no available way to disable/enable mobile data.

IAmCoder
  • 3,179
  • 2
  • 27
  • 49
1

There is no need for (actually no use of using) Xamarin for this task. All you'll need is the Android Debug Bridge (ADB) (see the linked page for instructions on how to install it)

To enable and disable the mobile data connection, use the commands

adb shell svc data enable
adb shell svc data disable

(see this answer, I've not marked this question as duplicate because it is scoped a bit differently)

Please note that USB debugging has to be enabled on the device.

In your windows application, you can implement the following class to en- or disable mobile data.

class MobileDeviceService
{
    public void DisableMobileData()
    {
        Process.Start("adb.exe", "shell svc data disable");
    }

    public void EnableMobileData()
    {
        Process.Start("adb.exe", "shell svc data enable");
    }
}

If you'd like to hide the window or block until the command has finished, you can use Process.Start(ProcessStartInfo) with a StartInfo instance (see the docs for ProcessStartInfo) that has been configured respectively.

If there is more than one device attached to your PC, you can list the connected devices with

adb devices

and then use the -s <SERIAL> option, to select the respective device

public void EnableMobileData(string deviceSerial)
{
    Process.Start("adb.exe", $"-s {deviceSerial} shell svc data enable");
}
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • Excellent. It worked for Vestel Venus Go. Release: (8.1.0). SDK (27). But it did not work for HTC Sensation XE with Beats Audio Z715e. Release(4.0.3). SDK(15). After enable or disable data command it gives me Permission Denied. – Fatih TAN Nov 14 '19 at 08:25
  • @FatihTAN It seems to depend. I've read about the command that the device has to be rooted in order to use them. Obviously the HTC won't allow the commands unless it's rooted (possibly you'll have to `su` before). – Paul Kertscher Nov 14 '19 at 12:14
  • Anyway, since it worked for my unrooted devices, I assumed that it'd work without. – Paul Kertscher Nov 14 '19 at 12:15