Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 package com.github.rosjava.android_remocons.common_tools.nfc;
00036
00037 import android.app.Activity;
00038 import android.content.Intent;
00039 import android.os.Bundle;
00040 import android.util.Log;
00041 import android.widget.TextView;
00042
00043 import com.github.robotics_in_concert.rocon_rosjava_core.rosjava_utils.ByteArrays;
00044 import com.github.rosjava.android_remocons.common_tools.R;
00045
00046 import java.util.HashMap;
00047
00048 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_MASTER_HOST_FIELD_LENGTH;
00049 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_PASSWORD_FIELD_LENGTH;
00050 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_PAYLOAD_LENGTH;
00051 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_SSID_FIELD_LENGTH;
00052
00056 public class NfcReaderActivity extends Activity {
00057 public static boolean enabled = true;
00058
00059 private NfcManager nfcManager;
00060 private TextView textView;
00061
00062 private HashMap<String, Object> data;
00063
00064 @Override
00065 public void onCreate(Bundle savedState) {
00066 super.onCreate(savedState);
00067
00068 try{
00069 setContentView(R.layout.nfc_tag_scan);
00070 textView = (TextView) findViewById(R.id.text);
00071 textView.setText("Scan a NFC tag");
00072 nfcManager = new NfcManager(this);
00073 }
00074 catch (Exception e) {
00075 Log.e("NfcReader", e.getMessage());
00076 finish();
00077 }
00078 }
00079
00080 @Override
00081 public void onResume() {
00082 super.onResume();
00083 if (nfcManager != null)
00084 nfcManager.enableForegroundDispatch();
00085 }
00086
00087 @Override
00088 protected void onNewIntent(Intent intent) {
00089 super.onNewIntent(intent);
00090
00091 if ((nfcManager != null) && (nfcManager.onNewIntent(intent))) {
00092 Log.i("NfcReader", "NFC tag read");
00093 byte[] payload = nfcManager.getPayload();
00094 if (payload.length != NFC_PAYLOAD_LENGTH + 3)
00095 {
00096 Log.e("NfcReader", "Payload doesn't match expected length: " + payload.length +" != " + NFC_PAYLOAD_LENGTH);
00097 return;
00098 }
00099
00100 data = new HashMap<String, Object>();
00101
00102 int offset = 3;
00103 data.put("WIFI", ByteArrays.toString(payload, offset, NFC_SSID_FIELD_LENGTH).trim());
00104 offset += NFC_SSID_FIELD_LENGTH;
00105 data.put("WIFIPW", ByteArrays.toString(payload, offset, NFC_PASSWORD_FIELD_LENGTH).trim());
00106 data.put("WIFIENC", "WPA2");
00107 offset += NFC_PASSWORD_FIELD_LENGTH;
00108 String host = ByteArrays.toString(payload, offset, NFC_MASTER_HOST_FIELD_LENGTH).trim();
00109 offset += NFC_MASTER_HOST_FIELD_LENGTH;
00110 short port = ByteArrays.toShort(payload, offset);
00111 data.put("URL", "http://" + host + ":" + port);
00112
00113 finish();
00114 }
00115 }
00116
00117 @Override
00118 public void onPause() {
00119 super.onPause();
00120 if (nfcManager != null)
00121 nfcManager.disableForegroundDispatch();
00122 }
00123
00124 @Override
00125 public void finish() {
00126
00127 Intent returnIntent = new Intent();
00128 if (data != null) {
00129
00130 returnIntent.putExtra("tag_data", data);
00131 setResult(RESULT_OK, returnIntent);
00132 }
00133 else {
00134 setResult(RESULT_CANCELED, returnIntent);
00135 }
00136
00137 super.finish();
00138 }
00139 }