# Coding Stuff

### **A.** Data stored in Antares

<figure><img src="https://3873791589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7cujmJ5QHdJaAjH815aZ%2Fuploads%2FQIdnigZJe9Y9fj5sCuVo%2Fimage.png?alt=media&#x26;token=5da29fb9-6855-4fad-9d00-c0015b9883fe" alt=""><figcaption></figcaption></figure>

### **B.** Get the latest data from your device via Android

#### **1. AndroidManifest.xml**

In order to execute the Antares API, we need Internet access and in order to access the Internet on Android, we need to add the following lines to AndroidManifest.xml.

{% code lineNumbers="true" %}

```xml
<!--- IMPORTANT!!!!!!! --->
<!--- Tambahkan line berikut di AndroidManifest.xml Anda --->
<!--- Setelah manifest --->
<uses-permission android:name="android.permission.INTERNET" />
<!--- Sebelum application --->
```

{% endcode %}

#### **2. activity\_main.xml**

Modify **ALL** the contents of activity\_main.xml with the following content:

{% code lineNumbers="true" %}

```xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No Data"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/btnRefresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Refresh"/>

    <Button
        android:id="@+id/btnOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="On"/>


    <Button
        android:id="@+id/btnOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Off"/>

</android.widget.LinearLayout>
```

{% endcode %}

#### **3. MainActivity.java**

{% code lineNumbers="true" %}

```java
// !!!!------ IMPORTANT ---------!!!!//
// Ubah semua code setelah package nama.package.Anda dengan
// dengan Code berikut

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

import id.co.telkom.iot.AntaresHTTPAPI;
import id.co.telkom.iot.AntaresResponse;

public class MainActivity extends AppCompatActivity implements AntaresHTTPAPI.OnResponseListener{

    private Button btnRefresh;
    private Button btnOn;
    private Button btnOff;

    private TextView txtData;
    private String TAG = "ANTARES-API";
    private AntaresHTTPAPI antaresAPIHTTP;
    private String dataDevice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // --- Inisialisasi UI yang digunakan di aplikasi --- //
        btnRefresh = (Button) findViewById(R.id.btnRefresh);
        btnOff = (Button) findViewById(R.id.btnOff);
        btnOn = (Button) findViewById(R.id.btnOn);

        txtData = (TextView) findViewById(R.id.txtData);

        // --- Inisialisasi API Antares --- //
        //antaresAPIHTTP = AntaresHTTPAPI.getInstance();
        antaresAPIHTTP = new AntaresHTTPAPI();
        antaresAPIHTTP.addListener(this);

        btnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antaresAPIHTTP.getLatestDataofDevice("your-access-key","your-application-name","your-device-name");

            }
        });

        btnOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antaresAPIHTTP.storeDataofDevice(1,"your-access-key", "your-application-name", "your-device-name", "{\\\"status\\\":\\\"1\\\"}");

            }
        });

        btnOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                antaresAPIHTTP.storeDataofDevice(1,"your-access-key", "your-application-name", "your-device-name", "{\\\"status\\\":\\\"0\\\"}");
            }
        });
    }

    @Override
    public void onResponse(AntaresResponse antaresResponse) {
        // --- Cetak hasil yang didapat dari ANTARES ke System Log --- //
        //Log.d(TAG,antaresResponse.toString());
        Log.d(TAG,Integer.toString(antaresResponse.getRequestCode()));
        if(antaresResponse.getRequestCode()==0){
            try {
                JSONObject body = new JSONObject(antaresResponse.getBody());
                dataDevice = body.getJSONObject("m2m:cin").getString("con");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        txtData.setText(dataDevice);
                    }
                });
                Log.d(TAG,dataDevice);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
```

{% endcode %}

#### **4. Logcat Result**

We can filter the output log results on Android. Please fill in the section as shown below. When the button is clicked, the output results can also be seen in the image below.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/HwM9o1UabCOpIodHDRQA/image.png" alt=""><figcaption></figcaption></figure>

Congratulations! You have created your first Android APP using the Antares Library.

<figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/eTEtZIYt6T6bcOwfmwS2/image.png" alt=""><figcaption></figcaption></figure>

### C. Connecting a Smartphone to Android Studio <a href="#connect_smartphone" id="connect_smartphone"></a>

#### **1.**&#x53;et Settings on Smartphone

1. Enter the "Settings" menu on your smartphone, then click AboutPhone. Triple click on the MIUI Version / Version Number section of your smartphone until "you are developer" appears.<br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/maE3fB0WFwDFsMvPsRW5/image.png" alt=""><figcaption></figcaption></figure>
2. Enter the "Developer Options" menu in Settings and activate USB Debugging.

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/HkVLu5vNv6nXXh6fXhBd/image.png" alt=""><figcaption></figcaption></figure>
3. Connect your Smartphone USB with Laptop / PC, if there is a notification like this then click OK and your Smartphone is connected to Android Studio.\ <br>

   <figure><img src="https://content.gitbook.com/content/7cujmJ5QHdJaAjH815aZ/blobs/gsxrfkoyx4zXv7bDjGu1/image.png" alt=""><figcaption></figcaption></figure>

<figure><img src="https://antares.id/assets/img/android-library-tutorial-17.jpeg" alt=""><figcaption></figcaption></figure>
