Kotlin new keyword does not exist

I cannot figure out how to subscribe to events from the Android SDK. I have the login implemented already. The problem comes when I’m trying to adapt the subscribe snippets to Kotlin. There is no ‘new’ keyword in Kotlin to create a ParticleEventHandler, and it won’t accept anything of any other type.

The error is

Interface ParticleEventHandler does not have constructors

This is the testing function I’m working on:

fun subscribe() = runBlocking { // this: CoroutineScope
        var subscribeSuccess = GlobalScope.async {
            // launch new coroutine in background  and continue (Non-Blocking)
            try {
                ParticleCloudSDK.getCloud().subscribeToMyDevicesEvents(
                    null,  // the first argument, "eventNamePrefix", is optional
                    ParticleEventHandler() {
                        public void onEvent(String eventName, ParticleEvent event) {
                            //Log.i("some tag", "Received event with payload: " + event.dataPayload);
                        }
                        }
                true
            } catch (e: ParticleCloudException) {

                e.printStackTrace()
                false
            }
        }
        if (subscribeSuccess.await()) {  // Wait for deferred var, continue if subscribed
            Toast.makeText(applicationContext, "Subscribed!", Toast.LENGTH_LONG).show()
            // Anko version  Hand off to next activity with status

        } else Toast.makeText(applicationContext, "Could not subscribe", Toast.LENGTH_LONG).show()
    }

For somebody with the same problem, I ended up making a java class and Im calling it from my kotlin main. I’ll keep updating and reply with my code if it works.

1 Like

This worked! In your kotlin project, create a java class where your mainActivity is. Be sure to create a constructor if android studio doesn’t make one for you. Put your java code in there from the SDK. For example:

public class ParticleUtil {

    public long subscriptionId;

    public ParticleUtil(){

    }

    public void subscribe() throws IOException {
        
        subscriptionId = ParticleCloudSDK.getCloud().subscribeToMyDevicesEvents(
                "status",  // the first argument, "eventNamePrefix", is optional
                new ParticleEventHandler() {
                    public void onEvent(String eventName, ParticleEvent event) {
                        Log.i("STATUS", "Received event with payload: " + event.dataPayload);
                    }

                    public void onEventError(Exception e) {
                        Log.e("some tag", "Event error: ", e);
                    }
                });
    }

}

Now in your kotlin code, spool up a new task like this:

fun subscribe() = runBlocking { // this: CoroutineScope
        val subscribeSuccess = GlobalScope.async {
            // launch new coroutine in background  and continue (Non-Blocking)
            try {
                val util = ParticleUtil()
                util.subscribe()
                subscriptionId = util.subscriptionId
                true
            } catch (e: ParticleCloudException) {

                e.printStackTrace()
                false
            }
        }
        if (subscribeSuccess.await()) {  // Wait for deferred var, continue if subscribed
            Toast.makeText(applicationContext, "Subscribed!", Toast.LENGTH_LONG).show()
            // Anko version  Hand off to next activity with status

        } else Toast.makeText(applicationContext, "Could not subscribe", Toast.LENGTH_LONG).show()
    }

You will need to create an object, in my case I named the java class ParticleUtil.java. Create it with the constructor you made in the java class,
val util = ParticleUtil()
call your method and you’re all set. I have my app receiving the labelled status events.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.