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.
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.
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!!
Can you open an issue on Github and post the stacktraces there? Thanks!
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?
You can have a look on the reachability check on github. It could be that splitted[0] does not hold anything data..?
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
You could poll the list of connected clients in a seperate background thread/task and compare it to the old list, then you can act on changes in the list.
Thank you Nick! Will try it out.
How can I add ssid to create the hotspot ?
When you enable the AP with setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) you can pass a WifiConfiguration instance which holds a SSID.
I try change SSID and password but not successful.
I created new instance belong WifiConfiguration, pre-set BSSID and preShareKey, and pass it to setWifiApEnabled() method. The app run without cashing but I just get the null SSID and without any security.
Could you share the full configuration for WifiConfiguration instance before pass it to setWifiApEnabled() method.
Have you tried to disable the AP then re-enable it with a new SSID?
Hi Nick,
I tried successfully with change SSID, I assigned SSID instead of BSSID and next I stuck with security, it always open without any security method.
Could you guide me which parameters in WifiConfigurtion class I should change to enable security for hotspot?
Thanks.
I don’t really know but maybe this link will help you: http://stackoverflow.com/questions/4374862/how-to-programatically-create-and-read-wep-eap-wifi-configurations-in-android
Thanks it Reduces 5 hours My Effort.
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
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?
You should strip down your code to a minimum and post it to Stackoverflow.
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.
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.
Polling is the way to go! Get the connected clients in a loop (maybe wait in between) and compare the lists.
Thank you Nick!
I’ll try
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
Yes, you can most likely use it! The code is licensed under the free open source Apache V2 license.
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?
Sorry, i am not aware of such an API.. Maybe there are some Asus specific classes available which you can use reflection on?
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.
Hi Ronan, i am not aware of this behavior. Could you provide me some details (Device, Android Version)?
Very useful class, thanks for sharing
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!
To be honest i am not really sure what you are talking about 🙂 Could you maybe post some more details?
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);
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?
You could try to ping the client to check if he is still connected. Do this every x ms and you can track who disconnects.
Is it possible to control access to internet after creating wifi hotspot. (like disabling internet access but keeping the wifi on)
I don’t think this is possible via an (un-)official java API, but since Android is a linux there might be a way to do e.g. mac filtering.
WOW just what I was searching for. Came here by searching for android
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.
Statistics and such are not covered in this class. There are various traffic monitor apps out there, maybe one of them is open source.
How to change Name and password of wifi hostpost
Hi, take a look at setWifiApEnabled and the WifiConfiguration class.
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);
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 😉
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
Do these other devices find your AP when you turn it on manually? Some devices hide Adhoc wlan networks.
Do anyone knows how to hide ssid?
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
Maybe this can help you.
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
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!
Hi,
I searched a bit but couldn’t found a way that seemed to be working..
I now opened a Stackoverflow post, maybe we will get our answer there:
http://stackoverflow.com/questions/16896124/set-channel-of-android-hotspot
Me again, trying to get the IP from the WifiManager object returns 0. 🙁
Maybe something is wrong with your code take a look how to get the local IP: http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device.
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?
I think the IP of the Hotspot is simply the IP of the device, which you can simply get (http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device).
I will make the Class available under Apache V2 in a few minutes, but if that isn’t good for you just contact me and we will find a solution 🙂
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?
Is it possible to get the signal strength of the clients connected?
It may, but since this is undocumented API you would need to crawl through the sources..