2G / 3G / 4G Network forcing

4.3k Views Asked by At

I'm busy writing some code to get signal strength and stuff but would like to programatically "force" my network connection to 2G / 3G / 4G so that for more conclusive measurements. I know there's a lot of applications capable of:

  1. forcing connection to a 2g/3g/4g Network
  2. forcing to a specific frequency band of channels
  3. even forcing measurements to specific cells (CIDs) of cell towers but have not had much progress with google thus far.

If someone can please point me into the right direction?, that would be great!

2

There are 2 best solutions below

0
On

I believe your requirements need a rooted phone. However if you want to force the network to be on LTE/GSM?CDMA, you can trigger those settings through an intent as follows:

    try{
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.setClassName("com.android.settings", "com.android.settings.RadioInfo");
        startActivity(intent);
    } catch(Exception e){
        Toast.makeText(getApplicationContext(), " Device not supported" , Toast.LENGTH_LONG).show();
    } 
3
On

PermissionUtils.java class for asking permission SDK >= 23

public class PermissionUtils {
Context context;
Activity current_activity;

PermissionResultCallback permissionResultCallback;
ArrayList<String> permission_list=new ArrayList<>();
ArrayList<String> listPermissionsNeeded=new ArrayList<>();

String dialog_content="";
int req_code;

public PermissionUtils(Context context)
{
    this.context=context;
    this.current_activity= (Activity) context;
    permissionResultCallback= (PermissionResultCallback) context;
}

public PermissionUtils(Context context, PermissionResultCallback callback)
{
    this.context=context;
    this.current_activity= (Activity) context;
    permissionResultCallback= callback;
}


public void check_permission(ArrayList<String> permissions, String dialog_content, int request_code)
{
    this.permission_list=permissions;
    this.dialog_content=dialog_content;
    this.req_code=request_code;

    if(Build.VERSION.SDK_INT >= 23)
    {
        if (checkAndRequestPermissions(permissions, request_code))
        {
            permissionResultCallback.PermissionGranted(request_code);
        }
    }
    else
    {
        permissionResultCallback.PermissionGranted(request_code);
    }

}


/**
 * Check and request the Permissions
 *
 * @param permissions
 * @param request_code
 * @return
 */

private  boolean checkAndRequestPermissions(ArrayList<String> permissions,int request_code) {

    if(permissions.size()>0)
    {
        listPermissionsNeeded = new ArrayList<>();

        for(int i=0;i<permissions.size();i++)
        {
            int hasPermission = ContextCompat.checkSelfPermission(current_activity,permissions.get(i));

            if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(permissions.get(i));
            }

        }

        if (!listPermissionsNeeded.isEmpty())
        {
            ActivityCompat.requestPermissions(current_activity, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),request_code);
            return false;
        }
    }

    return true;
}

/**
 *
 *
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
            if(grantResults.length>0)
            {
                Map<String, Integer> perms = new HashMap<>();

                for (int i = 0; i < permissions.length; i++)
                {
                    perms.put(permissions[i], grantResults[i]);
                }

                final ArrayList<String> pending_permissions=new ArrayList<>();

                for (int i = 0; i < listPermissionsNeeded.size(); i++)
                {
                    if (perms.get(listPermissionsNeeded.get(i)) != PackageManager.PERMISSION_GRANTED)
                    {
                        if(ActivityCompat.shouldShowRequestPermissionRationale(current_activity,listPermissionsNeeded.get(i)))
                            pending_permissions.add(listPermissionsNeeded.get(i));
                        else
                        {
                            Log.i("Go to settings","and enable permissions");
                            permissionResultCallback.NeverAskAgain(req_code);
                           return;
                        }
                    }

                }

                if(pending_permissions.size()>0)
                {
                    showMessageOKCancel(dialog_content,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    switch (which) {
                                        case DialogInterface.BUTTON_POSITIVE:
                                            Log.d("LOG","ON OK BTN CLICK CALLING CHECK PERMISSION METHOD");
                                            check_permission(permission_list,dialog_content,req_code);
                                            break;
                                        case DialogInterface.BUTTON_NEGATIVE:
                                            Log.i("permisson","not fully given");
                                            if(permission_list.size()==pending_permissions.size()){
                                                Log.d("LOG","ON NO BTN CLICK CALLING Permission Denided METHOD");
                                                //permissionResultCallback.PermissionDenied(req_code);
                                                check_permission(permission_list,dialog_content,req_code);
                                            }
                                            else{
                                                Log.d("LOG","ON NO BTN CLICK CALLING PartialPermissionGranted METHOD");
                                                permissionResultCallback.PartialPermissionGranted(req_code,pending_permissions);
                                            }

                                            break;
                                    }


                                }
                            });

                }
                else
                {
                    permissionResultCallback.PermissionGranted(req_code);
                }
            }
            break;
    }
}


/**
 * Explain why the app needs permissions
 *
 * @param message
 * @param okListener
 */
public void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
    new AlertDialog.Builder(current_activity)
            .setMessage(message)
            .setPositiveButton("Ok", okListener)
            .setNegativeButton("Cancel", okListener)
            .setCancelable(false)
            .create()
            .show();
}

public interface PermissionResultCallback
{
    void PermissionGranted(int request_code);
    void PartialPermissionGranted(int request_code, ArrayList<String> granted_permissions);
    void PermissionDenied(int request_code);
    void NeverAskAgain(int request_code);
}
}

Add this permission in your AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

TelephonyManagerHelper.java for Information

public class TelephonyManagerHelper implements PermissionUtils.PermissionResultCallback {

private Context context;
private Activity current_activity;
private boolean isPermissionGranted;
TelephonyManager telephonyManager;
String Information;


private ArrayList<String> permissions=new ArrayList<>();
private PermissionUtils permissionUtils;
private final static int REQUEST_CHECK_SETTINGS = 2000;


public TelephonyManagerHelper(Context context) {
    this.context=context;
    this.current_activity= (Activity) context;
    permissionUtils=new PermissionUtils(context,this);
    permissions.add(Manifest.permission.READ_PHONE_STATE);
    permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}

/**
 * Method to check the availability of location permissions
 * */

public void checkpermission()
{
    permissionUtils.check_permission(permissions,"Need permission for getting Phone information",1);
}

private boolean isPermissionGranted() {
    return isPermissionGranted;
}

/**
 * Handles the permission results
 */
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    permissionUtils.onRequestPermissionsResult(requestCode,permissions,grantResults);
}


/**
 * Handles the activity results
 */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    Information=getInformation();
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    break;
                default:
                    break;
            }
            break;
    }
}

public String getInformation() {
    try{
        telephonyManager= (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);

        int phoneType=telephonyManager.getPhoneType();
        String PhoneType="";
        if (phoneType==telephonyManager.PHONE_TYPE_NONE){  //0
            PhoneType="NONE";
        }else if(phoneType==telephonyManager.PHONE_TYPE_GSM){  //1
            PhoneType="GSM";
        }else if(phoneType==telephonyManager.PHONE_TYPE_CDMA){ //2
            PhoneType="CDMA";
        }else if(phoneType==telephonyManager.PHONE_TYPE_SIP){ //3
            PhoneType="SIP";
        }else{
            PhoneType="Not Found";
        }

        int networkType=telephonyManager.getNetworkType();
        String NetworkType="";

        if (networkType==telephonyManager.NETWORK_TYPE_UNKNOWN){   //0
            NetworkType="UNKNOWN";
        }else if(networkType==telephonyManager.NETWORK_TYPE_GPRS){ //1
            NetworkType="GPRS";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_EDGE){ //2
            NetworkType="EDGE";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_UMTS){ //3
            NetworkType="UMTS";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_CDMA){ //4
            NetworkType="CDMA";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_EVDO_0){ //5
            NetworkType="EVDO 0";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_EVDO_A){ //6
            NetworkType="EVDO A";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_1xRTT){ //7
            NetworkType="1xRTT";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_HSDPA){ //8
            NetworkType="HSDPA";
        }
        else if(networkType==telephonyManager.NETWORK_TYPE_HSUPA){ //9
            NetworkType="HSUPA";
        }else if(networkType==telephonyManager.NETWORK_TYPE_HSPA){ //10
            NetworkType="HSPA";
        }else if (networkType==telephonyManager.NETWORK_TYPE_IDEN){ //11
            NetworkType="IDEN";
        }else if(networkType==telephonyManager.NETWORK_TYPE_EVDO_B){ //12
            NetworkType="EVDO B";
        }else if(networkType==telephonyManager.NETWORK_TYPE_LTE){ //13
            NetworkType="LTE";
        }else if(networkType==telephonyManager.NETWORK_TYPE_EHRPD){ //14
            NetworkType="EHRPD";
        }else if(networkType==telephonyManager.NETWORK_TYPE_HSPAP){ //15
            NetworkType="HSPAP";
        }else if(networkType==telephonyManager.NETWORK_TYPE_GSM){ //16
            NetworkType="GSM";
        }else if(networkType==telephonyManager.NETWORK_TYPE_TD_SCDMA){ //17
            NetworkType="SCDMA";
        }else {                                                       // 18
            NetworkType="IWLAN";
        }


        @SuppressLint("MissingPermission")
        String DeviceId = telephonyManager.getDeviceId();
        @SuppressLint("MissingPermission")
        String PhoneNumber = telephonyManager.getLine1Number();

        @SuppressLint("MissingPermission")
        String SubscribeIdIMSI=telephonyManager.getSubscriberId();

        @SuppressLint("MissingPermission")
        String NetworkCountryISO= telephonyManager.getNetworkCountryIso();
        String NetworkOperator=telephonyManager.getNetworkOperator();
        int MCC = Integer.parseInt(NetworkOperator.substring(0, 3)); // mobile country code
        int MNC = Integer.parseInt(NetworkOperator.substring(3));    // mobile network code

        String NetworkOperatorName=telephonyManager.getNetworkOperatorName();
        String SimCountryIso=telephonyManager.getSimCountryIso();
        String SimOperator=telephonyManager.getSimOperator();
        String SimOperatorName=telephonyManager.getSimOperatorName();
        @SuppressLint("MissingPermission")
        String SimSerialNumber=telephonyManager.getSimSerialNumber();
        int simState=telephonyManager.getSimState();

        String SimState="";
        if (simState==telephonyManager.SIM_STATE_UNKNOWN){ //0
            SimState="UNKNOWN";
        }else if (simState==telephonyManager.SIM_STATE_ABSENT){ //1
            SimState="ABSENT";
        }else if (simState==telephonyManager.SIM_STATE_PIN_REQUIRED){ //2
            SimState="PIN REQUIRED";
        }else if (simState==telephonyManager.SIM_STATE_PUK_REQUIRED){ //3
            SimState="PUK REQUIRED";
        } else if (simState==telephonyManager.SIM_STATE_NETWORK_LOCKED){ //4
            SimState="NETWORK LOCKED";
        }else if (simState==telephonyManager.SIM_STATE_READY){ //5
            SimState="READY";
        }else if (simState==telephonyManager.SIM_STATE_NOT_READY)//6
        {
            SimState="NOT READY";
        }else if(simState==telephonyManager.SIM_STATE_PERM_DISABLED){ //7
            SimState="PERM DISABLED";
        }else if(simState==telephonyManager.SIM_STATE_CARD_IO_ERROR){ //8
            SimState="CARD IO ERROR";
        }else { //9
            SimState="CARD RESTRICTED";
        }


        @SuppressLint("MissingPermission")
        GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
        int CID = gsmCellLocation.getCid(); // CellID
        int LAC = gsmCellLocation.getLac(); // Location Area Code

        Information="Phone Type : "+PhoneType+"\n\n"+
                "Network Type: "+NetworkType+"\n\n"+
                "DeviceId:  "+DeviceId+"\n\n"+
                "PhoneNumber: "+PhoneNumber+"\n\n"+
                "SubscribeIdIMSI :  "+SubscribeIdIMSI+"\n\n"+
                "NetworkCountryISO :  "+NetworkCountryISO+"\n\n"+
                "NetworkOperator :  "+NetworkOperator+"\n\n"+
                "MCC Mobile Country Code:"+MCC+"\n\n"+
                "MNC Mobile Network Code:"+MNC+"\n\n"+
                "NetworkOperatorName :"+NetworkOperatorName+"\n\n"+
                "SimCountryIso :"+SimCountryIso+"\n\n"+
                "SimOperator :"+SimOperator+"\n\n"+
                "SimOperatorName  :"+SimOperatorName+"\n\n"+
                "SimSerialNumber  :"+SimSerialNumber+"\n\n"+
                "Sim State :"+SimState+"\n\n"+
                "cellID :"+CID+"\n\n"+
                "LAC Location Area Code:"+LAC+"\n\n";
    }catch (Exception e){e.printStackTrace();}
    return Information;

}

@Override
public void PermissionGranted(int request_code) {
    Log.i("PERMISSION","GRANTED");
    isPermissionGranted=true;
}

@Override
public void PartialPermissionGranted(int request_code, ArrayList<String> granted_permissions) {
    Log.i("PERMISSION PARTIALLY","GRANTED");
}

@Override
public void PermissionDenied(int request_code) {
    Log.i("PERMISSION","DENIED");
}

@Override
public void NeverAskAgain(int request_code) {
    Log.i("PERMISSION","NEVER ASK AGAIN");
    permissionUtils.showMessageOKCancel("App Required To Get Phone State \nPlease Grant Permission from AppInfo > Permissions > Phone , Location",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", BuildConfig.APPLICATION_ID, null);
                            intent.setData(uri);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(intent);
                            break;
                        case DialogInterface.BUTTON_NEGATIVE:

                            break;
                    }


                }
            });
}

}

SimCardInformationActivity.java display information

public class SimCardInformationActivity extends AppCompatActivity {
 TextView txtSimInformation;
 TelephonyManagerHelper telephonyHelper;
   String Info;
    @Override
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sim_card_information);
    txtSimInformation=findViewById(R.id.txtSimInformation);
    telephonyHelper=new TelephonyManagerHelper(SimCardInformationActivity.this);
    telephonyHelper.checkpermission();

    getTelephonyInformaiton();


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("LOG:On Activity Result","Request Code "+requestCode+"  Result Code"+requestCode);
    telephonyHelper.onActivityResult(requestCode,resultCode,data);
}

// Permission check functions
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    // redirects to utils
    Log.d("LOG:onRequestPermResult","Request Code "+requestCode+"  Grant Result"+grantResults);
    telephonyHelper.onRequestPermissionsResult(requestCode,permissions,grantResults);
}


private void getTelephonyInformaiton() {
      Info=telephonyHelper.getInformation();
      txtSimInformation.setText(Info);
}

@Override
protected void onResume() {
    super.onResume();
    getTelephonyInformaiton();
}

}

activity_sim_card_information.xml for layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app.SimCardInformationActivity">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txtSimInformation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </ScrollView>

</RelativeLayout>