What does "mWifiServiceMessenger == null" exception in LogCat imply? - android

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"/>

Related

getBSSID() in android studio

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.

WifiP2pManager null

I'm getting mad, i'm following this tutorial on Wifi peer to peer :
https://developer.android.com/guide/topics/connectivity/wifip2p.html
To find the error, I tried many things in onCreate() in my activity and my last test was just that :
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
manager.toString();
I found out that my manager is always null. I don't understand why, my API's level is correct and I don't find any problems about WifiP2pManager and getSystemService on Internet. What I missed ?
AndroidManifest.xml
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
I had the same problem. Each time I ran the code through the emulator I received an error message that the object was null, but when I ran the code in my Nexus (real device) there was no error and the object is not null. In my opinion the reason is that the emulator does not support p2p and the real device yes. Hope I helped.

Get MAC Address Android Crashes APP

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.

WiFi state is not enabling

I am trying to create a widget for enabling and disabling the wifi.
if(myWifiManager.isWifiEnabled()){
System.out.println("Toggle Wifi Enabled going to disable");
myWifiManager.setWifiEnabled(false);
}
else{
System.out.println("Wifi Disabled going to enable ");
myWifiManager.setWifiEnabled(true);
System.out.println("WI: "+myWifiManager.isWifiEnabled());
}
This is the code i am using the disabling part is working fine but the enabling part is not working fine. Soon after enabling the wifi i am printing the wifi state i am getting it as false.
Here is how to turn on and turn off wifi in android.
First you need to declare the following in your manifest file
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
After doing it that on your Activity class
private WifiManager wifiManager;
#Override
public void onCreate(Bundle icicle) {
....................
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(wifiManager.isWifiEnabled()){
wifiManager.setWifiEnabled(false);
}else{
wifiManager.setWifiEnabled(true);
}
}
Explanation
Get the Wifi service from our system
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
Check the our wifi is currently turned on or turned off
if(wifiManager.isWifiEnabled()){
Turn on/off our wifi
wifiManager.setWifiEnabled();
Reference
WifiEnabler
http://google-androidlovers.blogspot.com/2012/01/scan-for-wireless-networks-in-android.html
http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm
Deprecated: Starting with Build.VERSION_CODES#Q, applications are not
allowed to enable/disable Wi-Fi. Compatibility Note: For applications
targeting android.os.Build.VERSION_CODES#Q or above, this API will
always fail and return false. If apps are targeting an older SDK
(android.os.Build.VERSION_CODES#P or below), they can continue to use
this API.
(Source)
So this will only work for devices with android Pie and below.
Download this example it is what you want
https://github.com/siddhpuraamitr/WIfi-Toggle-Widget

Enabled using coding for use wireless network

I am new to android i stuck for using coding enable location and sectury in that use wireless network enabled so please tell mi how it's possible
thanks
priya naral
From what I gathered, you want to programmatically turn on Wi-Fi.
If so, first you need to add these to your AndroidManifest.xml:
<application>
.
.
.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
</application>
Then you have the ability to use WifiManager, so your code would look something like the following, where this is your application Context:
WifiManager manager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if(!manager.isWifiEnabled()){
manager.setWifiEnabled(true);
}

Categories

Resources