I have a problem when I want to connect my device to an existing network wifi.
That is the situation:
I am looking to an existing configured network, if there is not the one I am looking for I build it and then I try to connect the phone.
When the wifi configuration is built manually using the phone interface, I can find it and then connect to it.
But when I tried to build the configuration programmaticly Android cannot connect the phone to the network.
I have got the following message : "Association request to the driver failed".
I am wondering if the association that failed is the association between the scanned network and the configurated network. BUt I don't know why and so I don't know how to manage this problem.
EDIT: And I forget to say, that my WifiConfiguration is exactly the same as the one is created manually because I get the info from the conf with some logs.
EDIT2 : Here is my wificonf (network is protected by a wep key) :
WifiConfiguration wifiConf = new WifiConfiguration();
wifiConf.SSID = "\"ssid\"";
wifiConf.wepKeys[0] = "\"password\"";
wifiConf.wepTxKeyIndex = 0;
wifiConf.hiddenSSID = false;
wifiConf.status = WifiConfiguration.Status.DISABLED;
wifiConf.priority = 40;
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);//+
wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);//+
wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
EDIT3 : I already add the permission in AndroidManifest :
uses-permission android:name="android.permission.ACCESS_WIFI_STATE"
uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"
uses-permission android:name="android.permission.CHANGE_WIFI_STATE"
I hope my problem is clear and hope too that someone could help me.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is the correct form of this setting:
wifiConfiguration.SSID = "\"" + networkSSID + "\"";
Related
I want to set ssid and password of wifi hotspot in android using flutter.
I used wifi_iot but most of it's functions are deprecated.
is there any other way to do so?
I think using method channels would be better but I need code to open hotspot from kotlin/
give me full example of kotlin code please to open hotspot and set ssid, password
val wifiManager =
applicationContext.getSystemService(Context.WIFI_SERVICE) as
WifiManager val method =
wifiManager.javaClass.getMethod("setWifiApEnabled",
WifiConfiguration::class.java, Boolean::class.javaPrimitiveType) val
wifiConfig = WifiConfiguration() wifiConfig.SSID = "MyHotSpot"
wifiConfig.preSharedKey = "mypassword"
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
method.invoke(wifiManager, wifiConfig, true)
I want to get IP address from my mobile Android.
var context = application.android.context;
var wifiMgr = context.getSystemService("wifi");
var wifiInfo = wifiMgr.getConnectionInfo();
var ip = wifiInfo.getIpAddress();
console.log('ip', ip)
The result is: JS: ip -2029999936
But in fact this is not my IP.
Can you ask me any idea?
Update:
I follow this . I have this code:
Step1. In my component add this code:
import app = require("application");
app.android.context;
constructor() {
var context = android.content.Context;
var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
var wInfo = wifiManager.getConnectionInfo();
var mac = wInfo.getMacAddress();
}
Step2.
In AndroidManifest.xml add
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Error: [ts] Cannot find name 'android'. [2304] in this line: var
context = android.content.Context;
error TS1202: Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
You should have ACCESS_WIFI_STATE permission in AndroidManifest.xml for capturing IP address.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Then all you have to is,
import * as application from 'tns-core-modules/application';
declare var android;
const wifiManager = application.android.context.getSystemService(android.content.Context.WIFI_SERVICE);
const connectionInfo = wifiManager.getConnectionInfo();
const ip = android.text.format.Formatter.formatIpAddress(connectionInfo.getIpAddress());
declare var android; is to avoid TS errors while access native apis. An alternative is to install tns-platform-declarations plugin and point the declaration files in your references.d.ts.
Regarding the Mac Address, since Android 6.0
To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.
So it doesn't seem officially supported.
I am having an issue. The app I am busy with has a strange issue at the moment. I use the following library: WiseFly which is a simple wifi manager wrapper
My app manages WiFi networks. It will add to saved networks and connects to it depending on users interaction with my app. It works perfectly on all versions of android, except for Android Oreo on my Pixel.
It times out on the following line internally on the wrapper:
getWifiManager().disconnect();
getWifiManager().enableNetwork(wifiConfiguration.networkId, true);
getWifiManager().reconnect();
The last line is the issue. And if I go into the network settings on android, I see the saved network, but will also not connect from within android. Any ideas what I could look for to fix this issue?
This is the code used to add the network:
/**
* To generate a configuration for a WPA2 network
*
* #param ssid The ssid for the WPA2 network's configuration
* #param password The password for the WPA2 network's configuration
*
* #return WifiConfiguration - The WPA2 network configuration
*/
public WifiConfiguration generateWPA2NetworkConfiguration(String ssid, String password) {
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = mSSIDUtil.convertSSIDForConfig(ssid);
wifiConfiguration.preSharedKey = "\"" + password + "\"";
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
return wifiConfiguration;
}
Any help would be greatly appreciated.
Thanks
Try this
// Allow a previously configured network to be associated with.
getWifiManager().enableNetwork(wifiConfiguration.networkId, true);
// Reconnect to the currently active access point, if we are currently disconnected.
getWifiManager().reconnect();
// wait until reconnect process is completed
Thread.sleep(3000);
I have an Android device connected to Wifi network with an IP address. I did NS lookup on my Linux computer of this IP address and verified that there is a corresponding hostname for that IP address.
I have a piece of Java code, which when run on my Windows PC does reverse DNS lookup fine (returns a hostname):
String dnSuffix;
String ipAddress = "10.228.59.217";
InetAddress inetAddr;
try {
//inetAddr = InetAddress.getLocalHost();
inetAddr = InetAddress.getByName(ipAddress);
//System.out.println("inetAddr = " + inetAddr);
Log.v(LOG_TAG, "inetAddr = " + inetAddr);
if (inetAddr != null) {
dnSuffix = inetAddr.getHostName();
//System.out.println("dnSuffix is " + dnSuffix);
Log.v(LOG_TAG,"dnSuffix is " + dnSuffix);
}
} catch (UnknownHostException e) {
//System.out.println("Error getting DN suffix: " + e.getMessage());
Log.v(LOG_TAG,"Error getting DN suffix: " + e.getMessage());
}
dnSuffix is the hostname as expected on Windows.
But on Android, it returns an IP address instead of a hostname, which indicates that it failed.
I have all the permissions that I would think I need for this in my app:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I found this documentation:
getCanonicalHostName
public String getCanonicalHostName()
Gets the fully qualified domain name for this IP address. Best effort
method, meaning we may not be able to return the FQDN depending on the
underlying system configuration. If there is a security manager, this
method first calls its checkConnect method with the hostname and -1 as
its arguments to see if the calling code is allowed to know the
hostname for this IP address, i.e., to connect to the host. If the
operation is not allowed, it will return the textual representation of
the IP address.
Returns:
the fully qualified domain name for this IP address, or if the
operation is not allowed by the security check, the textual
representation of the IP address.
Since:
1.4
See Also:
SecurityManager.checkConnect(java.lang.String, int)
I also found someone having similar problem:
getCanonicalHostName returns an IP address
But no resolution.
Can anyone provide any help at all?
I'm really not sure it can works at all, following 2.3 sources (shortened)
inetAddr = InetAddress.getByName(ipAddress);
getByName(numeric) -> getAllByName/Impl(numeric) -> lookupHostByName(numeric)
return bytesToInetAddresses(getaddrinfo(host), host)[0]
host is the numeric string, then calling getHostName() on the resulting object will return host (the ip)
I have written code for adding open network to wifi configured list.It adds the open network to configured lists and displays the same SSID's in Wifi Settings.but it adds the same network with same name extra but it doesn't shows any open network When i press on the second on alert shows with Security WEP the following text i observed with those same networks
1) Open network
2) remembered , not in range
But i want to add open network into my list, why this extra one is added and if i connect the same network it is trying to connect to the (2) one programmatically.Actually I changed secured network to open network for this trial.It displays with open network text and when i press on that one it obtains the address and connects succesfully manually.Why this extra one is adding how can i add the open network to my list.For reference plz see the link for image.
http://www.freeimagehosting.net/uploads/3dbccfc2bd.png
Code snippet :
String hotSpotSsid = hotSpot.SSID;
String hotSpotBssid = hotSpot.BSSID;
Log.i(TAG,"in RSSI Changed Acion SSID: "+hotSpotSsid+" BSSID: "+hotSpotBssid);
StringBuffer sBuf = new StringBuffer("\"");
sBuf.append(hotSpotSsid+"\"");
hotSpotSsid = sBuf.toString();
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = hotSpotSsid;
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfiguration.BSSID = hotSpotBssid;
wifiConfiguration.hiddenSSID = false;
// wifiConfiguration.priority = 1;
// add this to the configured networks
int inetId = wifiManager.addNetwork(wifiConfiguration);
Log.i(TAG,"INetId :"+inetId);
configs = wifiManager.getConfiguredNetworks();
Log.e(TAG,"After adding config :"+configs);
if(inetId < 0) {
Log.i(TAG,"Unable to add network configuration for SSID: "+hotSpotSsid);
return;
}else {
message="\t Successfully added to configured Networks";
Log.i(TAG,message);
}
regards,
Rajendar
Try removing SSID and seeing if it works. I was having a similar issue and that worked for me.
Appending and prepending quotes to the SSID, as you did, should work though. Not sure why it doesn't.
Give SSID as wifiConfiguration.SSID = "\"".concat(SSID_NAME).concat("\"");