From 311669fa6eaad4a806e355d4c502116bc053f8cd Mon Sep 17 00:00:00 2001 From: Michele Ficarra Date: Wed, 27 Nov 2013 19:18:43 +0100 Subject: [PATCH] Added sending of nfc data to the service --- .../gdgfirenze/android/MainActivity.java | 50 +++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/com/google/developers/gdgfirenze/android/MainActivity.java b/src/com/google/developers/gdgfirenze/android/MainActivity.java index 936a2d3..b98d5d8 100644 --- a/src/com/google/developers/gdgfirenze/android/MainActivity.java +++ b/src/com/google/developers/gdgfirenze/android/MainActivity.java @@ -1,11 +1,22 @@ package com.google.developers.gdgfirenze.android; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.json.JSONObject; + import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.content.Intent; +import android.content.SharedPreferences; import android.nfc.NfcAdapter; +import android.nfc.Tag; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.provider.Settings.Secure; +import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -19,6 +30,9 @@ public class MainActivity extends Activity implements ActionBar.OnNavigationListener { private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; + private static final DateFormat dateFormat = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss.SSS"); + private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -48,9 +62,7 @@ protected void onCreate(Bundle savedInstanceState) { public void onResume() { super.onResume(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { - Toast toast = Toast.makeText(this, "NFC Tag Read", - Toast.LENGTH_SHORT); - toast.show(); + handleNfcTagDiscovert(); } } @@ -164,5 +176,37 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, return rootView; } } + + private void handleNfcTagDiscovert() { + try { + Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); + SharedPreferences p = PreferenceManager + .getDefaultSharedPreferences(this); + StringBuilder id = new StringBuilder(); + byte[] data = tag.getId(); + for (int i = 0; i < data.length; i++) { + id.append(String.format("%02x", data[i])); + if (i < data.length - 1) { + id.append(":"); + } + } + JSONObject jsonSamplePacket = new JSONObject(); + JSONObject obj = new JSONObject(); + jsonSamplePacket.put("sample", obj); + obj.put("device_id", p.getString("device_id", + Secure.getString(getContentResolver(), Secure.ANDROID_ID))); + obj.put("time", dateFormat.format(new Date())); + obj.put("nfc", id); + Intent intent = new Intent(this, DataSenderService.class); + intent.putExtra(DataSenderService.INTENT_EXTRA, + jsonSamplePacket.toString()); + startService(intent); + Toast toast = Toast.makeText(this, "NFC Tag " + id, + Toast.LENGTH_SHORT); + toast.show(); + } catch (Exception e) { + Log.e(TAG, "Error Writing tag...", e); + } + } }