1

I am writing an Android app using Xamarin (C#) which need to get the IMEI and roaming status from both SIM cards in a Dual SIM phone. I found some Java code to do it. But I don't know how to translate the Java reflection to C# (Even I do it using a Java-C# translator). Here is the link: Android : Check whether the phone is dual SIM

Could someone help me?

Here are the code snippet from the article that need to be translated to C#

private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String imei = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{ 

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);

        if(ob_phone != null){
            imei = ob_phone.toString();

        } 
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    } 

    return imei;
} 
Community
  • 1
  • 1
Wilton Kwok
  • 103
  • 8

1 Answers1

0

First add permission in your Manifest as

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

this will return IMEI android.telephony.TelephonyManager.getDeviceId().

J.K
  • 2,290
  • 1
  • 18
  • 29