Saturday, November 12, 2016

Java regular expression in Android dumpsys pattern matching

Objective: Extract dumpsys information and read the RSSI / signal strength value

// Dumpsys to grep the signal message
String signalValueText = "";
String dumpsys_log = Android.adbCommand(device, "adb -s " + Android.getSerialNumber(device) + " shell dumpsys telephony.registry | grep mSignalStrength=SignalStrength");
sleep(500);

// set regular expression to match pattern 
String pattern = "(.*)(SignalStrength:) (\\d{1,3}) (\\d{1,3}) -(\\d{1,3}) -(\\d{1,3}) -(\\d{1,3}) -(\\d{1,3}) -(\\d{1,3}) (\\d{1,3}) -(\\d{1,3}) -(\\d{1,3}) (\\d{1,3}) (\\d+) (\\d+) (gsm)(.*)";

// Create search pattern object
Pattern r = Pattern.compile(pattern);

// match the pattern and acquire right value, Extract all digits value from the string
Matcher m = r.matcher(dumpsys_log);
   if (m.find( )) {
    signalValueText = m.group(11);
   }else {
       Android.sequenceFail(deviceId, "dumpsys log NO match found! - FAILED");
   sleep(500);
   }
   
int signalValue = Integer.parseInt(signalValueText);
if (signalValue <100 amp="" signalvalue=""> 50)
{
System.out.println("Signal Strength = -" + signalValue);
sleep(500);
}
else 
{
System.out.println("Signal Strength range verification - FAILED, Current value = -" + signalValue);
sleep(500);
}

No comments: