NfcReaderActivity.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2013, Yujin Robot.
00005  *
00006  * All rights reserved.
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  *  * Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  *  * Redistributions in binary form must reproduce the above
00014  *    copyright notice, this list of conditions and the following
00015  *    disclaimer in the documentation and/or other materials provided
00016  *    with the distribution.
00017  *  * Neither the name of Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote products derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  * POSSIBILITY OF SUCH DAMAGE.
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) // 1 byte for status and 2 lang bytes
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; // skip 1 byte for status and 2 lang bytes
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         // Prepare data in tent
00127         Intent returnIntent = new Intent();
00128         if (data != null) {
00129             // Activity finished ok, return the data
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 }


android_remocons
Author(s): Daniel Stonier, Kazuto Murase
autogenerated on Sat Jun 8 2019 19:32:24