I was surprised to find out that the available coding samples on WiFi for Android on the net are scarce. The only code sample easy to understand for dummies like me is this. So I begun to read carefully each line, understand it and then coping it to my demo application named WiFire. (Yeah, I know...) Yet, the application does not work both on Eclipse and my HTC Desire. I get the message: "The application WiFire has stopped unexpectedly. Please try again." So the first thing I want to find out is why the app doesn't work? I suspect that it has something to do with the version of Android because this code sample is from November 2009. I guess a lot of things have changed since then.
At this point I want to make some things clear. I really hope that coping this code doesn't violate any of the rules of the forum. As you can see in the link, it's a code sample so I guess it exists to learn from that (which means coping and modify it). My goal isn't coping code. My goal is to learn. So I don't care how many weeks/months will this going to take. I want to make a WiFi Manager in order to learn deep Android developing. So, I will post every few days/weeks new parts of code which I'll have written and with your constructive comments I will improve my skills. For the record, if I make it (and that's a big if), I'm planning to make this app open sourced.
But first things first! I need to understand the code. Below is the WiFire main Activity file:
package icsd.mittos.wifire;
import java.util.List;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class WiFireActivity extends Activity {
private static final String TAG = "WiFire";
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
Button buttonScan;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener((OnClickListener) this);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
@Override
public void onStop() {
unregisterReceiver(receiver);
}
public void onClick(View view) {
Toast.makeText(this, "On Click Clicked. Toast to that!!!",
Toast.LENGTH_LONG).show();
if (view.getId() == R.id.buttonScan) {
Log.d(TAG, "onClick() wifi.startScan()");
wifi.startScan();
}
}
}
Below is the ScanReceiver file:
package icsd.mittos.wifire;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
public class WiFiScanReceiver extends BroadcastReceiver {
private static final String TAG = "WiFiScanReceiver";
WiFireActivity wifire;
public WiFiScanReceiver(WiFireActivity wifire) {
super();
this.wifire = wifire;
}
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = wifire.wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest.", results.size(), bestSignal.SSID);
Toast.makeText(wifire, message, Toast.LENGTH_LONG).show();
Log.d(TAG, "onReceive() message: " + message);
}
}
This is the XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonScan" android:text="Scan"></Button> <ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textStatus" android:text="WiFiDemo" /> </ScrollView> </LinearLayout>
And this is the Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="icsd.mittos.wifire" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".WiFireActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
The 4 files above are exactly the same as the ones here, the only difference is the name of the app. Also I have created my own XML as a mockup for the app I want to create in due time:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" > <Button android:id="@+id/AutoScan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="50" android:text="AutoScan" /> <Button android:id="@+id/Refresh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="50" android:text="Refresh" /> </LinearLayout> <RelativeLayout android:id="@+id/InformationRelativeLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/Signal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="10dp" android:text="Signal" /> <TextView android:id="@+id/SSID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:text="SSID" /> </RelativeLayout> <LinearLayout android:id="@+id/linearLayout4" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text="Grand Line" android:layout_marginLeft="10dp" android:layout_weight="50" android:layout_width="fill_parent"/> <TextView android:text="99%" android:gravity="right" android:padding="3dip" android:layout_width="fill_parent" android:layout_weight="50" android:layout_marginRight="10dp"/> </TableRow> </TableLayout> </LinearLayout> <RelativeLayout android:id="@+id/InnerRelativeLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" > <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <Button android:id="@+id/ManagerButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Manager" android:layout_weight="33.3" android:layout_alignParentBottom="true"/> <Button android:id="@+id/RadarButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Radar" android:layout_weight="33.3" android:layout_alignParentBottom="true"/> <Button android:id="@+id/MapButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Map" android:layout_weight="33.3" android:layout_alignParentBottom="true"/> </LinearLayout> </RelativeLayout> </LinearLayout>
But that's useless right now.
So my first question is: What am I missing? Why the app doesn't work?
Thanks in advance, I hope this will be a pleasant journey. :)


Sign In
Create Account

Back to top









