Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification when a device connects to your android wifi tethering AP?

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<<");
    }
};
like image 869
arjun Avatar asked Nov 11 '22 11:11

arjun


1 Answers

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

like image 63
Sebastiano Avatar answered Nov 14 '22 23:11

Sebastiano