Android Wifi Hotspot Manager Class

Android has the great option to let you Tether your connection via wifi, but as developer you got little to none control over this mechanism.

Therefore i wrote a class to correct this: WifiApManager.

With this class you can check the current Status of the Hotspot, enable/disable it, get/set the current AP configuration and also get the list of currently connected clients.

I also made an example to demonstrate it’s capabilities:

Here a pseudocode excerpt of the example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Main extends Activity {
	WifiApManager wifiApManager; 
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		wifiApManager = new WifiApManager(this);
	}
 
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			wifiApManager.setWifiApEnabled(null, true);
			break;
		case 2:
			wifiApManager.setWifiApEnabled(null, false);
			break;
		}
	}
}
Download example Sourcecode

The Code is hosted on github: https://github.com/nickrussler/Android-Wifi-Hotspot-Manager-Class

 
License:
The Code is free and open source available under the Apache V2 License.

64 thoughts on “Android Wifi Hotspot Manager Class

  1. Hi Nick,
    First of all thank you for this information it helped a lot !
    i have couple of questions though..
    1) The code works fine for Android devices and Linux operating system laptops but it can not get information of the users of Windows phone and windows 7/8/10 operating system in laptops. These windows phone and windows 7/8/10 operating system users are connected to my phone but the application can not detect them.. Is it because windows holds some other path of ARP table other than “/proc/net/arp” or something ?
    2) I am getting Name of the device whenever mobile data is off and only hotspot in on. but when i switch on the mobile data i get only IP Address instead of name. why so ?
    3) What if i want to remove a particular user from the connection. is it possible ?

    Thanks in advance.

    • Hi, I’m glad you can make use of the code.

      I’m sorry but I can’t help you with your questions, hopefully you can find something when searching and posting on SO..

    • Hi parthak,

      I am opening the project in android studio, An error is generated saying that project is not build with gradle.can you please guide me opening the project.

  2. hi nick!!! your code works great…but i m not getting the list of connected devices. Plus the app does not work and throws null pointer exception in main.java on Line
    textView1.setText(“WifiApState: ” + wifiApManager.getWifiApState() + “\n\n”);
    and in wifiApmanger on line
    finishListener.onFinishScan(result);
    untill n unless i do it with try and catch block
    plzz help!!

  3. HI Nick,

    You example is very helpful. i convert it to Android Studio project and run it on my HTC phone. It created the hotspot successfull because I can found the SSID, which I set in code, on another phone.

    But my problem is when I list clients in hotspot, I got “false” value in the isReachable filed. What is the root cause for this observation?

  4. Hi Nick. First of all, thanks a ton for this wonderful example. It worked perfectly. I wanted to know whether can i get notified if any client gets connected to the hotspot. I want this to happen in the background when the app is not running. Any help to achieve this will be highly appreciated.
    Thanks

  5. Hi there i have implemented this into my application which runs perfectly, however after i updated to Lolipop there seems to be an issue with turning on the Access Point. I was wandering what could be the issue?

    • I had a similar issue. It seems that you need to add a new permission in the manifiest file.
      Try adding the “android.permission.CHANGE_NETWORK_STATE” permission

  6. Hey its me i have used your code in conjunction with a project someone else has worked on, however im not able to see the list of clients that have joined.

    http://stackoverflow.com/questions/28928661/attempt-to-invoke-virtual-method-android-os-looper-android-content-context-getm/28928746#28928746

    This is the code that i have used, ignore the issue that i have asked on stack overflow i have fixed that problem now. However as mentioned before when i install the apk everything works fine however when i click the get clients button nothing seems to happen. If you would like i can give you the APK file for you to take a look?

  7. Hi, Nick
    I have more question.
    When I get a list of clients connected, client’s reachable value always ‘false’.
    And client is disconnected but list contain it’s info.
    I think it can’t be helped because arp is not refresh..
    Then, can I know connected client’s name (ex. Choi’s PC)?
    Please advice would be appreciated.

  8. Hi, Nick.
    Thanks for great library!
    I have one question.
    How can I detect client’s connection to my hotspot (after turns on the hotspot)
    like status bar-push message (ex.”1 device connected”)
    I wanna do something by programmatically when client connect hotspot.
    If you know about it, help me please.

  9. Hello i absolutely love what you have done here. Im not to sure on the regulations on the re usability of code, however would be possible if i can use this code for my dissertation project? im sorry if this is a silly question because in all i really wouldnt want to use this without your consent

    Thank you

  10. Hi. My Asus Fonepad (Android 4.4.2) has a hotspot setting that disables Wi-Fi after 8 minutes of inactivity. Is there a way to programmatically set it to never?

  11. Hi Nick,
    Firstly thanks for this excellent library.
    have you ever seen a case where android stops updating the file in proc/net/arp?

    I can connect clients to my phone’s hot stop but the file contains no listings, hence the method of reading to file to detect clients no longer works. Any ideas appreciated.

  12. Im having some problems yo open hotspot with hidden ssid by code, is this posible? Im thinking in that solution because o couldn’t connect to a password secury wifi. Any information about this?

    Thank you very much in advance!

      • Sorry about my last post, i wrote it on my cell phone and didnt checked it after writing. Im trying to open a hotspot with hiddenSSID set to true, but i can see it from all devices. Is the atribute deprecated? I have to set something else?

        WifiManagermWifiManager = (WifiManager) this.context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = “NETWORK”;
        wifiConfig.status = WifiConfiguration.Status.ENABLED;
        wifiConfig.hiddenSSID=true;
        Method method = mWifiManager.getClass().getMethod(“setWifiApEnabled”, WifiConfiguration.class, boolean.class);
        return (Boolean) method.invoke(mWifiManager, wifiConfig, enabled);

  13. when I disconnect client, the example can’t update the clients list in the view. And I restart the app, it can’t know the client has been disconnected either, how to know client was disconnected dynamitially?

  14. Is it possible to control access to internet after creating wifi hotspot. (like disabling internet access but keeping the wifi on)

  15. Hey, great class. Do you think there is a way to view the montly per-client data usage? I’d like o have some more detailed stats than what Android provides.

    • private void configWifi(){
      wc.SSID = “\”SSIDName\””;
      wc.preSharedKey = “password”;
      wc.hiddenSSID = true;
      wc.status = WifiConfiguration.Status.ENABLED;
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
      wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
      wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

  16. I want to define a connection to my access point. I want receive notifications when users connected. I know that I can query arp table(/proc/net/arp) , from which I can find connected users.
    Is there any better way to do this, except viewing arp-table every n seconds?

    • Well you could start a seperate Thread that calls an event listener instance. Inside this thread you would poll. This would not solve your problem at all but hides it 😉

  17. Hello.

    i used the above method to turn on tethering. But my AP is not found by other wifi devices. I am setting an odroid-A device as hotspot. Other wifi mobiles doesnot detect the hotspot.

    What could be the reason

  18. Hi, first of all thank you it’s very useful!
    But, do you know which broadcast intent is necessary for broadcast receivers which should listen on changes made on the device’s AP (enabling, disabling,…) ?

    thx & regards

  19. Thank, Good Information.

    if use ClientList than Use ‘Flags’ in ‘/proc/net/arp’ file.

    it can check connected or not connected~

    • Thanks for your interesting idea, could you implement it and make a pull request on github? If that’s too complicated for you, could you just send me a code example?

      Best Regards
      Nick

  20. Hi Nick, great job.
    Do you know if it is possible to change the channel also?
    By default is 6, but I want to change it. I was looking in the source and I discover one field which could be suitable for that but I didn’t get anything (no change, no error)
    I’m doing this:
    WifiConfiguration netConfig = new WifiConfiguration();
    netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

    Field wcAdhocFreq = WifiConfiguration.class.getField(“frequency”);
    wcAdhocFreq.setAccessible(true);
    wcAdhocFreq.setInt(netConfig, 2462); //2462 = center freq of channel 11

    mWifiApManager.setWifiApEnabled(netConfig, true);

    any idea? thx!

  21. Hey, I found this by Google and I must say it’s so helpful, thank you for sharing 🙂 I’ve literally just plugged this into my app (made your example app a library in eclipse) and it works like an ace, but I’m wondering if it’s possible to get the IP address of the hotspot? Can I get that from the WifiManager object? Also, do you have any specific license you want to put this under or is it free?

  22. can you tell me why is the getWifiApConfiguration() function return null and also it does not set the WifiConfiguration options when passed in setWifiApEnabled() it just uses the default Ap options.

    • Could you tell me the android version you are using? Best case you could provide me a version of the emulator to reproduce the (potential) bug?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.