Go to the documentation of this file.00001 package com.github.rosjava.android_remocons.rocon_nfc_writer;
00002
00003 import android.app.Activity;
00004 import android.content.Intent;
00005 import android.content.SharedPreferences;
00006 import android.os.Bundle;
00007 import android.view.Menu;
00008 import android.view.View;
00009 import android.widget.Button;
00010 import android.widget.EditText;
00011 import android.widget.TextView;
00012 import android.widget.Toast;
00013
00014 import com.github.robotics_in_concert.rocon_rosjava_core.rosjava_utils.ByteArrays;
00015 import com.github.rosjava.android_remocons.common_tools.nfc.NfcManager;
00016
00017 import java.security.InvalidParameterException;
00018
00019 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_APP_RECORD_FIELD_LENGTH;
00020 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_MASTER_HOST_FIELD_LENGTH;
00021 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_PASSWORD_FIELD_LENGTH;
00022 import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_SSID_FIELD_LENGTH;
00023
00027 public class RoconNfcWriter extends Activity {
00028
00029 private static final String PREFS_KEY_SSID = "SSID_KEY";
00030 private static final String PREFS_KEY_PSWD = "PSWD_KEY";
00031 private static final String PREFS_KEY_HOST = "HOST_KEY";
00032 private static final String PREFS_KEY_PORT = "PORT_KEY";
00033 private static final String PREFS_KEY_HASH = "HASH_KEY";
00034 private static final String PREFS_KEY_DATA = "DATA_KEY";
00035 private static final String PREFS_KEY_AAR = "AAR_KEY";
00036
00037 private String ssid;
00038 private String password;
00039 private String masterHost;
00040 private String masterPort;
00041 private String appHash;
00042 private String extraData;
00043 private String appRecord;
00044
00045 private EditText editSsid;
00046 private EditText editPassword;
00047 private EditText editMasterHost;
00048 private EditText editMasterPort;
00049 private EditText editAppHash;
00050 private EditText editExtraData;
00051 private EditText editAppRecord;
00052
00053 private Button buttonWrite;
00054 private TextView messageText;
00055
00056 private NfcManager mNfcManager;
00057
00058 @Override
00059 protected void onCreate(Bundle savedInstanceState) {
00060 super.onCreate(savedInstanceState);
00061 setContentView(R.layout.activity_main);
00062
00063 editSsid = (EditText) findViewById(R.id.ssid);
00064 editPassword = (EditText) findViewById(R.id.pasword);
00065 editMasterHost = (EditText) findViewById(R.id.master_host);
00066 editMasterPort = (EditText) findViewById(R.id.master_port);
00067 editAppHash = (EditText) findViewById(R.id.nfc_app_id);
00068 editExtraData = (EditText) findViewById(R.id.extra_data);
00069 editAppRecord = (EditText) findViewById(R.id.app_record);
00070
00071 buttonWrite = (Button) findViewById(R.id.nfc_write);
00072 messageText = (TextView) findViewById(R.id.message);
00073
00074 ssid = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_SSID, editSsid.getText().toString());
00075 password = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_PSWD, editPassword.getText().toString());
00076 masterHost = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_HOST, editMasterHost.getText().toString());
00077 masterPort = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_PORT, editMasterPort.getText().toString());
00078 appHash = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_HASH, editAppHash.getText().toString());
00079 extraData = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_DATA, editExtraData.getText().toString());
00080 appRecord = getPreferences(MODE_PRIVATE).getString(PREFS_KEY_AAR, editAppRecord.getText().toString());
00081
00082 editSsid.setText(ssid);
00083 editPassword.setText(password);
00084 editMasterHost.setText(masterHost);
00085 editMasterPort.setText(masterPort);
00086 editAppHash.setText(appHash);
00087 editExtraData.setText(extraData);
00088 editAppRecord.setText(appRecord);
00089
00090 mNfcManager = new NfcManager(this);
00091 }
00092
00093 @Override
00094 public boolean onCreateOptionsMenu(Menu menu) {
00095
00096 getMenuInflater().inflate(R.menu.main, menu);
00097 return true;
00098 }
00099
00100 @Override
00101 protected void onPause() {
00102
00103 super.onPause();
00104
00105 if(mNfcManager != null)
00106 mNfcManager.disableForegroundDispatch() ;
00107 }
00108
00109 @Override
00110 protected void onResume() {
00111
00112 super.onResume();
00113
00114 if(mNfcManager != null)
00115 mNfcManager.enableForegroundDispatch() ;
00116 }
00117
00118 @Override
00119 protected void onNewIntent(Intent intent) {
00120
00121 super.onNewIntent(intent);
00122
00123 if(mNfcManager != null) {
00124 if(mNfcManager.onNewIntent(intent)) {
00125 messageText.setText("You can write Nfc tag");
00126 messageText.setTextColor(0xFF00FF00);
00127 buttonWrite.setEnabled(true);
00128 }
00129 }
00130 }
00131
00132 public void onNfcWrite(View v) {
00133
00134 try {
00135 ssid = editSsid.getText().toString();
00136 password = editPassword.getText().toString();
00137 masterHost = editMasterHost.getText().toString();
00138 masterPort = editMasterPort.getText().toString();
00139 appHash = editAppHash.getText().toString();
00140 extraData = editExtraData.getText().toString();
00141 appRecord = editAppRecord.getText().toString();
00142
00143 if (appRecord.length() > NFC_APP_RECORD_FIELD_LENGTH)
00144 throw new InvalidParameterException("AAR limited to " + NFC_APP_RECORD_FIELD_LENGTH + " chars");
00145
00146 byte[] content = ByteArrays.concat(ByteArrays.toFixSizeBytes(ssid, NFC_SSID_FIELD_LENGTH, (byte)0),
00147 ByteArrays.toFixSizeBytes(password, NFC_PASSWORD_FIELD_LENGTH, (byte)0),
00148 ByteArrays.toFixSizeBytes(masterHost, NFC_MASTER_HOST_FIELD_LENGTH, (byte)0),
00149 ByteArrays.toBytes(Short.decode(masterPort)),
00150 ByteArrays.toBytes(Integer.decode(appHash)),
00151 ByteArrays.toBytes(Short.decode(extraData)));
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 boolean success = mNfcManager.writeTextNdefMessage(content, appRecord);
00165
00166 if(success) {
00167 messageText.setText("Success to write NFC Tag!");
00168 messageText.setTextColor(0xFF00FF00);
00169 }
00170 else {
00171 messageText.setText("Fail to write NFC Tag!");
00172 messageText.setTextColor(0xFFFF0000);
00173 }
00174
00175 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
00176 editor.putString(PREFS_KEY_SSID, ssid);
00177 editor.putString(PREFS_KEY_PSWD, password);
00178 editor.putString(PREFS_KEY_HOST, masterHost);
00179 editor.putString(PREFS_KEY_PORT, masterPort);
00180 editor.putString(PREFS_KEY_HASH, appHash);
00181 editor.putString(PREFS_KEY_DATA, extraData);
00182 editor.putString(PREFS_KEY_AAR, appRecord);
00183 editor.commit();
00184 }
00185 catch (InvalidParameterException e) {
00186 Toast.makeText(this, "Respect field max size: " + e.getMessage(), Toast.LENGTH_LONG).show();
00187 messageText.setText("Review fields format");
00188 messageText.setTextColor(0xFFFF0000);
00189 }
00190 catch (IllegalArgumentException e) {
00191 Toast.makeText(this, "Field must contain a short integer: " + e.getMessage(), Toast.LENGTH_LONG).show();
00192 messageText.setText("Review fields format");
00193 messageText.setTextColor(0xFFFF0000);
00194 }
00195 }
00196 }