Mariusz
Mariusz

Reputation: 1410

Get CellSignalStrength from NeighboringCellInfo

i need to get signal level from CellSignalStrength.getLevel()

i take it from CellInfoGsm by

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
CellInfo allCellInfo = (CellInfo) telephonyManager.getAllCellInfo().get(0);
CellSignalStrength cellSignalStrength = ((CellInfoGsm) allCellInfo).getCellSignalStrength();
int level = cellSignalStrength.getLevel();

but on some devices i get null from telephonyManager.getAllCellInfo() so ad android doc said i use telephonyManager.getNeighboringCellInfo()

is there any way to get CellSignalStrength or "user friendly" signal level from NeighboringCellInfo?

Upvotes: 0

Views: 715

Answers (1)

Mariusz
Mariusz

Reputation: 1410

i solved it - i guess i do - by rewrite getLevel() function from CellInfoGsm

i call NeighboringCellInfo.getRssi() and then convert it to level, like CellInfoGsm.getLevel() do it

for(NeighboringCellInfo nci:telephonyManager
                    .getNeighboringCellInfo()){
                if(neighborCellInfo == null) {
                    neighborCellInfo = nci;
                } else if(nci.getRssi() > neighborCellInfo.getRssi()) {
                    neighborCellInfo = nci;
                }
            }

            int level;

            int GSM_SIGNAL_STRENGTH_GREAT = 12;
            int GSM_SIGNAL_STRENGTH_GOOD = 8;
            int GSM_SIGNAL_STRENGTH_MODERATE = 8;

            // ASU ranges from 0 to 31 - TS 27.007 Sec 8.5
            // asu = 0 (-113dB or less) is very weak
            // signal, its better to show 0 bars to the user in such cases.
            // asu = 99 is a special case, where the signal strength is unknown.
            int asu = neighborCellInfo.getRssi();
            if (asu <= 2 || asu == 99) level = 0;
            else if (asu >= GSM_SIGNAL_STRENGTH_GREAT) level = 4;
            else if (asu >= GSM_SIGNAL_STRENGTH_GOOD)  level = 3;
            else if (asu >= GSM_SIGNAL_STRENGTH_MODERATE)  level = 2;
            else level = 1;

Upvotes: 1

Related Questions