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.
<!--- IMPORTANT!!!!!!! ---><!--- Tambahkan line berikut di AndroidManifest.xml Anda ---><!--- Setelah manifest ---><uses-permission android:name="android.permission.INTERNET" /><!--- Sebelum application --->
2. activity_main.xml
Modify ALL the contents of activity_main.xml with the following content:
// !!!!------ IMPORTANT ---------!!!!//// Ubah semua code setelah package nama.package.Anda dengan// dengan Code berikutimportandroid.os.Bundle;importandroid.util.Log;importandroid.view.View;importandroid.widget.Button;importandroid.widget.TextView;importorg.json.JSONException;importorg.json.JSONObject;importandroidx.appcompat.app.AppCompatActivity;importid.co.telkom.iot.AntaresHTTPAPI;importid.co.telkom.iot.AntaresResponse;publicclassMainActivityextendsAppCompatActivityimplementsAntaresHTTPAPI.OnResponseListener{privateButton btnRefresh;privateButton btnOn;privateButton btnOff;privateTextView txtData;privateString TAG ="ANTARES-API";privateAntaresHTTPAPI antaresAPIHTTP;privateString dataDevice; @OverrideprotectedvoidonCreate(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 =newAntaresHTTPAPI();antaresAPIHTTP.addListener(this);btnRefresh.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View v) {antaresAPIHTTP.getLatestDataofDevice("your-access-key","your-application-name","your-device-name"); } });btnOn.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View v) { antaresAPIHTTP.storeDataofDevice(1,"your-access-key", "your-application-name", "your-device-name", "{\\\"status\\\":\\\"1\\\"}");
} });btnOff.setOnClickListener(new View.OnClickListener() { @OverridepublicvoidonClick(View v) { antaresAPIHTTP.storeDataofDevice(1,"your-access-key", "your-application-name", "your-device-name", "{\\\"status\\\":\\\"0\\\"}");
} }); } @OverridepublicvoidonResponse(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 =newJSONObject(antaresResponse.getBody()); dataDevice =body.getJSONObject("m2m:cin").getString("con");runOnUiThread(new Runnable() { @Overridepublicvoid run() {txtData.setText(dataDevice); } });Log.d(TAG,dataDevice); } catch (JSONException e) {e.printStackTrace(); } } }}
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.
Congratulations! You have created your first Android APP using the Antares Library.
C. Connecting a Smartphone to Android Studio
1.Set Settings on Smartphone
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.
Enter the "Developer Options" menu in Settings and activate USB Debugging.
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.