I'm trying to get MAC addres of Wifi network of Eclipse android emulator with this:
WifiManager wifiManager = (WifiManager) LoginActivity.this.getSystemService(WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
System.out.println("HI");
System.out.println(macAddress);
This is the android manifest permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When the execution arrives to
System.out.println(macAddress);
The app crashes with java nullpointerexception and close it.
What are doing wrong? I have to add wifi conectivity to emulator?
The doc about WifiManager.getConnectionInfo() states that it :
Return dynamic information about the current Wi-Fi connection, if any
is active.
This means that in this case, you'd want to check whether your WifiInfo is not null and that your device is effectively connected to a Wi-Fi network. In the case of the emulator, the MAC address is always null, so you should make sure what you're outputting is valid.
Related
I am trying to get BSSID of WiFi but i am always getting 02:00:00:00:00:00 . How to get the Wifi MAC address ?
In android Manifest -
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Code
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
Toast.makeText(UserDashboardActivity.this,"macAddress " + wifiInfo.getBSSID(),Toast.LENGTH_LONG).show();
I am dealing with the same problem. As far as I know using <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> permission requires also permission <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />. Also make sure to check if Settings -> Location is turned on in your mobile device settings, and that location permission is granted in App settings.
Last hope is to check ACCESS_FINE_LOCATION in runtime.
I have a problem banning a specific bssid. I can see there is a class called WifiNative that was in api 19 but not able to access it:https://android.googlesource.com/platform/frameworks/base/+/kitkat-release/wifi/java/android/net/wifi/WifiNative.java
Is there any other way anyone knows of to do this? Maybe via JNI? Any android system libraries that can be used? c,c++?
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
newWifiInfo = wifiManager.getConnectionInfo();
from there you can get the BSSID by doing
newWifiInfo.getBSSID()
then "blacklist" by turning off the wifi via
wifiManager.setWifiEnabled(false)
Also don't forget your permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
If you want more control over your wifi state, you can use the following library to "sniff" your connection and other things
https://github.com/pwittchen/ReactiveNetwork
When getting a WifiManager System Service like this
WifiManager mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
I have the following permissions in the Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Eclipse LogCat logs an error
"mWifiServiceMessenger == null" [Tag: WifiManager]
Everything seems to work fine nevertheless. I do get an instance of WifiManager back. But the error is logged, also regardless of the Wifi state (enabled or disabled.
I would like to understand why this error is logged and what it does imply.
This error occurs when the device has the WiFi turned off. If the device can't get the WIFI_SERVICE.
Anyway not all the devices throw this error.
In your case it is posible that you're trying to get the WiFi service and if not you're using the cellular data.
It is thrown by WifiManager:
E/WifiManager: mWifiServiceMessenger == null
I had the same problem and I found the solution. You have to add CHANGE_WIFI_STATE permission in your manifest.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
I need to find a way to get the SSID of the network that my Android device is shared (My device is being a portable wifi hotspot).
Not the SSID of the network I connected to.
thanks
I finally found that there are some hidden method about getting/setting wifiAP mode in android WifiManager class. In WifiManager class, getWifiApConfiguration could be invoke to get the configuration in which the SSID of wifiAp could be found.
I know OP has already answered his/her own question, but it wasn't too clear for me, so I'll just post my complete solution here for future people that got here:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
//or other info about the current hotspot, accessible through config
}
}
Note: you would need the following permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
How to get the mac address in the absence of the wifi!! and am using the following code to get the mac address in the presence of the wifi `public static String getMACaddress(Context ctx)
{
WifiManager wifiManager = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
{
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress();
}
`
in the else part i want to get the mac address in the normal way i mean without the internet connection.
See the below post.. In this without using of wifi connection getting mac address.may it will help you.
// AndroidManifest.xml permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
// test functions
Utils.getMACAddress("wlan0");
Utils.getMACAddress("eth0");
Utils.getIPAddress(true); // IPv4
Utils.getIPAddress(false); // IPv6
How to get IP address of the device from code?