Using APIs for Android SDK(photon)

Can anyone give some help on how beginners in java can use APIs of Android SDK given in documentation of Photon https://docs.particle.io/reference/android/

You should read a book or some tutorials on setting up Android Studio and writing some sample apps (not using the device setup SDK) first. Then you should be able to create a new app that uses the Device Setup SDK fairly easily. The Device Setup SDK also includes the Particle API library, so you can make those calls as well.

Simple Auth - Android app test

Tested with Android Studio 3.0.1, January 2018.

  • Start a new Android Studio Project
  • Targeted Phone and Tablet: API 15 - Android 4.0.3 (Ice Cream Sandwich)
  • Empty Activity

You can target a newer API version than 15, but you can’t target an older version because that’s the minimum supported by the Device SDK.

I did this test with Particle Device SDK 0.4.9. I was unable to get version 0.5.0 working as my test phone has a maximum SDK of 23 and it seems like 0.5.0 would only build with SDK 26.

Edit build.gradle in app/src, add to the dependencies section:

    compile 'io.particle:devicesetup:0.4.9'
    androidTestCompile 'io.particle:devicesetup:0.4.9'

Also update the minSdkVersion to 15, the minimum required by the DeviceSetup SDK, if necessary.

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.rickk.tempmon10"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
  • I also had to remove a dependency from build.gradle, as it conflicted with another dependency and I couldn’t get it to resolve using the recommended method of adding meta-data to AndroidManifest.xml.
    implementation 'com.android.support:appcompat-v7:26.1.0'
  • It should be possible to build the empty project now. It’s probably a good idea to do that to make sure everything is good so far before adding code.

  • Edit app > java > tempmon10 > MainActivity and add an onSetupDevice handler:

    /** Called when the user touches the SetupDevice button */
    public void onSetupDevice(View view) {
        Log.d("MainActivity", "onSetupDevice called");
    }
  • Open app > res > layout > activity_main.xml
  • Drag a Button to the view
  • Set the title to Setup Device
  • Set the onClick to onSetupDevice

Before going much further, it’s probably a good idea to run the program thus far. Clicking on the Setup Device button should result in something like this in the Android Monitor:

05-06 09:42:54.011 12324-12324/com.rickk.tempmon10 D/MainActivity: onSetupDevice called

Now on to the actual setup.

  • Add the device setup library initialization to app > java > tempmon10 > MainActivity onCreate method.
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParticleDeviceSetupLibrary.init(this.getApplicationContext());
        
        setContentView(R.layout.activity_main);
    }
  • Add the entries to app > manifests > AndroidManifest.xml to the application tag. This is only necessary for Particle Device Setup SDK 0.4.9 and earlier; a different method is used in 0.5.0.
<!-- All of the following are from the device setup lib, and must be present in your app's
manifest or you will not go to space today. -->
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.DiscoverDeviceActivity"
            android:label="@string/title_activity_discover_device"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.SelectNetworkActivity"
            android:label="@string/title_activity_select_network"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.PasswordEntryActivity"
            android:label="@string/title_activity_password_entry"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateVisible" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.ConnectingActivity"
            android:label="@string/title_activity_connecting"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.SuccessActivity"
            android:label="@string/title_activity_success"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden" />
        <activity
            android:name="io.particle.android.sdk.utils.ui.WebViewActivity"
            android:label="@string/title_activity_web_view"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.GetReadyActivity"
            android:label="@string/title_activity_get_ready"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar" />
        <activity
            android:name="io.particle.android.sdk.devicesetup.ui.ManualNetworkEntryActivity"
            android:label="@string/title_activity_manual_network_entry"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateVisible" />
        <activity
            android:name="io.particle.android.sdk.accountsetup.CreateAccountActivity"
            android:label="@string/title_activity_create_account"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateHidden" />
        <activity
            android:name="io.particle.android.sdk.accountsetup.LoginActivity"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateHidden" />
        <activity
            android:name="io.particle.android.sdk.accountsetup.PasswordResetActivity"
            android:label="@string/title_activity_password_reset"
            android:screenOrientation="portrait"
            android:theme="@style/ParticleSetupTheme.NoActionBar"
            android:windowSoftInputMode="adjustResize|stateVisible" />
  • Add the code to actually handle device setup when you click the button.
    public void onSetupDevice(View view) {
        Log.d("MainActivity", "onSetupDevice called");
        ParticleDeviceSetupLibrary.startDeviceSetup(this, MainActivity.class);
    }
2 Likes