Skip to main content
1-Visitor
June 14, 2017
Solved

thingworx android error

  • June 14, 2017
  • 10 replies
  • 4581 views

i am trying to run the sample code provided in the thingworx android sdk

I have a problem in every sample project

it says cant resolve the constructor

thing = new VirtualThing(thingName, "A basic virtual thing", client);

because the client is not initiialized

similarly

client.isConnected()  is also showing error

i dont know what 'client' is  because the variable is also not declared anywhere.

kindly tell the solution

Best answer by supandey

Hi Dibya, it seems to me that your project is not correctly setup in studio, here's the blog you could use to correct it Exploring ThingWorx Android SDK together with web based Revision Control System and secured connection to ThingWorx Serv…

10 replies

supandey5-Regular MemberAnswer
5-Regular Member
June 14, 2017

Hi Dibya, it seems to me that your project is not correctly setup in studio, here's the blog you could use to correct it Exploring ThingWorx Android SDK together with web based Revision Control System and secured connection to ThingWorx Serv…

5-Regular Member
June 14, 2017

Hi dibya ranjan mishra​,

Have you added the ThingWorx Android SDK jars to your project?

dmishra1-VisitorAuthor
1-Visitor
June 14, 2017

yes i have added. I just had to import the sample project in the android studio

Could you just run the exampleconnection project and check if it is working or not

5-Regular Member
June 14, 2017

From where have you downloaded this project? Could you share the link?

dmishra1-VisitorAuthor
1-Visitor
June 14, 2017

package com.thingworx.ptc.shell.utilities;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.os.Handler;

import android.os.HandlerThread;

import android.os.IBinder;

import android.os.Looper;

import android.os.Message;

import android.os.Process;

import android.support.annotation.Nullable;

import android.util.Log;

<---------this is the error I am saying about-------------------------------->

import com.thingworx.communications.client.AndroidConnectedThingClient;

-------------------------------------------------------------------------------------------------------------------------------

import com.thingworx.communications.client.things.VirtualThing;

public class ThingworxProcessService extends Service {

    public final String TAG = ThingworxProcessService.class.getName();

    private Looper mServiceLooper;

    private ServiceHandler serviceHandler;

    private AndroidConnectedThingClient client;

    private IntentServiceReceiver serviceReceiver;

    @Override

    public void onCreate() {

        serviceReceiver = new IntentServiceReceiver();

        HandlerThread thread = new HandlerThread("ThingworxProcessService", Process.THREAD_PRIORITY_BACKGROUND);

        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler

        mServiceLooper = thread.getLooper();

        serviceHandler = new ServiceHandler(mServiceLooper);

    }

    @Override

    public int onStartCommand(Intent intent, int flags, int startId) {

        // For each start request, send a message to start a job and deliver the

        // start ID so we know which request we're stopping when we finish the job

        Message msg = serviceHandler.obtainMessage();

        msg.setData(intent.getExtras());

        msg.arg1 = startId;

        serviceHandler.sendMessage(msg);

        return START_STICKY;

    }

    @Nullable

    @Override

    public IBinder onBind(Intent intent) {

        return null;

    }

    @Override

    public void onDestroy() {

        unregisterReceiver(serviceReceiver);

        super.onDestroy();

    }

    // Handler that receives messages from the thread

    private final class ServiceHandler extends Handler {

        public ServiceHandler(Looper looper) {

            super(looper);

        }

        @Override

        public void handleMessage(Message msg) {

            Log.i(TAG, "handleMessage Start");

            try {

                IntentFilter intentFilter = new IntentFilter();

                intentFilter.addAction(Constants.ACTION_SHUTDOWN);

                registerReceiver(serviceReceiver, intentFilter);

                Bundle bundle = msg.getData();

                long pollingRate = bundle.getLong(Constants.POLLING_RATE_EXTRA, 1000l);

                boolean lastConnectionState = bundle.getBoolean(Constants.LAST_CONNECT_STATE_EXTRA);

                String stateObserver = bundle.getString(Constants.CONNECTION_OBSERVER);

                client = ThingworxService.client;

                boolean isConnected;

                while (client == null || !client.isShutdown()) {

                    if (client != null && client.isConnected()) {

                        isConnected = true;

                        for (VirtualThing thing : client.getThings().values()) {

                            try {

                                Thread.sleep(15000);

                                Log.v(TAG, "Scanning device");

                                thing.processScanRequest();

                            } catch (Exception eProcessing) {

                                Log.e(TAG, "Error Processing Scan Request for [" + thing.getName() + "] : " + eProcessing.getMessage());

                            }

                        }

                        Intent scanned = new Intent();

                        scanned.setAction(Constants.ACTION_SCAN_COMPLETE);

                        sendBroadcast(scanned);

                    } else {

                        isConnected = false;

                        Thread.sleep(15000);

                    }

                    if(isConnected != lastConnectionState){

                        if(stateObserver != null) {

                            Intent stateChange = new Intent();

                            stateChange.setAction(Constants.ACTION_STATE_CHANGED);

                            stateChange.putExtra(Constants.CONNECTION_OBSERVER, stateObserver);

                            sendBroadcast(stateChange);

                            lastConnectionState = isConnected;

                        }

                    }

                    Thread.sleep(pollingRate);

                }

            } catch (InterruptedException e) {

                Log.e(TAG, "Polling thread exiting." + e.getMessage());

            }

            // Stop the service using the startId, so that we don't stop

            // the service in the middle of handling another job

            stopSelf(msg.arg1);

            Log.i(TAG, "handleMessage End");

        }

    }

    public class IntentServiceReceiver extends BroadcastReceiver {

        @Override

        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if(action.equals(Constants.ACTION_SHUTDOWN)){

                if(client != null && client.isConnected())

                    try {

                        client.shutdown();

                    } catch (Exception e) {

                        Log.e(TAG, "Error Shutting Down Client", e);

                    }

            }

        }

    }

}

dmishra1-VisitorAuthor
1-Visitor
June 14, 2017

I apologize for my mistake

it was showing error in the beginning

but the real error was in gradle dependancies

then this thing works fine

I am extremely pleased with the way you have responded to my problem

Thank you so much.

Hope to get this support though out

5-Regular Member
June 14, 2017

Yeah this is why i was wondering if there was anything during the build process. Anyhow glad it has worked out for you.