0

I am trying to listen the event when someone connect to/disconnect from my portable AP hotspot. I try this code

 Process p;
try
{
  p = Runtime.getRuntime().exec("logcat -s hostapd:I");
  BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
  String line;
  while ((line = br.readLine()) != null)
  {
    if (line.contains("AP-STA-CONNECTED") || line.contains("AP-STA-DISCONNECTED")) Log.e("mytag", line);
  }
}
catch (IOException e)
{
  // TODO Auto-generated catch block
  e.printStackTrace();
}

and it works like what I want. But I don't know how to put it in my service to run parallel with my activity. Or is there another way to identify when someone connect or disconnect from my portable AP hotspot?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
jnsn
  • 13
  • 1
  • 8

1 Answers1

0

Service do not run parallel with an Activity unless you put that on a separate thread.

if you use service or not you have to use separate thread to run this code as parallel.

In your case i believe best would to use WiFiManager to get SSID information.

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 if (wifiManager != null) {
     WifiInfo wifi = wifiManager.getConnectionInfo();
     if (wifi != null) {
        String ssid = wifi.getSSID();
 }           
}

if your App is monitoring which AP, it is connected to, try Broadcast Receiver with following intent. You can register it by code or in Android Manifest.

ConnectivityManager.CONNECTIVITY_ACTION

So how the whole things will work? whenever you app receive this connectivity change broadcast you will check AP name in your BroadcastReceiver.

minhaz
  • 4,233
  • 3
  • 33
  • 49
  • Thank for your answer, but my app is built to control system wifi hotspot with advanced function, I want to know when someone connect to my hotspot. I am trying to use the thread. – jnsn Apr 02 '13 at 06:42