Skip to content

Instantly share code, notes, and snippets.

@dyadica

dyadica/About Secret

Created March 13, 2017 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyadica/ccd400ce2968d87d97ed5113c44ec8e1 to your computer and use it in GitHub Desktop.
Save dyadica/ccd400ce2968d87d97ed5113c44ec8e1 to your computer and use it in GitHub Desktop.
Android and Azure Integration via Photon Cloud and IoT Hub
Android and Azure Integration via Photon Cloud and IoT Hub
package uk.co.dyadica.photonandroid;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.cloud.ParticleEventVisibility;
import io.particle.android.sdk.utils.Async;
public class PhotonActivity extends AppCompatActivity
{
// Device ID of a particle.io device. For talking to a
// specific device.
private static final String deviceID = "<< Your Device ID >>";
// Username and password of the particle.io account
private static final String username = "<< Your Username >>";
private static final String password = "<< Your Password >>";
// The name of the event to trigger
private static final String cloudEvent = "<< Your Event-name >>";
// Flag to show connection status
public boolean isConnected = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photon);
PerformParticleConnection();
// The Event Call Button
findViewById(R.id.eButton).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
TriggerParticleEvent();
}
});
// The Device Call Button
findViewById(R.id.dButton).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
GetParticleDevice();
}
});
}
// Method used to trigger connection to the
// particle.io cloud
private void PerformParticleConnection()
{
Async.executeAsync(ParticleCloud.get(PhotonActivity.this), new Async.ApiWork<ParticleCloud, Object>()
{
@Override
public Object callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException
{
// Perform Log on
ParticleCloud.logIn(username, password);
return true;
}
@Override
public void onSuccess(@NonNull Object i)
{
// A little debug
Log.i("ParticleCloud", "Connected");
// Update the connection status
isConnected = true;
}
@Override
public void onFailure(@NonNull ParticleCloudException e)
{
// A little debug
e.printStackTrace();
// Update the connection status
isConnected = false;
}
});
}
// Method used to trigger an event on the
// particle.io cloud
private void TriggerParticleEvent()
{
if(isConnected == false)
return;
Async.executeAsync(ParticleCloud.get(PhotonActivity.this), new Async.ApiWork<ParticleCloud, Object>()
{
@Override
public Object callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException
{
try
{
String data = "{\"x\": 100, \"y\": 100, \"z\": 100}";
ParticleCloudSDK.getCloud().publishEvent(cloudEvent, data,
ParticleEventVisibility.PUBLIC, 60);
}
catch (ParticleCloudException ex)
{
Log.i("CloudException", "Failed to publish event: " + ex.getMessage());
}
return true;
}
@Override
public void onSuccess(@NonNull Object i) {
Log.i("EventPublished:", cloudEvent);
}
@Override
public void onFailure(@NonNull ParticleCloudException e) {
e.printStackTrace();
}
});
}
// Method used to get device data from a named (id)
// particle.io device
private void GetParticleDevice()
{
if(isConnected == false)
return;
Async.executeAsync(ParticleCloud.get(this), new Async.ApiWork<ParticleCloud, ParticleDevice>()
{
@Override
public ParticleDevice callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException
{
// Get all of the logged in user devices
ParticleCloud.getDevices();
// Find the named (ID) device
ParticleDevice mDevice = ParticleCloud.getDevice(deviceID);
// Return the connected device
return ParticleCloud.getDevice(mDevice.getName());
}
@Override
public void onSuccess(@NonNull ParticleDevice particleDevice)
{
// Output the name of the device
Log.i("DeviceName:", particleDevice.getName());
}
@Override
public void onFailure(@NonNull ParticleCloudException e)
{
// Trace the error
e.printStackTrace();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment