5

I enable wi-fi tethering through my application in my android device. How do I get notification in my application when someone connects to the Wi-Fi network tethered by my application? Do I need to register for some specific broadcast receivers?

I have pasted below the application source code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((ToggleButton)   findViewById(R.id.toggle_tethering)).setOnCheckedChangeListener(this);
}

@Override

public void onCheckedChanged(CompoundButton button, boolean isChecked) {

   WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   Boolean result = false;
   WifiConfiguration config = new WifiConfiguration();
   config.SSID = "Tab3OpenWifi";
   config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
   String setWifiApConfigurationMethodName = "setWifiApConfiguration";
   Method setWifiApConfigurationMethod = wifiManager.getClass().getMethod(setWifiApConfigurationMethodName, WifiConfiguration.class);
   result = (Boolean) setWifiApConfigurationMethod.invoke(wifiManager, config);

   if (result) {
      String setWifiApEnableMethodName = "setWifiApEnabled";
      Method setWifiApEnableMethod = wifiManager.getClass().getMethod(setWifiApEnableMethodName, WifiConfiguration.class, boolean.class);
      String message;
      if (isChecked) {
         result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, true);
         if (result) {
             message = "Enabling tethering successfully";
         } else {
             message = "Enabling tethering failed";
         }
     } else {
         result = (Boolean) setWifiApEnableMethod.invoke(wifiManager, null, false);
         if (result) {
            message = "Disabling tethering successfully";
         } else {
            message = "Disabling tethering failed";
         }
     }
     Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
   } else {
     Toast.makeText(this, "Failed to update Wifi Tethering config.", Toast.LENGTH_SHORT).show();
   }
}

I used below broadcast receiver to get the AP state.

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override

    public void onReceive(Context context, Intent intent) {

        Log.d(TAG,"mReceiver.onReceive>>");
        String action = intent.getAction();
        if (WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
            Log.d(TAG,"WIFI_AP_STATE_CHANGED_ACTION");
        } else if (ACTION_TETHER_STATE_CHANGED.equals(action)) {
            ArrayList<String> available = intent.getStringArrayListExtra(
                    EXTRA_AVAILABLE_TETHER);
            ArrayList<String> active = intent.getStringArrayListExtra(
                    EXTRA_ACTIVE_TETHER);
            ArrayList<String> errored = intent.getStringArrayListExtra(
                    EXTRA_ERRORED_TETHER);

            Log.d(TAG,"==Available==");
            for(String str : available)
                Log.d(TAG, ""+str);

            Log.d(TAG,"==Active==");
            for(String str : active)
                Log.d(TAG, ""+active);

            Log.d(TAG,"==Error==");
            for(String str : errored)
                Log.d(TAG, ""+str);

        } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {

        }
        Log.d(TAG,"mReceiver.onReceive<<");
    }
};
arjun
  • 61
  • 1
  • 4
  • At the moment I am using the method mentioned in this link to fetch the connected devices. Instead of query each time I would like to get notified when client connects. http://stackoverflow.com/questions/21522961/how-to-get-the-client-device-details-which-is-connected-to-wifi-hotspot – arjun Mar 14 '14 at 09:40

1 Answers1

1

I don't think there is an API for that, nor any broadcast event which you can listen to.

The only option that I can think of is operating at a lower level, i.e. the kernel level. I am no expert, but I used to monitor the /proc/net/tcp file to look for open connections. That may be a starting point for you.

I also wrote a simple library, if you want to get a glimpse of how I did it. You can find it here: https://github.com/dextorer/AndroidTCPSourceApp

Sebastiano
  • 12,289
  • 6
  • 47
  • 80
  • In my experience, you need to use jni to read the /proc/net/tcp files because reading from java doesn't return the full file. I found this by experimentation. This stackoverflow seems to confirm it http://stackoverflow.com/questions/6632314/android-progamatically-determine-list-of-apps-that-have-a-tcp-connection-open – Juan Acevedo Oct 06 '15 at 23:29