NFCSync.java 6.4 KB

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