Is there any intent generated when the preferred network is changed on android?
Though WIFI has the higher priority when both Mobile data and WIFI are turned on.but it can be changed using this statement
connectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
However,if I query preferred network immediately after the above statement using
connectivityManager.getNetworkPreference()
I still get ConnectivityManager.TYPE_WIFI as return value.
So it seems that setNetworkPreference doesn't change preferred network immediately.
My question,
Is there any intent generated for change in network preference?
Create a broadcast receiver listening for:
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
See
Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data for an example.
Related
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.
Actually In my app I have to show the toggle button on/off if mobile data packect on/off.
But there is a problem my local boradcast can't listen mobile data packet on/off event if my WiFi is already on. Actually as I understand If my WiFi on then mobile data packet enable and disable event never affect connectivity status. Thats why my some intent filter never works.
android.net.conn.CONNECTIVITY_CHANGE
I need a intent filter which is similer to android.net.wifi.WIFI_STATE_CHANGED so that whenever mobile data packet on or off event fire my broadcast can listen it easily. And I can change my widget.
I dont know of any intent-filter that we could receive when the mobile data is turned on or off but the following code is to determine whether mobile data is turn on or off.
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected())
//mobile data is on
else
//mobile data is off
What you can do is write a service of your own that will constantly run the above code. When the mobile data is on, you can send a broadcast to your activity telling the same. Same goes when the mobile data is off.
Edit1:-
This works even if wi-fi is connected.
Edit2:- Don't forget to add ACCESS_NETWORK_STATE permission while using this
I think in your app,you need to import WifiManager Class.
WifiManagerclass provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling
Context.getSystemService(Context.WIFI_SERVICE)
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
I want to determine if a paired device is currently active. I don't really need to connect to it, just determine if it has a heartbeat.
I'm fighting a series of IO errors while trying to connect a bluetooth socket, but I'm not sure I really need to.
Assuming you are using Android
If you have a BluetoothDevice object to this device, you can register to listen for the Broadcast Actions - ACL_CONNECTED or ACL_DISCONNECTED and keep track of the connection state.
If you want to find out wether a BT-Headset is currently actively connected (audio being routed to it), do the following:
Declare the following intent-filter
<intent-filter >
<action android:name="android.bluetooth.headset.action.AUDIO_STATE_CHANGED" />
</intent-filter>
and in your Receiver in onReceive check for:
if ("android.bluetooth.headset.action.AUDIO_STATE_CHANGED".equals(intent.getAction())) {
headsetAudioState = intent.getIntExtra("android.bluetooth.headset.extra.AUDIO_STATE", -2);
}
and save the int as a static variable. Access it anytime you want to know if BT audio is connected(1) / disconnected(0). Not pretty, but gets the job done.
Also check out:
https://github.com/android/platform_frameworks_base/blob/gingerbread/core/java/android/bluetooth/BluetoothHeadset.java
How can i know if a BluetoothSocket is still connected to the endpoint? How can i detect if the socket has been disconnected by the endpoint?
Thanks
In my apps, I keep track of I/O errors. If a successful read() takes place, then I reset the counters. If the error counters go up high enough (4-5 is usually a good number) then I consider the connection dead, and proceed to tear it down and re-build it.
The SDK talks about a state change intent, but I'm not clear whether it's referring to a specific connection, or the bluetooth adapter itself here:
Optionally, your application can also
listen for the ACTION_STATE_CHANGED
broadcast Intent, which the system
will broadcast whenever the Bluetooth
state has changed. This broadcast
contains the extra fields EXTRA_STATE
and EXTRA_PREVIOUS_STATE, containing
the new and old Bluetooth states,
respectively. Possible values for
these extra fields are
STATE_TURNING_ON, STATE_ON,
STATE_TURNING_OFF, and STATE_OFF.
Listening for this broadcast can be
useful to detect changes made to the
Bluetooth state while your app is
running.