I am developing an Android App to be used in Android OS mobile Point of Sale (POS) terminals (Android 7) that has two SIM Slots. I need the App, upon app start, to first test the signal strength in both SIMs and automatically select to work using the SIM with the best signal strength. Afterwards, to periodically (every 15/30/60 mins according to the chosen settings) check the signal strength again for both SIMS, and automatically switch default SIM for Data to the SIM with the best signal strength. So far I am able to make the App measure the signal strength in both SIMs, and display them to the user, so as to select the SIM to work with. This is achieved using Cellinfo in telephonyManager function and the following code which I found here:
private static String getSignalStrength (Context context) throws SecurityException {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String strength = "";
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "";
}
List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); //This will give info of all sims present inside your mobile
if(cellInfos != null) {
for (int i = 0 ; i < cellInfos.size() ; i++) {
if (cellInfos.get(i).isRegistered()) {
if (cellInfos.get(i) instanceof CellInfoWcdma) {
CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfos.get(i);
CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
strength += String.valueOf(cellSignalStrengthWcdma.getDbm());
} else if (cellInfos.get(i) instanceof CellInfoGsm) {
CellInfoGsm cellInfogsm = (CellInfoGsm) cellInfos.get(i);
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfogsm.getCellSignalStrength();
strength += String.valueOf(cellSignalStrengthGsm.getDbm());
} else if (cellInfos.get(i) instanceof CellInfoLte) {
CellInfoLte cellInfoLte = (CellInfoLte) cellInfos.get(i);
CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();
strength += String.valueOf(cellSignalStrengthLte.getDbm());
} else if (cellInfos.get(i) instanceof CellInfoCdma) {
CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfos.get(i);
CellSignalStrengthCdma cellSignalStrengthCdma = cellInfoCdma.getCellSignalStrength();
strength += String.valueOf(cellSignalStrengthCdma.getDbm());
}
strength+=";";
}
}
}
return strength;
}
I am now stuck at how to switch the default SIM for data automatically, and without the need for user intervention.