cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

thingworx android error

dmishra
1-Visitor

thingworx android error

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

ACCEPTED SOLUTION

Accepted Solutions
supandey
19-Tanzanite
(To:dmishra)

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…

View solution in original post

10 REPLIES 10
supandey
19-Tanzanite
(To:dmishra)

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…

ankigupta
12-Amethyst
(To:dmishra)

Hi dibya ranjan mishra​,

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

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

supandey
19-Tanzanite
(To:dmishra)

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

supandey
19-Tanzanite
(To:supandey)

BTW are you executing this project on Win 10?

sir

actually i corrected that error

it was importing some AndroidConnectedThingClient

but I guess thingworx has changed it to ConnectedThingClient

because i extracted all the 2 jar files I could not find AndroidConnectedThingClient.class

Its better if you could see the sample codes and change it to ConncetedThingClient

https://developer.thingworx.com/resources/guides/thingworx-android-sdk-setup-guide

i got the android sdk from here and then trying to run the ExampleConnection project present in the samples folder of android sdk

dmishra
1-Visitor
(To:dmishra)

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);

                    }

            }

        }

    }

}

dmishra
1-Visitor
(To:dmishra)

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

supandey
19-Tanzanite
(To:dmishra)

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

Announcements


Top Tags