Go to the documentation of this file.00001 package com.ros.turtlebot.apps.rocon;
00002
00003
00004 import java.net.URI;
00005 import java.net.URISyntaxException;
00006 import java.util.List;
00007
00008 import org.ros.android.MasterChooser;
00009 import org.ros.android.RosActivity;
00010 import org.ros.exception.RosRuntimeException;
00011
00012 import android.annotation.SuppressLint;
00013 import android.app.Activity;
00014 import android.content.BroadcastReceiver;
00015 import android.content.Context;
00016 import android.content.Intent;
00017 import android.content.IntentFilter;
00018 import android.net.Uri;
00019 import android.net.wifi.ScanResult;
00020 import android.net.wifi.WifiConfiguration;
00021 import android.net.wifi.WifiManager;
00022 import android.nfc.NfcAdapter;
00023 import android.os.AsyncTask;
00024 import android.os.Bundle;
00025 import android.os.Vibrator;
00026 import android.util.Log;
00027 import android.widget.TextView;
00028 import android.widget.Toast;
00029
00030 public class NfcReaderActivity extends Activity {
00031
00032 WifiManager wifiManager = null ;
00033 IntentFilter wifiFilter = null ;
00034 List<ScanResult> scanResults ;
00035 List<WifiConfiguration> configs ;
00036 Vibrator vibrator = null ;
00037
00038 boolean connecting = false ;
00039
00040 String nfc_table = "";
00041 String nfc_ssid = "";
00042 String nfc_password = "";
00043 String nfc_masteruri = "";
00044 String nfc_weblink = "";
00045
00047 @Override
00048 public void onCreate(Bundle savedInstanceState) {
00049 super.onCreate(savedInstanceState);
00050
00051
00052
00053 wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
00054 wifiFilter = new IntentFilter();
00055 wifiFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
00056 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
00057
00058 vibrator.vibrate(500);
00059
00060 Intent intent = getIntent();
00061 String action = intent.getAction();
00062 RoconNfcManager nfcManager = new RoconNfcManager(this);
00063 nfcManager.onNewIntent(intent);
00064
00065 if(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))
00066 {
00067
00068 String payload = nfcManager.getPayload();
00069 String[] params = payload.split(";;");
00070
00071 for(String param : params) {
00072 if(param.startsWith("ta:"))
00073 nfc_table = param.substring(3);
00074 else if(param.startsWith("ss:"))
00075 nfc_ssid = param.substring(3);
00076 else if(param.startsWith("pw:"))
00077 nfc_password = param.substring(3);
00078 else if(param.startsWith("mu:"))
00079 nfc_masteruri = param.substring(3);
00080 else if(param.startsWith("wl:"))
00081 nfc_weblink = param.substring(3);
00082 }
00083
00084 if(nfc_table.length() == 0 ||
00085 nfc_ssid.length() == 0 ||
00086 nfc_masteruri.length() == 0 ||
00087 nfc_weblink.length() == 0)
00088 {
00089 String stat = "NFC Data is wrong!" + "\n-Table No : " + nfc_table + "\n-SSID : " + nfc_ssid + "\n-Password : " + nfc_password +
00090 "\n-MasterURI : " + nfc_masteruri + "\n-WebLink : " + nfc_weblink ;
00091 Toast.makeText(this, stat, Toast.LENGTH_LONG).show();
00092 }
00093
00094
00095 if(!wifiManager.isWifiEnabled())
00096 wifiManager.setWifiEnabled(true);
00097
00098
00099 wifiManager.startScan();
00100 configs = wifiManager.getConfiguredNetworks() ;
00101
00102
00103
00104 }
00105 }
00106
00107 @Override
00108 protected void onResume() {
00109
00110 super.onResume();
00111 if(wifiFilter != null)
00112 registerReceiver(wifiEventReceiver, wifiFilter);
00113 }
00114
00115 @Override
00116 protected void onPause() {
00117
00118 super.onPause();
00119 if(wifiFilter != null)
00120 unregisterReceiver(wifiEventReceiver);
00121
00122 finish();
00123 }
00124
00125 private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() {
00126
00127 @Override
00128 public void onReceive(Context context, Intent intent) {
00129
00130
00131 if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
00132
00133 scanResults = wifiManager.getScanResults();
00134 if(connecting == false) {
00135 connecting = true ;
00136 wifiConnect() ;
00137
00138 Intent rocon_intent = new Intent(NfcReaderActivity.this, RoconMainActivity.class);
00139 rocon_intent.putExtra("table", nfc_table);
00140 rocon_intent.putExtra("weblink", nfc_weblink);
00141 rocon_intent.putExtra("masteruri", nfc_masteruri);
00142
00143 startActivity(rocon_intent);
00144 }
00145 }
00146 }
00147 };
00148
00149
00150 private void wifiConnect()
00151 {
00152 Toast.makeText(this, "wifiConnect()", Toast.LENGTH_SHORT).show();
00153
00154 scanResults = wifiManager.getScanResults();
00155 if(scanResults == null || scanResults.size() == 0) {
00156 Toast.makeText(this, "No access point is found.", Toast.LENGTH_SHORT).show();
00157 return ;
00158 }
00159
00160 ScanResult foundResult = null ;
00161 for(ScanResult result : scanResults){
00162 if(nfc_ssid.equals(result.SSID)) {
00163 foundResult = result ;
00164 break ;
00165 }
00166 }
00167
00168 if( foundResult == null ) {
00169 Toast.makeText(this, "Sorry!" + nfc_ssid + " is not found!", Toast.LENGTH_SHORT).show();
00170 return ;
00171 }
00172
00173 configs = wifiManager.getConfiguredNetworks() ;
00174
00175 for(WifiConfiguration config : configs){
00176 if(("\"" + nfc_ssid + "\"").equals(config.SSID)) {
00177 wifiManager.enableNetwork(config.networkId, true);
00178 return ;
00179 }
00180 }
00181
00182 WifiConfiguration config = new WifiConfiguration() ;
00183 config.SSID = "\"" + foundResult.SSID + "\"" ;
00184 config.priority = 40 ;
00185
00186 if(foundResult.capabilities.contains("WPA")) {
00187 config.status = WifiConfiguration.Status.ENABLED ;
00188 config.preSharedKey = "\"" + nfc_password + "\"" ;
00189 } else if(foundResult.capabilities.contains("WEB")){
00190 config.wepKeys[0] = "\"" + nfc_password + "\"" ;
00191 config.wepTxKeyIndex = 0 ;
00192 } else {
00193 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
00194 }
00195
00196 int newId = wifiManager.addNetwork(config);
00197 if(newId < 0) {
00198 Toast.makeText(this, "Sorry! Fail to add network " + nfc_ssid, Toast.LENGTH_SHORT).show();
00199 return ;
00200 }
00201 else {
00202 if(wifiManager.enableNetwork(newId, true)) {
00203 Toast.makeText(this, "Trying to connect to " + config.SSID, Toast.LENGTH_SHORT).show();
00204 wifiManager.saveConfiguration();
00205 }
00206 else {
00207 Toast.makeText(this, "Sorry! Fail to connect to " + nfc_ssid, Toast.LENGTH_SHORT).show();
00208 }
00209 }
00210 }
00211
00212 }