Android Wifi Roaming through AP with same SSID - android

I saw that Android system has a bad behavior with Wifi roaming.
We have a Wifi centralized network with many AP with a signle SSID.
The Adroid Phones wont roams seamlessly.
An Android Phone tries to stay connected to an AP until the signal reaches zero even if there are others AP (with the same SSID) with a good signal!
When the signal is zero, finally it performs an assosiation to another AP (with a good signal). But with this behavior the phone loses all the TCP Connections!
For example:
the phone is connected in WiFi to AP1
the phone moves in the building and now hears two signals from AP1 and from AP2.
When the signal form AP2 is stronger than the signal from AP1, i want that the phone do a reassosiation (not an assosiation) to AP2.
The idea is:
Perform a WifiManager.startScan()
Get the results WifiManager.getScanResults()
Find the best AP in the results
Perform a reassosiation to the best AP
Repeat every 30 seconds.
I talk about reassosiation because i don't want that the phone loses the TCP Connections.
There is a way to do this ?
Thank you,
Salvo

You cannot do this as you describe. A client cannot determine the state of the TCP connection on it's own. Your network must also move the communication channel from one AP to another. This can be done with the right network controllers.
Also, you should look at IEEE 802.11k -
https://en.wikipedia.org/wiki/IEEE_802.11k-2008

Add below permissions;
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Register for below intent;
private WifiBroadcastReceiver wifiBroadcastReceiver = new WifiBroadcastReceiver();
Then in routine;
registerReceiver(wifiBroadcastReceiver, new IntentFilter("android.net.wifi.SCAN_RESULTS"));
Use the below class to change the reassociation;
public class WifiBroadcastReceiver extends BroadcastReceiver {
private WiFiManager manager = null;//set the value in constructor
private WifiConfiguration connectedConfiguration = null;//set the value in constructor
private int connectedNetId;
private void updateConnectedConfiguration(String ssid) {
configs = manager.getConfiguredNetworks();
int nid = 0;
for (WifiConfiguration cnf : configs) {
if (cnf.SSID.substring(1, cnf.SSID.length() - 1).equals(ssid)) {
connectedConfiguration = cnf;
connectedNetId = nid;
}
nid++;
}
}
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = manager.getScanResults();
WifiInfo info = manager.getConnectionInfo();
ScanResult stronger = null;
for (ScanResult scanResult : results) {
try {
if (scanResult.SSID.equals(info.getSSID())) {
if (stronger == null) {
if (WifiManager.compareSignalLevel(info.getRssi() + 5, scanResult.level) < 0) {
stronger = scanResult;
}
} else if (WifiManager.compareSignalLevel(stronger.level, scanResult.level) < 0) {
stronger = scanResult;
}
}
} catch (Exception e) {
}
}
if (stronger != null && !stronger.BSSID.equals(info.getBSSID())) {
updateConnectedConfiguration(info.getSSID());
if (connectedConfiguration != null) {
connectedConfiguration.BSSID = stronger.BSSID;
manager.updateNetwork(connectedConfiguration);
manager.saveConfiguration();
manager.enableNetwork(connectedNetId, true);
manager.reassociate();
info = manager.getConnectionInfo();
//showNotification("\nConnecting " + stronger.SSID, stronger.BSSID + " " + stronger.level + "dBm");
}
}
}
}

Related

in an app, how to swich between access points without loosing connection?

Trying to connect to same SSID, but different BSSID (same WiFi, different access point). Now, if I disconnect and reconnect, why do I still have to find the best signal? By default, it will connect to best signal (closest) access point. How can I connect to best signal without disconnecting and reconnecting?
I'm using wifimanager to find my network and my access points. I can find all my access points and their BSSID and their signal level. Now, in order to connect to best signal, I need to disconnect and reconnect, this will make my app to loose connection for a while, until reconnects.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
List<ScanResult> wifiList = wifiManager.getScanResults();
ArrayList<String> deviceList = new ArrayList<>();
for (ScanResult scan_wifi_results : wifiList) {
if (scan_wifi_results.SSID.equals("Office")||scan_wifi_results.SSID.equals("Warehouse")) {
if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level, scan_wifi_results.level) < 0) { bestSignal = scan_wifi_results; }
deviceList.add(scan_wifi_results.SSID + " - db: " + scan_wifi_results.level + " - Mac: " + scan_wifi_results.BSSID); }
}
ArrayAdapter arrayAdapter = new ArrayAdapter(context, R.layout.wifi_template, deviceList.toArray());
wifiDeviceList.setAdapter(arrayAdapter);
}
WifiConfiguration config = new WifiConfiguration();
config.SSID = bestSignal.SSID;
config.BSSID = bestSignal.BSSID;
netID = wifiManager.addNetwork(config);
}
wifiManager.disconnect();
wifiManager.enableNetwork(netID,true);
wifiManager.reconnect();
Toast.makeText(context,"Connected to best Signal: " + bestSignal.BSSID,Toast.LENGTH_SHORT).show();
}
if I add wifiManager.enableNetwork(netID,true) without disconnect and reconnect, it will not change access point
Is possible to accomplish that without loosing wifi connection?

How to view list of devices that is connected to my router and select one of them then send and receive data from it

I am new to android application development and I want to build an application that uses wifi to connect to another devices (not mobile devices) and sends some data from my phone to a device and receives some data from the other device.
What I have in my local network:
mobile phone support wifi with android os.
device support wifi connection (e.g. temperature sensor).
What I need:
The phone needs to connect to the router and receive a list of available devices and check if the sensor is connected to the network or not.
Connect to the sensor and send a message from the phone that tells the sensor to measure the temperature.
The sensor sends back a message that contains the temperature.
Your question is nor quite clear to me. But as per my understanding you want to list down some Wi-Fi enabled devices on your mobile & then connect to one of them & finally communicate with them.
For Listing all access points you have to implement the code below.
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
List<ScanResult> scanResults = mainWifiObj.getScanResults();
mScannedNetworksAdapter.clear();
listView_Access_point.clearChoices();
if (scanResults != null) {
for (ScanResult result : scanResults) {
mScannedNetworksAdapter.add(result)
}
}
listView_Access_point.setAdapter(mScannedNetworksAdapter);
}
}
};
Now After listing all the Wi-Fi access points you have to click one of them to get connected. To do this you have implement OnItemClickListener & then override onItemclick(). See the code below.
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
ScanResult result = (ScanResult) parent.getItemAtPosition(position);
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + result.SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if(list!=null){
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + result.SSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
}
Then you have to implement a BroadcastReceiver which tells if the access point is connected successfully.
private final BroadcastReceiver mWifiConnectionEstablished = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (NetworkInfo.State.CONNECTED.equals(nwInfo.getState()) && nwInfo.isConnected()) {
//The connection is established. Now do your stuff here
}
}
}
};
Finally there are several methods for communication. One of them is socket communication. You will get several examples in google for socket communication in android.

Android: detect whether wifi access point still in range

I have a method to detect available wifi access ppoints. My method works well but when I am not in the range I still getiing the SSID of the last scan results displayed in my xml file though I am out of the range of this SSID.
private void check_wifi_available() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(this, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
}
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "SV-"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
// Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
// Matcher m = p.matcher(ssid);
// if(m.matches()){}else{}
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
}
}
if (updatedResults.size() > 0) {
String a = deliverBestAccessPoint(updatedResults);
textWifi.setText(a.toString());
}
}
}
This is best handled by the operating system. The best you could do is set up a timer to periodically scan for WiFi devices and update the results.
Other than that, on rooted devices you may be able to manually send 802.11 requests to the access point/router and do a timeout check for replies.
To clarify: the operating system, when it is scanning for devices, sends out a broadcast message and reports what devices it hears back from. When devices are toward the edge of the 'range' they may report as being available even if connecting and maintaining a connection is problematic because the signal is not strong enough.
EDIT:
For what it's worth, ScanResult has a "level" member variable that reports the signal strength. You could do some more fine filtering for low-strength results. http://developer.android.com/reference/android/net/wifi/ScanResult.html

How do you retrieve the WiFi Direct MAC address?

I am trying to retrieve the MAC address of an Android device. This is ordinarily possible through the WiFiManager API if WiFi is on.
Is there any way to get the MAC address if WiFi is off and WiFi Direct is on?
WiFi AND WiFi Direct can't be on at same time on my phone.
Thanks
I had been searching for this during my project. My requirements were to uniquely identify devices in an adhoc P2p network formed with WiFi Direct. Each device should identify its friend device the next time when it comes into proximity. I needed my own WiFi (Direct) MAC and my friends' to create a Key for this friend zone creation.
My Research: The design is in such a way that there is an Unique Universal ID and a Local ID. Reason: Universal ID can only be used to connect to Infrastructure mode Networks. Local ID could be used for "ad-hoc" mode networks(device to device). In this ad-hoc mode, there are possibilities that a single device might simultaneosly belong to several ad-hoc groups.
Hence to support this concurrent operations, P2p devices support
Multiple MAC entities, possibly on different channels.
For each session, a persistent group MAY use a different channel and device
MAC for each session.
P2P devices use their global MAC address as Device ID during discovery and negotiation, and a temporary local MAC address for all frames within a group. Understood from here
However, there is NO straight forward way to obtain one's own WiFi P2p MAC address. Issue 53437: Android.
In this issue discussion, the project member from google has suggested this is possible and just that it hasn't been documented
Solution: Using intent filter WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION and the extra
from the intent WifiP2pManager.EXTRA_WIFI_P2P_DEVICE
This is how I have used it in my project:
#Override
public void onReceive(Context context, Intent intent) {
....
....
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION
.equals(action)) {
WifiP2pDevice device = (WifiP2pDevice) intent
.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
String myMac = device.deviceAddress;
Log.d(TAG, "Device WiFi P2p MAC Address: " + myMac);
/* Saving WiFi P2p MAC in SharedPref */
sharedPref = context.getSharedPreferences(context.getString(R.string.sp_file_name), Context.MODE_PRIVATE);
String MY_MAC_ADDRESS = sharedPref.getString(context.getString(R.string.sp_field_my_mac), null);
if (MY_MAC_ADDRESS == null || MY_MAC_ADDRESS != myMac) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(context.getString(R.string.sp_field_my_mac), myMac);
editor.commit();
}
Hope this helps someone!
The mac addresss of WiFi is different than that of WiFi Direct. Usually first 2 letters might be different. Be careful about that.
The mac address of WiFi is different than that of WiFi Direct.
You can get WiFi direct address using next code:
public String getWFDMacAddress(){
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ntwInterface : interfaces) {
if (ntwInterface.getName().equalsIgnoreCase("p2p0")) {
byte[] byteMac = ntwInterface.getHardwareAddress();
if (byteMac==null){
return null;
}
StringBuilder strBuilder = new StringBuilder();
for (int i=0; i<byteMac.length; i++) {
strBuilder.append(String.format("%02X:", byteMac[i]));
}
if (strBuilder.length()>0){
strBuilder.deleteCharAt(strBuilder.length()-1);
}
return strBuilder.toString();
}
}
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
return null;
}
The WiFi Direct mac address is going to be different. It's explained beautifully by #auselen here https://stackoverflow.com/a/14480530/3167704.
I just figured out a way to retrieve WiFi Direct mac address. It isn't pretty but gets the job done. Here's the code,
final WifiP2pManager p2pManager = (WifiP2pManager) getSystemService(WIFI_P2P_SERVICE);
final WifiP2pManager.Channel channel = p2pManager.initialize(this, getMainLooper(), null);
p2pManager.createGroup(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
p2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
#Override
public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
Log.i("", wifiP2pGroup.getOwner().deviceAddress);
// Following removal necessary to not have the manager busy for other stuff, subsequently
p2pManager.removeGroup(channel, new WifiP2pManager.ActionListener() {
#Override
public void onSuccess() {
Log.i("", "Removed");
}
#Override
public void onFailure(int i) {
Log.i("", "Failed " + i);
}
});
}
});
}
#Override
public void onFailure(int i) {
Log.i("", String.valueOf(i));
}
});

android: Determine security type of wifi networks in range (without connecting to them)

I can enumerate all wifi networks in range (using startScan + SCAN_RESULTS_AVAILABLE_ACTION + getScanResults) and get their SSID and BSSID values, but I can't figure out how to determine the security type of each network.
In my main object:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(scanReceiver, intentFilter);
((WifiManager)getSystemService(Context.WIFI_SERVICE)).startScan();
In my scanReceiver object:
public void onReceive(Context c, Intent intent) {
if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())){
mainObject.scanComplete();
}
}
And again in my main object:
public void scanComplete()
{
List<ScanResult> networkList = ((WifiManager)getSystemService.(Context.WIFI_SERVICE)).getScanResults();
for (ScanResult network : networkList)
{
<do stuff>
}
}
The code works insofar that scanComplete eventually gets called, and I can successfully enumerate all nearby wifi networks and get their SSID and BSSID, but I can't figure out how to determine their security type.
Is there a way to do this?
Thanks in advance.
I think you can find it in the source code of Settings.apk.
First you should call wifiManager.getConfiguredNetworks() or wifiManager.getScanResults(),
then use the two methods below: (find them in AccessPoint class "com.android.settings.wifi"):
static int getSecurity(WifiConfiguration config) {
if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
return SECURITY_PSK;
}
if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) ||
config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
return SECURITY_EAP;
}
return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
}
static int getSecurity(ScanResult result) {
if (result.capabilities.contains("WEP")) {
return SECURITY_WEP;
} else if (result.capabilities.contains("PSK")) {
return SECURITY_PSK;
} else if (result.capabilities.contains("EAP")) {
return SECURITY_EAP;
}
return SECURITY_NONE;
}
Hope this is helpful.
You need to parse the ScanResult's capabilities string in the scanComplete method. According to the Android developer documentation, :
ScanResult.capabilities describes the authentication, key management, and
encryption schemes supported by the access point.
You might be able to make use of -- or at the very least use as an example -- the static helper methods available in the AccessPointState class.
AccessPointState.getScanResultSecurity
AccessPointState.isEnterprise
Thanks very much,... you made my day...
I have something to add here. Without scanning for the networks, one can get the currently connected wifi configuration information (specially encryption and key management) as follows,
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> networkList = wifi.getScanResults();
if (networkList != null) {
for (ScanResult network : networkList)
{
String Capabilities = network.capabilities;
Log.w (TAG, network.SSID + " capabilities : " + Capabilities);
}
}
The security type of each network is in the Capabilities column of the scan results.
So to get the security type for you, add this to your part of your code.
public void scanComplete()
{
List<ScanResult> networkList = ((WifiManager)getSystemService.(Context.WIFI_SERVICE)).getScanResults();
for (ScanResult network : networkList)
{
String Capabilities = network.capabilities;
//Then you could add some code to check for a specific security type.
if(Capabilities.contains("WPA"))
{
// We know there is WPA encryption
}
else if(Capabilities.contains("WEP"))
{
// We know there is WEP encryption
}
else
{
// Another type of security scheme, open wifi, captive portal, etc..
}
}
}
Anyways, here's some quick source code. I would fully recomment Ayj's answer though, as it is an exceptional response and more complete.

Categories

Resources