how to get otp when sim is inserted in 1st slot only?

Issue

I am trying to develop an android project where OTP should recive only when number which user trying to register is inserted in first slot. How to do that? is this possible with help of firebase? and also want to autofill that otp with a dialogue box like user wants to read otp automatically or not?

Solution

Permission:

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

Use code

public String getNumber() { 
  
    if (ActivityCompat.checkSelfPermission(this, READ_SMS) == PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) ==
                    PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
        // Permission check
  
        // Create obj of TelephonyManager and ask for current telephone service
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String phoneNumber = telephonyManager.getLine1Number();
  
        
        return phoneNumber;
    } else {
        // Ask for permission
        requestPermission();
    return null;
    }
}

This will return the sim one number if it exists. Now check if it’s equal to the number provided by the user. Store the number in the database and check whether the same sim is inserted at startup.
Note that when you compare the numbers, their format must be the same.

Answered By – Humane Bicycle

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published