1

I'm trying to display the current SSID (and eventually capture it so I can use it at later date)

This far I've been able to compile the following code - but I can't get it to display and the application force closes each time I run it. I know I'm overlooking something simple. Any help is greatly appreciated!

  • Amani Swann

JAVA

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.preference.PreferenceActivity;

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager)      context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo =     connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager)     context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/linkingmanagerbackground2"
          android:layout_alignParentTop="true">


<View
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"  
    android:onClick="imageClick"  >
</View>

</RelativeLayout>

LOGCAT

03-16 01:34:52.938: E/Trace(2236): error opening trace file: No such file or directory (2)
03-16 01:34:53.487: D/dalvikvm(2236): GC_FOR_ALLOC freed 42K, 7% free 2466K/2652K, paused 45ms, total 49ms
03-16 01:34:53.567: I/dalvikvm-heap(2236): Grow heap (frag case) to 6.061MB for 3686416-byte allocation
03-16 01:34:53.627: D/dalvikvm(2236): GC_FOR_ALLOC freed 2K, 4% free 6064K/6256K, paused 55ms, total 55ms
03-16 01:34:53.717: D/dalvikvm(2236): GC_CONCURRENT freed <1K, 4% free 6064K/6256K, paused 9ms+16ms, total 95ms
03-16 01:34:55.048: D/libEGL(2236): loaded /system/lib/egl/libEGL_emulation.so
03-16 01:34:55.128: D/(2236): HostConnection::get() New Host Connection established 0x2a155228, tid 2236
03-16 01:34:55.208: D/libEGL(2236): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-16 01:34:55.218: D/libEGL(2236): loaded /system/lib/egl/libGLESv2_emulation.so
03-16 01:34:55.419: W/EGL_emulation(2236): eglSurfaceAttrib not implemented
03-16 01:34:55.448: D/OpenGLRenderer(2236): Enabling debug mode 0
03-16 01:34:56.568: D/AndroidRuntime(2236): Shutting down VM
03-16 01:34:56.568: W/dalvikvm(2236): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-16 01:34:56.638: E/AndroidRuntime(2236): FATAL EXCEPTION: main
03-16 01:34:56.638: E/AndroidRuntime(2236): java.lang.Error: Unresolved compilation problem: 
03-16 01:34:56.638: E/AndroidRuntime(2236):     Connect cannot be resolved to a type
03-16 01:34:56.638: E/AndroidRuntime(2236):     at     com.nfc.linked.SplashScreen$1.run(SplashScreen.java:34)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Handler.handleCallback(Handler.java:725)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Looper.loop(Looper.java:137)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invokeNative(Native Method)    
03-16 01:34:56.638: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at dalvik.system.NativeStart.main(Native Method)
user2161499
  • 67
  • 1
  • 10

2 Answers2

3

use the following permissions in your application manifest

< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
stinepike
  • 54,068
  • 14
  • 92
  • 112
2

Your problem doesn't seem to be related to the Wi-Fi SSID. You may have have one of these two problems before that:

Community
  • 1
  • 1
Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
  • I still have no idea what I should do. (Apologies if I'm a bit of a noob) – user2161499 Mar 16 '13 at 02:04
  • CURRENT LOGCAT: https://docs.google.com/document/d/1LMie5xogAJZR_eLw8D5_GGdH2Vs55xwdrqSHKgjDD38/edit?usp=sharing – user2161499 Mar 16 '13 at 02:54
  • Your problem seems to be earlier in the code. There is an exception: `com.nfc.linked.SplashScreen$1.run(SplashScreen.java:34)`. Post more of your code, preferable a smaller version of your app, with just what you need for the Wi-Fi part. See http://www.sscce.org/. – Christian Garbin Mar 16 '13 at 11:22
  • Ok - now I've got the kinks ironed out... but it only displays a blank white screen. (Anyone have any idea(s) why?) Current JAVA: https://docs.google.com/document/d/1SUjCKTd9AkdJMgoSL-5bRnONbUu43J4vbSCq7XFXjNA/edit?usp=sharing I REALLY appreciate your help with this!!! - Amani – user2161499 Mar 16 '13 at 17:11
  • If that's your entire code, you don't have any user interface to show anything. The code just reads the SSID. You need a view to show it. See http://developer.android.com/training/basics/firstapp/building-ui.html. You can also start with a simple toast for a quick test. See toast example in http://developer.android.com/guide/topics/ui/notifiers/toasts.html. Put that code right after your read the SSID, e.g. `Toast.makeText(getApplicationContext(), ssid, Toast.LENGTH_LONG).show();` – Christian Garbin Mar 16 '13 at 18:01