Skip to content
This repository has been archived by the owner on Mar 11, 2019. It is now read-only.

mvdan/accesspoint

Repository files navigation

accesspoint

Build Status

Manage wireless access points on Android 2.2 or later.

Note that this project is abandoned since its method doesn't work on Android 7.1 or later. Have a look at these newer alternatives that have been tested to work on Android 8.0:

Quick start

repositories {
	jcenter()
}

dependencies {
	compile 'cc.mvdan.accesspoint:library:0.2.0'
}
import cc.mvdan.accesspoint.WifiApControl;

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiApControl apControl = WifiApControl.getInstance(context);

boolean enabled = apControl.isEnabled();
int state = apControl.getState();

WifiConfiguration config = apControl.getConfiguration();
Inet4Address addr4 = apControl.getInet4Address();
Inet6Address addr6 = apControl.getInet6Address();

// These are cached and may no longer be connected, see
// WifiApControl.getReachableClients(int, ReachableClientListener)
List<WifiApControl.Client> clients = apControl.getClients()

// Wifi must be disabled to enable the access point
wifiManager.setWifiEnabled(false);
apControl.enable();

apControl.disable();
wifiManager.setWifiEnabled(true);

Remember to ask for WRITE_SETTINGS on 6.0+! The example app has a sample implementation of that. The library will not do this for you, as it involves UI interaction that should be handled and customizable by your app.

Features

  • Enabling and disabling your AP
  • Configuring your AP via WifiConfiguration
  • Getting your device's IP address in your AP network
  • Getting the list of clients connected to your AP

Permissions

You will need the following:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

How does it work?

Enabling, disabling and configuring of wireless Access Points are all unaccessible in the SDK behind hidden methods in WifiManager. Reflection is used to get access to those methods.

Getting your own IP address is done by getting the IP address that is associated with the wireless network interface.

Getting the list of clients consists of parsing /proc/net/arp and parsing each line to see what devices are neighbours in our wireless network.

An extra method is available to get the list of reachable clients, since the arp table is cached for up to a few minutes. The method asynchronously tests the reachability of each client.

License

Published under the Apache2 license.