NFCSync.java 5.3 KB

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