-
-
Save dyadica/ccd400ce2968d87d97ed5113c44ec8e1 to your computer and use it in GitHub Desktop.
Android and Azure Integration via Photon Cloud and IoT Hub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android and Azure Integration via Photon Cloud and IoT Hub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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