androidandroid-sourcetelephonymanagerrilandroid-9.0-pie

TelephonyManager.requestNetworkScan - Android P Preview


Has anyone tried this API? I'm developing a privileged system app to scan the network. For some reason I get error 2 'Wrong parameters' when I try using it. Granted the app MODIFY_PHONE_STATE permission, and still get the error Here's an extract:

public class ScannerActivity extends Activity implements View.OnClickListener {

private final int PHONE_STATE_REQUEST = 1;
private Button scanButton;
private TextView resultsTextView;

private class RadioCallback extends TelephonyScanManager.NetworkScanCallback {
    private List<CellInfo> mCellInfoResults;
    private int mScanError;

    @Override
    public void onResults(List<CellInfo> cellInfoResults) {
        mCellInfoResults = cellInfoResults;
           ScannerActivity.this.runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   for (CellInfo cellInfo:mCellInfoResults) {
                       resultsTextView.append(" " + cellInfo.toString() + " ");
                   }
               }
           });
    }


    @Override
    public void onError(int error) {
        mScanError = error;
        ScannerActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                resultsTextView.append(" Error: " + mScanError);
            }
        });
    }

    @Override
    public void onComplete() {
        ScannerActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                resultsTextView.append(" Scan Completed! ");
            }
        });
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner);

    scanButton = (Button) findViewById(R.id.scan_button);
    scanButton.setOnClickListener(this);
    resultsTextView = (TextView) findViewById(R.id.results_text_view);
}



public void onClick(View view) {

    NetworkScanRequest networkScanRequest;
    RadioAccessSpecifier radioAccessSpecifiers[];
    int bands[];
    ArrayList<String> PLMNIds = new ArrayList<String>(Arrays.asList("42501"));


    TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    bands[0] = AccessNetworkConstants.UtranBand.BAND_1;
    radioAccessSpecifiers = new RadioAccessSpecifier[1];
    radioAccessSpecifiers[0] = new RadioAccessSpecifier(
            AccessNetworkConstants.AccessNetworkType.UTRAN,
            bands,
            null);

    networkScanRequest = new NetworkScanRequest(
            NetworkScanRequest.SCAN_TYPE_ONE_SHOT,
            radioAccessSpecifiers,
            0,
            60,
            false,
            0,
            PLMNIds);

    telephonyManager.requestNetworkScan(networkScanRequest, AsyncTask.SERIAL_EXECUTOR,new RadioCallback());
}

}

Does anyone has an idea what parameters did i pass wrong? I first thought that it might be LTE / 3G issue, but I get the same results both for UTRAN & EUTRAN.

I also ruled out SIM issue - the same error happens with 2 different SIMS, both successfully scanning the network form settings app for example.


Solution

  • OK, figured it out - for anyone who may come across this issue -

    searchPeriodicity & incrementalResultsPeriodicity cannot be 0, despite the documentation, and irrelevance of these parameters for one shot scan.