Can I force android to recheck connection if it's lost? - android

Can I from my application force android to reconnect to cellular network immediately?
If yes how to do this?

Apparently the functionality as of Android 5 has been moved to system apps only. How to enable mobile data on/off programmatically
But you can still register a listener for network changes.
https://developer.android.com/training/basics/network-ops/reading-network-state
When you get the onLost() event you can start a Wireless Setting Activity so the user can do it manually.
//As seen in the link above.
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
This will be a common occurrence so get use to it. For example you can createBond() for a Bluetooth device, but you can't removeBond() anymore. Thus you have to send the user to the Bluetooth setting activity for the user to
"forget" about the device.

Related

Android - turn GPS on or off programmatically

Why we need to go setting for on/off GPS on the other hand we can on/off WIFI and Bluetooth programmatically without move to settings.
Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0.
There used to be a way to enable / disable GPS programmatically by sending the android.location.GPS_ENABLED_CHANGE broadcast:
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", enabled);
sendBroadcast(intent);
where enabled would be true or false respectively.
If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions.
Now the answer to your question
Why we need to go setting for on/off GPS on the other hand we can
on/off WIFI and Bluetooth programmatically without move to settings ?
Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. A lot of people are very sensitive about things like real-time location monitoring . That's why Google made it mandatory to get the user's consent before using the GPS function. The following dialog is seen whenever the user turns GPS on:
And hence it is no longer possible to programmatically change the GPS settings, as by necessity it requires the user's permission. What the programmer can do is direct the user to the GPS settings by calling
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
and let the user make a choice.
As an interesting point, if you try sending the GPS_ENABLED_CHANGE broadcast on the new OS versions, you get a
java.lang.SecurityException: Permission Denial:
not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
error. As you can see, its a SecurityException with a permission denial message.
The premise of your question is no longer correct. With Google Play Services 7, you can display a dialog to change the location provider settings from within your app. Jump to 1:10 in this video.

Permanently disable mobile data connections in Android

How can I permanently disable mobile data connections in Android?
I know I can turn this on and off in the menu, but what I want is to permanently make it impossible.for anyone to turn this on, for example.by deleting a necessary file (similar to deleting IO80211Family.kext on a Mac).
At the same time, I want to retain the possibility to connect via WLAN.
Please provide an idiot level explanation.
"Please provide an idiot level explanation." I can not do this, because SO is not allowing it.
About solution to your problem,
Step-1 : Register receiver with android.net.conn.CONNECTIVITY_CHANGE action in manifest.
Step-2 : onReceive method of broadcastreceiver, check for the type of internet connection Data connection / WLAN.
Ref : Android: Internet connectivity change listener
Step-3 : Disable data connection programmatically
Ref : Enable/disable data connection in android programmatically

How can I avoid or dismiss Android's Bluetooth pairing notification when I am doing programmatic pairing?

I have an app where I am programmatically controlling Bluetooth pairing and unpairing. I can pair before connection and unpair afterwards. The reason I need to do this is specific to my application and not in the scope of my question.
Basically what I am doing is:
Get a reference ib to IBluetooth object as described in this answer
Register a BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
Call ib.createBond(address)
Wait for BroadcastReceiver to trigger
Convert user pin into bytes with convertPinToBytes()
Call ib.setPin(address, pinBytes) from within BroadcastReceiver
Anyways, this approach works great, except for the fact that when I do the pairing, I get a notification in the Status bar requesting that the user enter a PIN to complete the pairing. But this is in fact unnecessary, because by the time the user sees this, my app has already used setPin(). I'd really like for that notification to either a) not appear at all, or b) be dismissed automatically somehow.
I realize this may not even be possible, but I thought I would ask in case someone has a creative idea.
Try setting the confirmation first in the PAIRING_REQUEST
BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput").invoke(device);
This worked for me between two Android devices using RFCOMM but I'm not entering any PINs
Since Android API 19 Google switched these Methods to public Methods, so there is no need for Reflection any more. :)
Do this in the PAIRING_REQUEST notification event:
BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(paramBluetoothDevice, null)).booleanValue();
But you gotta tell me how did you pair your remote device without the user to enter Passkey/PIN? off course, you know the PIN for the remote device which is trying to pair to your device but how did you provide that PIN to the remote device.

Android bluetooth: how to poll for connections/disconnections

I need to continuously poll for getting bluetooth devices connections/disconnections in my activity to update a listView with the currenctly available devices.
I use btAdapter.startDiscovery() but it is not permanent ... how can i correctly get the on/off events for the devices?
I would suggest using a broadcastreceiver to listen for the specific events you are talking about. You could even fire off another discovery mode after it comes out of its current discovery mode to have it keep scanning
BluetoothAdapter.ACTION_DISCOVERY_FINISHED
BluetoothDevice.ACTION_ACL_CONNECTED
BluetoothDevice.ACTION_ACL_DISCONNECTED
You can use the intent extra's to be able to get the name (ect) from the device that connects
I would read http://developer.android.com/guide/topics/wireless/bluetooth.html and
http://developer.android.com/reference/android/content/BroadcastReceiver.html

android access point/hot spot doubt

I am building an app which requires an access point to be enabled throughout... how can i get an interrupt or notification whenever the access point gets disabled...?
Do you need a WIFI connection or any connection at all? This will trigger when wifi drops:
SUPPLICANT_CONNECTION_CHANGE_ACTION

Categories

Resources