0
votes

I am trying to add the following block of code to an existing class

Class nfcExtrasClazz = Class.forName("com.android.nfc_extras.NfcAdapterExtras");
Method getMethod = nfcExtrasClazz .getMethod("get", Class.forName("android.nfc.NfcAdapter"));
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
Object nfcExtras = getMethod .invoke(nfcExtrasClazz, adapter);
Method getEEMethod = nfcExtras.getClass().getMethod("getEmbeddedExecutionEnvironment", (Class[]) null);
Object ee = getEEMethod.invoke(nfcExtras , (Object[]) null);
Class eeClazz = se.getClass();
Method openMethod = eeClazz.getMethod("open", (Class[]) null);
Method transceiveMethod = ee.getClass().getMethod("transceive",new Class[] { byte[].class });
Method closeMethod = eeClazz.getMethod("close", (Class[]) null);
openMethod.invoke(se, (Object[]) null);
Object response = transceiveMethod.invoke(se, command);
closeMethod.invoke(se, (Object[]) null);

I realized that I need to put this code in another thread. So I got a hold of an existing local service implementation, and tried to add the above code to it. But within the service implementation code I get the following error: "The method getDefaultAdapter(Context) in the type NfcAdapter is not applicable for the arguments (BackgroundService.ServiceWorker)" There are two classes in the package; one is the main: public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private int counter = 1; private NfcAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }

        public void doClick(View view) {
            switch(view.getId()) {
            case R.id.startBtn:
                Log.v(TAG, "Starting service... counter = " + counter);
                adapter = NfcAdapter.getDefaultAdapter(this);
                Intent intent = new Intent(MainActivity.this,
                    BackgroundService.class);
            intent.putExtra("counter", counter++);
            startService(intent);
            break;
        case R.id.stopBtn:
            stopService();
        }
    }

    private void stopService() {
        Log.v(TAG, "Stopping service...");
        if(stopService(new Intent(MainActivity.this,
                    BackgroundService.class)))
            Log.v(TAG, "stopService was successful");
        else
            Log.v(TAG, "stopService was unsuccessful");
    }

    @Override
    public void onDestroy()
    {
        stopService();
        super.onDestroy();
    }
}

And here is the class that implements the local service: public class BackgroundService extends Service { private static final String TAG = "BackgroundService"; private NotificationManager notificationMgr; private NfcAdapter adapter; private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");

    @Override
    public void onCreate() {
        super.onCreate();

        Log.v(TAG, "in onCreate()");
        notificationMgr =(NotificationManager)getSystemService(
               NOTIFICATION_SERVICE);
        displayNotificationMessage("Background Service is running");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        int counter = intent.getExtras().getInt("counter");
        Log.v(TAG, "in onStartCommand(), counter = " + counter +
                ", startId = " + startId);

        new Thread(myThreads, new ServiceWorker(counter), "BackgroundService")
            .start();

        return START_NOT_STICKY;
    }

    class ServiceWorker implements Runnable
    {
        private int counter = -1;
        public ServiceWorker(int counter) {
            this.counter = counter;
        }

        public void run() {
            final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
            // do background processing here...
            try {
                Log.v(TAG2, "sleeping for 10 seconds. counter = " + counter);
                Thread.sleep(10000);
                // I added the following code; I temprarily commented all of lines out
                //NfcAdapterExtras adapterExtras = NfcAdapterExtras.get(NfcAdapter.getDefaultAdapter(this));
                //NfcExecutionEnvironment nfceEe = adapterExtras.getEmbeddedExecutionEnvironment();
                Class nfcExtrasClazz = null;
                try {
                    nfcExtrasClazz = Class.forName("com.android.nfc_extras.NfcAdapterExtras");
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method getMethod = null;
                try {
                    getMethod = nfcExtrasClazz .getMethod("get", Class.forName("android.nfc.NfcAdapter"));
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                adapter = NfcAdapter.getDefaultAdapter(this);
                Object nfcExtras = null;
                try {
                    nfcExtras = getMethod .invoke(nfcExtrasClazz, adapter);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Method getEEMethod = null;
                try {
                    getEEMethod = nfcExtras.getClass().getMethod("getEmbeddedExecutionEnvironment", 
                                      (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Object ee = null;
                try {
                    ee = getEEMethod.invoke(nfcExtras , (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Class eeClazz = ee.getClass();
                Method openMethod = null;
                try {
                    openMethod = eeClazz.getMethod("open", (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method transceiveMethod = null;
                try {
                    transceiveMethod = ee.getClass().getMethod("transceive",
                                        new Class[] { byte[].class });
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Method closeMethod = null;
                try {
                    closeMethod = eeClazz.getMethod("close", (Class[]) null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    openMethod.invoke(ee, (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String command = "00000000";
                try {
                    Object myresponse = transceiveMethod.invoke(ee, command );
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    closeMethod.invoke(ee, (Object[]) null);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.v(TAG2, "... waking up");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Log.v(TAG2, "... sleep interrupted");
            }
        }
    }

    @Override
    public void onDestroy()
    {
        Log.v(TAG, "in onDestroy(). Interrupting threads and cancelling notifications");
        myThreads.interrupt();
        notificationMgr.cancelAll();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(TAG, "in onBind()");
        return null;
    }

    private void displayNotificationMessage(String message)
    {
        Notification notification = new Notification(R.drawable.emo_im_winking, 
                message, System.currentTimeMillis());

        notification.flags = Notification.FLAG_NO_CLEAR;

        PendingIntent contentIntent = 
                PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        notification.setLatestEventInfo(this, TAG, message, contentIntent);

        notificationMgr.notify(0, notification);
    }
}

I think I am not adding the nfcadapter instance from my main activity to the service perhaps? Could someone please have a look?

2
What is this object se in openMethod.invoke(se, (Object[]) null); You probably missing a line that initialised se.Barışcan Kayaoğlu
@BarışcanKayaoğlu; Thanks for the quick reply. It stands for Secure Element. I don't understand reflection well enough to know what Nik is doing. I wish I did.user3326293
Well since it's a variable name and not initialised, it doesn't actually need to mean anything. Check my answer.Barışcan Kayaoğlu

2 Answers

1
votes

Looking at the link, Replace your se with ee, it's a typo.

0
votes

se is never created with this code, so it can not be resolved to an object.

The first time we see se is in the call

Class eeClazz = se.getClass();

which is presumably where you get the error.