NFCSync.java 6.5 KB

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