Fatih
Fatih

Reputation: 810

Turn On/Off Android Device Mobile Data in C# Windows Form Application By Using Xamarin.Android

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?

Upvotes: 1

Views: 758

Answers (2)

Paul Kertscher
Paul Kertscher

Reputation: 9742

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");
}

Upvotes: 1

IAmCoder
IAmCoder

Reputation: 3442

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.

Upvotes: 1

Related Questions