Can't enable Wifi programmatically on some devices - java

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>

Related

WifiManager not working on API 29 and higher using Android Studio

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);

BluetoothAdapter getAddress() return 02:00:00:00:00:00

I try to return the Bluetooth mac address of my device but this is not working and return this address : 02:00:00:00:00:00 .
-I'm using API Level 28
-I can get my Bluetooth mac address in my android system settings but not programmatically in my app.
-I can get the mac address of this device programmatically from other devices.
What I understand is that access to address mac is only for system applications ,but if It's true why other devices get my mac address and not me !
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
test.setText(adapter.getAdress());
I try many permissions but still the same problem !
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
<uses-permission android:name="android.permission.INTERNET" />
Since Android 6, Marshmallow apps do not have access anymore to the bluetooth hardware MAC address. This change was made because the address was abused by many apps to identify and track smartphones. To avoid that existing apps are broken the getter method still exists but it returns a dummy address. For details see the corresponding note within Android's official release notes here
But what about visibility of the MAC address to other devices?
Bluetooth Low Energy (LE) does not use the hardware MAC address for communication. It uses a randomized variant of it (see here
for details). Since Bluetooth LE does not stop scanning for nearby devices in background even when the user explicitly disables Bluetooth this feature
is considerable ;-)
Bluetooth classic does not randomize the MAC address. But in constrast to Bluetooth LE the user can at least easily and fully disable Bluetooth.

Oracle MAF and requesting permissions in Android 6.0 (API level 23)

I have an MAF application that uses the devices camera to take a picture. Everything was working fine until I tested it on an S6 running API 23, where it crashed with:
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=3432, uid=10060 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
As far as I can tell this is because of the way Android are handling permission in the newer API's, noted here:
https://developer.android.com/training/permissions/requesting.html
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
So I followed the instructions on how to request permission during run-time (I've installed Android Support Libraries), the problem is, the method signature looks like this:
int checkSelfPermission (Context context,
String permission)
I cannot find a way to get a reference to the context (the example in the provided link also uses an Activity, same problem there). I just don't have the faintest clue how to do this from within MAF, as it seems Application/Activity objects are not exposed.
I have tried simply extending Activity in one of my classes and attempted to call the checkSelfPermission method with this but then I get run-time errors, after reading up on what an Activity is I understand why this is wrong.
tl:dr
How do we handle the new Android 6 (API level 23) run-time permission requests with oracle's mobile application framework?
Edit:
So as per the discussion in the comments we can see the app is supposed to still function normally on Android 6, even if you're not requesting permissions at run-time.
So I had a look at the AndroidManifest.xml file that MAF is generating on build, and I can see that the permission (from the above exception) android.permission.READ_EXTERNAL_STORAGE is not present:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.bsaf.atlas.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="com.bsaf.atlas.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET"/>
Also interesting, is that the GPS location does not update either, and the log is constantly posting this:
07-07 02:07:13.089 2174 2174 E LocationProvider: Caught security exception registering for location updates from system. This should only happen in DumpRenderTree.
So my next question is, how do I influence AndroidManifest.xml within MAF? And why is this struggling with location updates?
UPDATE (22/08.2016):
This was actually happening because I was setting the target SDK to 23 in my build configurations. Downloading the new Eclipse simply reset this back to the default (21)
Old Answer:
I solved this by downloading OEPE for Neon (I was using OEPE for Mars).
Oracle Enterprise Pack for Eclipse
The application now correctly generates a popup requesting permission to use the camera.
Please note, this has created an issue with location services and the app crashes when requesting gps location, I have not yet discovered a fix or the source of the problem.

java.lang.SecurityException: BLUETOOTH permission crash. (Samsung devices only)

I'm getting the following crash, but this only occurs on some Samsung devices (not all, we develop/test on Samsung devices)
Crash log:
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10025 nor current process has android.permission.BLUETOOTH.
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.bluetooth.IBluetooth$Stub$Proxy.isEnabled(IBluetooth.java:1156)
at android.bluetooth.BluetoothAdapter.isEnabled(BluetoothAdapter.java:697)
at org.altbeacon.beacon.service.scanner.CycledLeScanner.finishScanCycle(CycledLeScanner.java:245)
at org.altbeacon.beacon.service.scanner.CycledLeScanner.scheduleScanCycleStop(CycledLeScanner.java:234)
at org.altbeacon.beacon.service.scanner.CycledLeScanner$1.run(CycledLeScanner.java:230)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Here's my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.myapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!--
The following two permissions are not required to use Google Maps Android API v2,
but are recommended. We also require them for location updates.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name=".permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name=".permission.C2D_MESSAGE" />
<application
android:name=".myapp"
android:allowBackup="true"
android:icon="#drawable/appicon"
android:label="#string/app_name"
android:logo="#drawable/action_bar_pinwheel_back"
android:theme="#style/AppTheme" >
<activity
android:name=".activities.VerifyPinActivity"
android:label="#string/title_activity_verify_pin"
android:screenOrientation="portrait"
android:theme="#style/NoActionBarDark"
android:windowSoftInputMode="stateVisible|adjustPan" >
</activity>
...(more activities here removed)
</application>
</manifest>
I really have no idea why it only occurs on a few devices; my initial guess is it has something to do with Samsung KNOX, and the (few) users that are crashing are KNOX users with a phone policy to disable Bluetooth.
Has anyone else run into this? Or does anyone have any idea what this might be?
If you are sure the users met this problems only on Samsung KNOX,the following answer maybe will help you.
Thinking step by step is the way to solve your problem.
1.This only occurs on some Samsung devices.
So, it has nothing to do with your code,also the permission.There is
something wrong with the Samsung devices.
2.Check the crash log:
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10025 nor current process has android.permission.BLUETOOTH.
According to the experience,if you have met the log like "Neither user
**** nor current process has *** permission.",this means that your app doesn't have the permission to do something in the system.This occurs
often on that you do something need the system level permission.
If you are installed as system level app together with customized Android ROM,or you signed your app with the platform.x509.pem and platform.pk8 file of the customized Android ROM,and then you can have the permission.
Now check what is Samsung KNOX:
Samsung Knox (stylized Samsung KNOX) is an enterprise mobile security solution.
Bluetooth is one kind of data transmission way to the other's
device.This will cause the security problem.
Check the offical website:
https://www.samsungknox.com
Search the key words 'Bluetooth',and we found this:
https://www.samsungknox.com/en/faq/are-bluetooth-bluetooth-low-energy-and-nfc-available-inside-knox-container
KNOX 2.4+ — Bluetooth, Bluetooth Low Energy (GATT), and NFC are supported inside the KNOX container.
KNOX 2.3 or below — Bluetooth, Bluetooth GATT, and NFC aren't supported inside the KNOX container. If the device is connected to a peripheral (e.g. headphones) in the personal space, Bluetooth will remain connected, even if the user enters the KNOX container. Apps inside the KNOX container can't initiate a Bluetooth connection.
1.If your users are using KNOX 2.3 or below,and they can update their OS of device to KNOX 2.4+.tell your users who use Samsung
KNOX update their OS of device.
2.If your users are using KNOX 2.3 or below,and they can't update their OS of device to KNOX 2.4+.your app will not work under this
situation,the problem is not the problem,what you can do is only tell
uses that:the OS of your device doesn't support Bluetooth,so it's not
working.
3.If the above will not help you, you can contact with Samsung KNOX Official
Another tip:
To improve your user experience of your app, you need to prevent your app to be crashed.
Crashed is the worst user experience.
If you can not solve the problem,you can do this:
try{
You code related with Bluetooth.
}catch (Exception e)
{
}
Remember:The offical always helps you to solve the problems better!
You can look here. I'd guess this is your problem.
Are Bluetooth, Bluetooth Low Energy, and NFC available inside the KNOX container?
KNOX 2.4+ — Bluetooth, Bluetooth Low Energy (GATT), and NFC are supported inside the KNOX container.
KNOX 2.3 or below — Bluetooth, Bluetooth GATT, and NFC aren't supported inside the KNOX container. If the device is connected to a peripheral (e.g. headphones) in the personal space, Bluetooth will remain connected, even if the user enters the KNOX container. Apps inside the KNOX container can't initiate a Bluetooth connection.
I never had any hands-on experience with this, but I hope this helps.
try to add below to your manifest
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />
From SDK 23 onward you need to check the permission for bluetooth in case required.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (mContext.checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED) {
//TODO - Do the work
}
}
From KNOX Premium v1.1 onwards, Samsung no longer needs to sign third-party app (APK) files so that they can run inside KNOX containers. Instead, the appropriate permissions are granted at runtime, after an app activates its ELM and KLM licenses successfully. for more info see this

Android wifi p2p unsupported (wifidirect)

i'm using the protocol for making p2p connection using wifi in android (http://developer.android.com/reference/android/net/wifi/p2p/package-summary.html)
I have a problem calling the discoverPeers() function :
ActionListener.onFailure(int reason) is called (its an interface to trigger some events) and i receive reason=1, which means 'P2P_UNSUPPORTED'.
I added all the permissions required and the (use... wifidirect) tag.
i'm using a virtual device and the latest android sdk(4.0.3),
i have an internet connection on this device.
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.wifi.direct" android:required="true"/>
As far as I know you can't run the demo in the emulator because it doesn't support real Wifi.
You should launch the demo on a real device like the Galaxy Nexus which supports ICS and Wifi-Direct.
facepalm Yea...I was running on an emulator instead of the real deal...works fine if you use a device that ACTUALLY has Wifi...ha!

Categories