Andriod Studio Particle Error

I have been following the docs for making an app in andriod studio and they state to copy a block of code into the onCreate method, hence my code looks as follows:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ParticleDeviceSetupLibrary.init(this);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ParticleDeviceSetupLibrary.startDeviceSetup(MainActivity.this,
                        MainActivity.class);
                    }
                });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1
                Async.executeAsync(ParticleCloudSDK.getCloud(),
                        new Async.ApiProcedure<ParticleCloud>() {
                            @Override
                            public Void callApi(ParticleCloud particleCloud)
                                    throws ParticleCloudException, IOException {
                                //2
                                List<ParticleDevice> devices = particleCloud.getDevices();
                                //3
                                for (ParticleDevice particleDevice : devices) {
                                    if ("myDevice".equals(particleDevice.getName())) {
                                        try {
                                            //4
                                            int result = particleDevice
                                                    .callFunction("digitalwrite",
                                                            Arrays.asList("D7", "HIGH"));
                                            //5
                                            if (result == 1) {
                                                Toast.makeText(MainActivity.this,
                                                        "Called a function on myDevice",
                                                        Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (ParticleDevice
                                                .FunctionDoesNotExistException e) {
                                            //e.printStackTrace() to see whole stack trace
                                        }
                                    }
                                }
                                return null;
                            }

                            @Override
                            public void onFailure(ParticleCloudException exception) {
                                //e.printStackTrace() to see whole stack trace
                            }
                        });
            }
        });
    }}

However although the first button to set up the photon works, the 2nd button does nothing when its clicked. Can anyone tell me if I misinterpreted the docs or if something has changed due to new version of Andriod Studio being released?

Is the name of your device really “myDevice”?
You may consider addding an else branch to your

  if ("myDevice".equals(particleDevice.getName())) {
    ...
  }

in order to catch the case when myDevice is not found.

BTW, I’m not sure it makes a lot of sense to always iterate over all devices when you click the button. Wouldn’t it be better to first select the target device once and then only act on that one selected device?
Just for style reasons I’d also turn the logic of the name-compare round like this

  if (particleDevice.getName().equals("myDevice")) {

yeah I was following the docs exactly. The used some debug statements and found that everything is working except inside the try {.... } loop.

The errors spitting out are:

W/System.err: io.particle.android.sdk.cloud.ParticleDevice$FunctionDoesNotExistException: Function digitalWrite does not exist on this device
at io.particle.android.sdk.cloud.ParticleDevice.callFunction(ParticleDevice.java:379)
at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:111)

and a few more. I think the crucial part of the errors is function digitalWrite doesnt exist?

Are you running Tinker app on the device?
You are calling digitalwrite in code, but the error message complains about digitalWrite

Upper/lower case does matter here too.

in my code I changed it to digitalWrite after (as im pretty sure the functions digitalWrite not digitalwrite, but either case I’ve tried both) I uploaded this but that hasn’t worked. I’m building my own app in android studio. I have followed the docs exactly https://docs.particle.io/guide/how-to-build-a-product/mobile-app/

so I’m not sure why it is spitting out this error if it’s what they’ve said to do.

Can you check how the function is exposed in console.particle.io/devices ?

If you are using Tinker then it is in fact digitalwrite
https://build.particle.io/examples/56214d636666d9ece3000006

If you are not running Tinker on your device, you need to implement and expose a Particle.function() and call it by the name it is exposed/registered under.

changed it to digitalwrite after flashing the app with tinker, it worked. However the toast.make text is causing a fatal error causing the app to crash, but with that commented out it runs fine. Ill try and solve the toast error myself.

Thanks for your help :slight_smile: @ScruffR

2 Likes