To check whether the wifi scan is complete - android

I'm writing a code for getting the information of all the access points in the range continuously.
Here is my code:
myRunnable = new Runnable() {
#Override public void run() {
while(){
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.startScan();
List<ScanResult>results= wifi.getScanResults();
try{
//data Logging
}
}
}
};
myThread = new Thread(myRunnable);
myThread.start();
As the scanning and logging of data continuous repeatedly, i want to check whether the scan is complete before logging data.
Is there any flag or function which checks for wifi.startScan() is complete or not , before logging the data.
Could you please help me out with a code.

i want to check whether the scan is complete before logging data. Is
there any flag or function which checks for wifi.startScan() is
complete or not , before logging the data. Could you please help me
out with a code.
Yes, there is mechanizm designated for this purpose. To reach your goal you need to implement BroadcastReceiver that will listen for WiFi scans.
All what you need is to create BroadcastReceiver with appropriate IntentFilter. So you need this action for filter:
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
This action means that an access point scan has completed, and results are available. And then just create BroadcastReceiver (statically or dynamically, it's doesn't matter).
If you don't know how to start, look at this tutorial:
Android BroadcastReceiver Tutorial

To build up on the answers above, I just wanted to add the related code to give an example.
1. Registering for the event
// don't forget to set permission to manifest
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
context.registerReceiver(this,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
//check if WIFI is enabled and whether scanning launched
if(!wifiManager.isWifiEnabled() || !wifiManager.startScan())
{
throw new Exception("WIFI is not enabled");
}
2. Receiving the result
To receive, the object registered (in this case this) needs to extend BroadcastReceiver and implement the following method:
public void onReceive(Context context, Intent intent) {
List<ScanResult> result = wifiManager.getScanResults();
}

You need to implement a BroadcastReceiver listening for the scan results returned from WifiManager.startScan(). onReceive() allows you to access the scan results directly. It takes about 1 second for the scan to complete and trigger onReceive()...

Related

Issue handling lost Bluetooth connections using BroadcastReceiver when connected to two devices

I have the following code to handle lost Bluetooth connections.
public class BluetoothReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//call method to cancel connection thread
}
}
}
However, I have another app that uses Bluetooth connected to a different device running in the background. If I lose connection to that device, I also lose the connection to the device within this app.
I was wondering, is there any way to prevent this?
As described here you are able to get the device instance from the intent with intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) If the device is NOT the one you are interested in, just DO NOT call:
//call method to cancel connection thread

Android: Continuously seek for a specific WIFI Connection since boot completed

How can I check if a specific wifi connection is availible say for example SSID: Saaram01 is available. Whenever it is available I get notified.
I have done all this work through a button, like if you click the button it notifies if Saaram01 is available overwise does nothing.
The problem or the question basically is how can I check for this SSID availability everything 24/7.. Obviously for this I cant use a background service.. So is there anyother possible way to do it ?? Like using broadcast reciever or anything else ?
Any help will be highly appreciated !
Thank You.
IntentFilter i = new IntentFilter();
i.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
// TODO Auto-generated method stub
ScanWiFiActivity a = ScanWiFiActivity.instance();
WifiManager w = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> l = w.getScanResults();
a.Clear();
for (ScanResult r : l) {
if(r.SSID.equals("saaram01") {
//Perform your logic here
}
}
}
};
registerReceiver(receiver, i);
n the FOR block work your magic and take action when you identify your network by SSID or BSSID

Application never receives RSSI_CHANGED_ACTION

I am new to Android programming and am trying to understand the concept of BroadcastReceivers. In order to help myself, I am just trying to write a small application that monitors Wifi signal strength.
Now, from my understanding I can simply wait to receive the RSSI_CHANGED_ACTION broadcasted by the system. The RSSI should change frequently which means I should be receiving this notification frequently...however, never do I receive it once. I have watered my code down to the bare minimum so it just logs a message when the notification is received.
public class RssiActivity extends Activity {
public BroadcastReceiver rssiReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d("Rssi", "RSSI changed");
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onResume() {
super.onResume();
registerReceiver(rssiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
Log.d("Rssi", "Registered");
}
#Override
public void onPause() {
super.onPause();
unregisterReceiver(rssiReceiver);
Log.d("Rssi", "Unregistered");
}
}
I have already seen this post Android: How to monitor WiFi signal strength and it doesn't seem to help me. I have also tried the code sample here http://android-er.blogspot.com/2011/01/check-rssi-by-monitoring-of.html and it never updated the RSSI value either. I'm quite confused as to why this is. Any help you can give me would be greatly appreciated. Thanks!
So, I had the same problem that you did, wanting to an updated RSSI value as the user walked around, etc, and I could not solve it using RSSI_CHANGED_ACTION.
Like the issue you're having, my callback would not be called correctly. Strangely, it was only called once, when the activity was created, and then never again.
My Workaround
In your onCreate(), register a callback for SCAN_RESULTS_AVAILABLE_ACTION. Then call WifiManager.startScan().
Now, in your callback, do:
WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
int newRssi = wifiMan.getConnectionInfo().getRssi();
wifiMan.startScan();
Now you have a loop, where the callback initiates a scan, receives the results, and initiates another scan.
It's gross and will suck a lot of power, however, you can watch the RSSI values change as you walk around.
Full Code
(note that I use onResume and onPause to register and unregister, so it will only scan repeatedly, e.g. waste battery, when the activity is onscreen)
#Override
public void onResume() {
super.onResume();
//Note: Not using RSSI_CHANGED_ACTION because it never calls me back.
IntentFilter rssiFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
this.registerReceiver(myRssiChangeReceiver, rssiFilter);
WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
}
#Override
public void onPause() {
super.onPause();
this.unregisterReceiver(myRssiChangeReceiver);
}
/**
* Broadcast receiver to update
*/
private BroadcastReceiver myRssiChangeReceiver
= new BroadcastReceiver(){
#Override
public void onReceive(Context arg0, Intent arg1) {
WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
int newRssi = wifiMan.getConnectionInfo().getRssi();
Toast.makeText(getActivity(), ""+newRssi, Toast.LENGTH_SHORT).show();
}};
Sorry I'm so late, I just had to find out I had to solve your problem :P
WifiManager.RSSI_CHANGED_ACTION is triggered when the RSSI levels change. I.E. you lose or win a wifi bar. It does not happen that often.
My guess is it's sticky so it triggers when registered.
As said, the best way I found to solve the problem is through WifiManager.SCAN_RESULTS_AVAILABLE_ACTION .
are you shure that it has to trigger (meaning are you shure the signal strength is changing)? have you read the BroadcastReciever Dokumentation?
Note: If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack.
Try to register your reciever inside of your AndroidManifest.

Listening for Bluetooth Status

I'm writing a code that listens for the Bluetooth device to become disconnected, then does something. How would I go about doing this? I'm not exactly sure what I want to put after it yet, figured I'd get this sorted out first. Hopefully I wasn't completely wrong with this code, as I'm new to developing. This is what I have so far:
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action))
{ // This will be the followup action, once I figure out what I want it to be.
First, use the BluetoothChat example to start with as coding with Bluetooth is pretty complex and it's nice to have working code. What you want to look at is handleMessage() in BluetoothChat.java, and you'll need to save off the state. When the state changes from BluetoothChatService.STATE_CONNECTED to BluetoothChatService.STATE_NONE, the device has become disconnected.

Intent action for new network added (Android)

WiFiManager has an addNetwork(wifiConfiguration) method which allows to programmatically add a new network. This is also performed behind the scenes by the system whenever the user/native-manager tries to connect to a new access point and I want to listen to this event.
I tried:
IntentFilter myStateChanged = new IntentFilter();
myStateChanged.addAction( WifiManager.NETWORK_IDS_CHANGED_ACTION );
ctx.registerReceiver(myStateChgRcvr, myStateChanged);
But in my broadcast receiver what do I do with the intent object to get the desired info? I want to ascertain that this is indeed a case of a new network profile added and I want to get that network's info:
private BroadcastReceiver myStateChgRcvr = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent i) {
i.getParcelableExtra(...)); //???
}
};
Many thanks,
Spitzer
But in my broadcast receiver what do I do with the intent object to get the desired info?
Nothing. There are no documented Intent extras on that Intent.
I want to ascertain that this is indeed a case of a new network profile added and I want to get that network's info:
Call getConfiguredNetworks() on WifiManager and see if anything has changed, I guess.

Categories

Resources