1

Google told me that in order for me to be able to use either TelephonyManager or TelcomManager to read device identifiers I need one of the requirements stated on the documentation for the respective device identifiers such as GetImei or GetLine1Number. I decided to meet the requirement that states The caller needs to be the default SMS Role holder on that device. So I created an app that is listed as an alternative SMS app on my device and then I check if my app holds that role and then try to get the device identifiers. My code reads the IMEI of both sim cards successfully but fails to read the phone number of the device as the method returns null but I did not expect this as the default sms app should be able to access that number for texting purposes. I am using the following code, help me make this work.

try
            {
                if (roleManager.IsRoleHeld(RoleManager.RoleSms))
                {
                    var manager = (TelephonyManager)GetSystemService(Context.TelephonyService);
                    new Android.App.AlertDialog.Builder(this).SetTitle("Device Identifiers")
                                  .SetMessage("phone no " + manager.Line1Number + "\n" + "line 1 imei: " + manager.GetImei(0) + "\n" +
                                  "line 2 imei: " + manager.GetImei(1) + "\n" +
                                  "serial no: " + manager.SimSerialNumber

                                  ).Show();
                    //telephonyManager.GetLine1 fails so
                    //try and use the telecom service to read the number
                    var telcom = (TelecomManager)GetSystemService(Context.TelecomService);
                    //get a list of all capable calling accounts
                    IList<PhoneAccountHandle> handles = telcom.CallCapablePhoneAccounts;
                    if (handles != null)
                    {
                        //Toast the phone capable calling accounts count
                        Toast.MakeText(this, handles.Count.ToString(), ToastLength.Short).Show();
                        //get the phone account handle in index 0
                        PhoneAccountHandle handle1 = handles[0];
                        //get the phone number associated with that acount
                        string phone1 = telcom.GetLine1Number(handle1);
                        if (phone1 != null)
                        {
                            new Android.App.AlertDialog.Builder(this).SetTitle("Phone Number")
                                .SetMessage(phone1).Show();
                        }
                    }
                    string phone = telcom.GetLine1Number(handles[1]);
                    if (phone != null)
                    {
                        Toast.MakeText(this,phone,ToastLength.Short).Show();
                    }
                    else
                    {
                        Toast.MakeText(this, "GetLine1 is returning null", ToastLength.Short).Show();
                    }
                }
            } catch (Java.Lang.SecurityException exc)
            {
                Toast.MakeText(this, exc.Message, ToastLength.Short).Show();
            }

An answer in Android Java is also acceptable, Thank You.

TechGeek
  • 316
  • 1
  • 11

1 Answers1

2

At first, please check the AndroidManifest.xml if you added the <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in it or not.

In addition, there is no reliable way to get the phone number from the SIM card because some telecom operators don't add this information in SIM card and someone did. So sometimes you can get the number and sometimes you will get a null even a phone number you used before.

The most used solution on the SO is using the Google Play Service to get the phone number. You can check this case. And in the Xamarin, you can use the package named Xamarin.GooglePlayServices.Auth to do that.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14