mercredi 18 novembre 2015

How to instantiate PhoneFactory in android using reflection

I want to instantiate a PhoneFactory to send AT command to GSM modem in android. I have used the following code in a new Thread :

 try{
        Looper.prepare();
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        String TAG = "my tag";
        final Class<?> classPhoneFactory = classLoader.loadClass("com.android.internal.telephony.PhoneFactory");
        Log.i(TAG, "Class loaded " + classPhoneFactory.toString());

        Method[] metods = classPhoneFactory.getMethods();
        for(int i=0;i<metods.length;i++){
            if(metods[i].getName().equals("makeDefaultPhone")){
                System.out.println(metods[i].getName());
                Class<?>[] pType = metods[i].getParameterTypes();
                for(int j = 0 ; j<pType.length;j++ ){
                    System.out.println(pType[j].getName());
                }
                Log.i(TAG, "Method loaded " + metods[i].getName());
                metods[i].invoke(classPhoneFactory, appContext);
            }
        }


        Method methodGetDefaultPhone = classPhoneFactory.getDeclaredMethod("getDefaultPhone");
        Log.i(TAG, "Method loaded " + methodGetDefaultPhone.getName());

        Object objectCallManager = methodGetDefaultPhone.invoke(null);
        Log.i(TAG, "Object loaded " + objectCallManager.getClass().getName());

        Method []objectCallManagerMethods = objectCallManager.getClass().getMethods();
        for(int i =0;i<objectCallManagerMethods .length;i++){
            if(objectCallManagerMethods[i].getName().equals("invokeOemRilRequestStrings")){
                MyMessageHandler mhandler = new MyMessageHandler();
                String[] commandString = {"UNIAT", "AT+CGMM\r"};
                int EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE = 1400;
                Message msg = mhandler.obtainMessage(EVENT_RIL_OEM_HOOK_CMDSTR_COMPLETE);
                objectCallManagerMethods[i].invoke(objectCallManager, commandString, msg);
                Method asyncMethods[] = msg.getClass().getMethods();
                Field fieldResult = msg.obj.getClass().getField("result");
                Field fieldException = msg.obj.getClass().getField("exception");


                s1 = fieldResult.get(msg.obj)+"";
                s2 = fieldException.get(msg.obj).toString()+"";

            }
        }

    }
    catch(Exception e){
        e.printStackTrace();
    }

But when I invoke the "makeDefaultPhone" i get the following Exception:

    11-18 18:05:37.711: W/System.err(10469): java.lang.reflect.InvocationTargetException

11-18 18:05:37.711: W/System.err(10469):    at java.lang.reflect.Method.invokeNative(Native Method)

11-18 18:05:37.711: W/System.err(10469):    at java.lang.reflect.Method.invoke(Method.java:511)

11-18 18:05:37.711: W/System.err(10469):    at famouri.flashsms.ModemHandlerThread.run(ModemHandlerThread.java:41)

11-18 18:05:37.711: W/System.err(10469): Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { act=com.android.internal.telephony.IWapPushManager }

11-18 18:05:37.711: W/System.err(10469):    at android.app.ContextImpl.bindService(ContextImpl.java:1341)
11-18 18:05:37.711: W/System.err(10469):    at android.app.ContextImpl.bindService(ContextImpl.java:1315)

11-18 18:05:37.711: W/System.err(10469):    at android.content.ContextWrapper.bindService(ContextWrapper.java:401)

11-18 18:05:37.711: W/System.err(10469):    at com.android.internal.telephony.WapPushOverSms$WapPushConnection.bindWapPushManager(WapPushOverSms.java:98)

11-18 18:05:37.711: W/ActivityManager(359): Permission Denial: Accessing service ComponentInfo{com.android.smspush/com.android.smspush.WapPushManager} from pid=10469, uid=10130 requires com.android.smspush.WAPPUSH_MANAGER_BIND

11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.WapPushOverSms.<init>(WapPushOverSms.java:137)
11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.SMSDispatcher.<init>(SMSDispatcher.java:307)

11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.gsm.GsmSMSDispatcher.<init>(GsmSMSDispatcher.java:112)
11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.gsm.GSMPhone.<init>(GSMPhone.java:189)

11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.gsm.GSMPhone.<init>(GSMPhone.java:173)
11-18 18:05:37.719: W/System.err(10469):    at com.android.internal.telephony.PhoneFactory.makeDefaultPhone(PhoneFactory.java:158)

Indeed, I want to send AT command directly from my application to GSM Modem. I dont understand what is the problem!





Aucun commentaire:

Enregistrer un commentaire