NFCSync.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package de.tudarmstadt.informatik.hostage.sync;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import android.annotation.TargetApi;
  25. import android.app.Activity;
  26. import android.content.Intent;
  27. import android.nfc.NdefMessage;
  28. import android.nfc.NdefRecord;
  29. import android.nfc.NfcAdapter;
  30. import android.nfc.NfcAdapter.CreateNdefMessageCallback;
  31. import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
  32. import android.nfc.NfcEvent;
  33. import android.os.Build;
  34. import android.os.Bundle;
  35. import android.os.Handler;
  36. import android.os.Message;
  37. import android.os.Parcelable;
  38. import android.util.Log;
  39. import android.widget.TextView;
  40. import android.widget.Toast;
  41. import de.tudarmstadt.informatik.hostage.R;
  42. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  43. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  44. public class NFCSync extends Activity implements CreateNdefMessageCallback,
  45. OnNdefPushCompleteCallback {
  46. NfcAdapter mNfcAdapter;
  47. TextView mInfoText;
  48. private static final int MESSAGE_SENT = 1;
  49. public static Object deserialize(byte[] data) throws IOException,
  50. ClassNotFoundException {
  51. ByteArrayInputStream in = new ByteArrayInputStream(data);
  52. ObjectInputStream is = new ObjectInputStream(in);
  53. return is.readObject();
  54. }
  55. public static byte[] serialize(Object obj) throws IOException {
  56. ByteArrayOutputStream out = new ByteArrayOutputStream();
  57. ObjectOutputStream os = new ObjectOutputStream(out);
  58. os.writeObject(obj);
  59. return out.toByteArray();
  60. }
  61. /** This handler receives a message from onNdefPushComplete */
  62. private final Handler mHandler = new Handler() {
  63. @Override
  64. public void handleMessage(Message msg) {
  65. switch (msg.what) {
  66. case MESSAGE_SENT:
  67. Toast.makeText(getApplicationContext(), "Message sent!",
  68. Toast.LENGTH_LONG).show();
  69. Log.i("NFC", "Message sent!");
  70. break;
  71. }
  72. }
  73. };
  74. /**
  75. * Implementation for the CreateNdefMessageCallback interface
  76. */
  77. @Override
  78. public NdefMessage createNdefMessage(NfcEvent event) {
  79. // Get Networkdata
  80. UglyDbHelper dbh = new UglyDbHelper(this);
  81. ArrayList<HashMap<String, Object>> localNetworkInformation = dbh
  82. .getNetworkInformation();
  83. Log.i("NFC", "Creating Message");
  84. NdefMessage msg = null;
  85. try {
  86. msg = new NdefMessage(NdefRecord.createMime(
  87. "application/de.tudarmstadt.informatik.hostage",
  88. serialize(localNetworkInformation))
  89. /**
  90. * The Android Application Record (AAR) is commented out. When a
  91. * device receives a push with an AAR in it, the application
  92. * specified in the AAR is guaranteed to run. The AAR overrides the
  93. * tag dispatch system. You can add it back in to guarantee that
  94. * this activity starts when receiving a beamed message. For now,
  95. * this code uses the tag dispatch system.
  96. */
  97. // ,NdefRecord.createApplicationRecord("com.example.android.beam")
  98. );
  99. } catch (IOException e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. return msg;
  104. }
  105. @Override
  106. public void onCreate(Bundle savedInstanceState) {
  107. super.onCreate(savedInstanceState);
  108. setContentView(R.layout.activity_nfc);
  109. mInfoText = (TextView) findViewById(R.id.textView);
  110. // Check for available NFC Adapter
  111. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  112. if (mNfcAdapter == null) {
  113. mInfoText = (TextView) findViewById(R.id.textView);
  114. mInfoText.setText("NFC is not available on this device.");
  115. } else {
  116. // Register callback to set NDEF message
  117. mNfcAdapter.setNdefPushMessageCallback(this, this);
  118. // Register callback to listen for message-sent success
  119. mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
  120. }
  121. }
  122. /**
  123. * Implementation for the OnNdefPushCompleteCallback interface
  124. */
  125. @Override
  126. public void onNdefPushComplete(NfcEvent arg0) {
  127. // A handler is needed to send messages to the activity when this
  128. // callback occurs, because it happens from a binder thread
  129. mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
  130. }
  131. @Override
  132. public void onNewIntent(Intent intent) {
  133. // onResume gets called after this to handle the intent
  134. setIntent(intent);
  135. }
  136. // HELPER
  137. @Override
  138. public void onResume() {
  139. super.onResume();
  140. // Check to see that the Activity started due to an Android Beam
  141. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  142. processIntent(getIntent());
  143. }
  144. }
  145. /**
  146. * Parses the NDEF Message from the intent and prints to the TextView
  147. */
  148. void processIntent(Intent intent) {
  149. Parcelable[] rawMsgs = intent
  150. .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
  151. // only one message sent during the beam
  152. NdefMessage msg = (NdefMessage) rawMsgs[0];
  153. // record 0 contains the MIME type, record 1 is the AAR, if present
  154. Object object;
  155. Log.i("NFC", "Getting Message!");
  156. try {
  157. object = deserialize(msg.getRecords()[0].getPayload());
  158. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) object;
  159. UglyDbHelper dbh = new UglyDbHelper(this);
  160. dbh.updateNetworkInformation(remoteNetworkInformation);
  161. } catch (ClassNotFoundException e) {
  162. // TODO Auto-generated catch block
  163. e.printStackTrace();
  164. } catch (IOException e) {
  165. // TODO Auto-generated catch block
  166. e.printStackTrace();
  167. }
  168. }
  169. }