Skip to content

Commit

Permalink
Added sending of nfc data to the service
Browse files Browse the repository at this point in the history
  • Loading branch information
michelefi committed Nov 27, 2013
1 parent 35a56d4 commit 311669f
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions src/com/google/developers/gdgfirenze/android/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
}
}

}

0 comments on commit 311669f

Please sign in to comment.