-1

I need to retrieve the IMEI Number and phone number from the sim card. I got successfully get an IMEI number from the sim card. But not retrieve the phone number. It shows empty in using the telephony manager line number. Can anyone know some other ideas to get the phone number information? I am using the above code:

if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadSms) != Android.Content.PM.Permission.Granted &&
        Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadPhoneNumbers) !=
        Android.Content.PM.Permission.Granted && Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this,
        Manifest.Permission.ReadPhoneState) != Android.Content.PM.Permission.Granted && Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != Android.Content.PM.Permission.Granted)
{
            RequestPermissions(new String[] { Manifest.Permission.ReadPhoneState, Manifest.Permission.ReadPhoneNumbers, Manifest.Permission.ReadSms,Manifest.Permission.AccessCoarseLocation }, PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
else
{
            setDeviceImei();
}
private void setDeviceImei()
{
            var tMgr = (TelephonyManager)Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.TelephonyService);
            Console.WriteLine(tMgr.Line1Number);
            //string deviceId = getDeviceID(telephonyManager,0);
            //string deviceId1 = getDeviceID(telephonyManager, 1);
            String imeiNumber1 = tMgr.GetDeviceId(0); //(API level 23)   
            String imeiNumber2 = tMgr.GetDeviceId(1);
            Console.WriteLine(imeiNumber1, imeiNumber2);
            SubscriptionManager smgr = (SubscriptionManager)GetSystemService(TelephonySubscriptionService);
            SubscriptionInfo sim1 = smgr.GetActiveSubscriptionInfoForSimSlotIndex(0);
            SubscriptionInfo sim2 = smgr.GetActiveSubscriptionInfoForSimSlotIndex(1);
            Console.WriteLine(sim1);
            Console.WriteLine(sim2);
}
Saamer
  • 4,687
  • 1
  • 13
  • 55
eve
  • 39
  • 7
  • hi, I believe android will soon restrict you from getting the IMEI. its best to prepare for it, check this https://developer.android.com/about/versions/10/privacy/changes#data-ids – redanesc Jun 11 '20 at 15:21

1 Answers1

0

You have to remember that Xamarin mobile development is basically native development using C#, so you are bound by similar limitations as the developers building the apps in the Native languages (Obj-C for iOS, Java for Android)

iOS

Apple has very strong privacy implementations and so they do not allow apps to "steal" any information that can uniquely a device/user. So there's no way of getting the IMEI number or Phone Number from the device directly. You can use "private APIs" to get it while you are testing, but the app will be rejected during the review process, so that's not useful

However, there is a "legal" way to capture the phone number without any user data entry that hasn't been blocked yet, as described here:

The idea is to have the app programmatically send a SMS message to a server with the app’s unique installation code. The app can then query the same server to see if it has recently received a SMS message from a device with this unique app installation code. If it has, it can read the phone number that sent it. Here’s a demo video showing the process. As you can see, it works like a charm!

Android

You already figured out the IMEI number. For the phone number, there isn't a reliable way, though you can use the same method as iOS described above. Here are some suggestions:

  • Make sure in the code you have tried, that you have added the permission READ_PHONE_STATE: <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
  • If that doesn't work, try to add the permissions READ_CONTACTS & READ_PROFILE, and add the using Android.Provider; along with this code:
var uri = ContactsContract.Contacts.ContentUri;
string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.CommonDataKinds.Phone.Number };

var cursor = ContentResolver.Query(uri, projection, null, null, null);

var contactList = new List<string>();

if (cursor.MoveToFirst())
{
    do
    {
        if (!cursor.MoveToNext())
            break;
        var phoneNumber = cursor.GetString(4); // This is the user's phone#
    } while (true);
}

What's the use case for this? I might be able to suggest a better solution for you.

Saamer
  • 4,687
  • 1
  • 13
  • 55