WifiManager not working on API 29 and higher using Android Studio - java

So I tried to use the code for wifi manager to disable/enable wifi, my app's target device are for API 29 and higher only. I just found out that WifiManager only works for older version. Is there other way on how to do it that will work on higher versions?
For reference only:
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

setWifiEnabled is deprecated in Android 10+ due to security reasons
This method was deprecated in API level 29.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
now your only way is to pass user to Settings GUI, in which Wi-Fi may be enabled manually only
Intent settingsIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
startActivityForResult(settingsIntent);

Related

Can't enable Wifi programmatically on some devices

I have this code:
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
Is it correct that this code won't properly working on absolutly any device?
Are there any ways to acheive it?
From the docs
This method was deprecated in API level 29.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect. If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
Also make sure you have the requested the permission Manifest.permission.CHANGE_WIFI_STATE
It should work for any device provided that the app manifest has the following permissions:
<manifest>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
</manifest>

Hey , I am just started to code in android and getting error Permission Denial: not allowed to send broadcast in android

permission denial
My activity code is
boolean isEnable= Settings.System.getInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON,0)==1;
Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnable?0:1);
intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state",!isEnable);
sendBroadcast(intent);
That is because ordinary SDK apps cannot send ACTION_AIRPLANE_MODE_CHANGED broadcasts. If you look at the documentation for that Intent action, you will see:
This is a protected intent that can only be sent by the system.
Also note that your AIRPLANE_MODE_ON stuff may not work on API Level 17 (Android 4.2) or higher devices.
And, note that ordinary SDK apps cannot hold the WRITE_SETTINGS permission. If you are building your own custom Android ROM, you could have apps that hold that permission in that ROM.

FileObserver does not work on external storage in Android 6.0 Marshmallow (API 23)

I have an app that observes a public directory on external storage with FileObserver.
It works fine on Lollipop devices. I want to add support for Marshmallow, so I set up a Nexus 9 tablet with it.
On the Marshmallow device, it fails, on Lollipop device it's OK.
On Marshmallow device, the FileObserver does not react to file system events that are caused by other processes. E.g. taking a screenshot, creating files via adb shell.
It works fine if the files are created by my app.
On Marshmallow, I ask for WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions from Manifest.xml.
When I compile with API 23, I also make sure that I call ActivityCompat.requestPermissions().
When I compile with API 22, I just rely on Manifest.xml.
It works if I observe stuff on /data/data/my-package-name.
It fails if I observe stuff on /storage/emulated/0/Pictures/Screenshots.
Did anybody test FileObserver considreing all of the following? :
Marshmallow device
API 23 and API 22?
external storage
files created by some other process than the observing app (e.g. adb or taking screenshot).
This appears to be a bug in Marshmallow, see this link.
This is not fixed even in Nougat, you could get rid of the API this whole FileObserver, as it makes completely useless the fact that on most of the devices, it will not work.

Why does WiFiP2P need internet permission?

Directly quoting the android website :
Wi-Fi peer-to-peer (P2P) allows Android 4.0 (API level 14) or later
devices with the appropriate hardware to connect directly to each
other via Wi-Fi without an intermediate access point
But according to the android website, for you to use the WiFiP2P class you have to have the following permission to your application's manifest file.
<uses-permission android:name="android.permission.INTERNET" />
My question is: If WiFiP2P connects 2 android devices directly then why does it need the internet permission?
Because, as described in the docs, the android.permission.INTERNET permission actually...
Allows applications to open network sockets.
No matter if the connection is intended to access the web or not, when one device connects to another external entity (like another Android device), it always uses network sockets.

On Android 2.2: Being in Tethering mode and scanning wifi at the SAME time

Hi: Is it generally possible to have Wifi Tethering turned on (creating a Hotspot) and at the same time scanning for Wifi devices? I want several devices just discover (NOT connect) each other and log the signal strength. To do that all devices must be discoverable over Wifi and at the same time discover others.
I would just need that for some specific use case..
seems that you cannot do this by default way in android: if you have the wifi tethering enabled you cant use any wifi connectivity features (scan included). This is an exclusive functionality: tether or wifi client.
The WifiManager seems to be in WIFI_UNKNOW state when you have the tethering enabled.
I'm also investigating on this but I can't find any solution till now.
These are from the latest Froyo API (this method is marked as #hide) but i think you can access it throught reflection, I hope.
public boolean setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled)
Start AccessPoint mode with the specified configuration. If the radio is already running in AP mode, update the new configuration Note that starting in access point mode disables station mode operation
As you can see the AP mode will disable station mode operation (scanning etc)
I just tried it: you can use reflection and invoke the method to start the WifiAP.
Then if you call the method startScan() on the WifiManager you will get a false response so the scan doesn't work with the WifiAP enabled.
Marco

Categories