问题
I'm trying to display the signal strength of the connected WiFi network in my Android app. I've tried the following:
//Receiver for information on the network info
private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("INFO", "onReceive()");
List<ScanResult> scanResult = mWifi.getScanResults();
for(ScanResult scan : scanResult)
{
Log.d("INFO","Network strength: " + String.valueOf(scan.level) + " dBm " + scan.SSID);
}
}
};
And then I do the registering/unregistering of my receiver on the onResume()/onPause():
@Override
public void onResume()
{
super.onResume();
mActivity.registerReceiver(mNetworkReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); //network info
}
@Override
public void onPause()
{
super.onPause();
mActivity.unregisterReceiver(mNetworkReceiver);
}
But this doesn't seem to work. I also have the ACCESS_WIFI_STATE
permission enabled. My receiver is only called if I call <WiFiManager>.startScan()
manually, but I'm trying to get it to work with my broadcast receiver so that I can always show the signal strength whenever it changes. I noticed that the onReceive()
provides a list which is unnecessary if I just want the connected network's stats. Any way to change that too?
edit: Also, I realize that there are some related questions on this, but the ones I've read weren't too helpful.
edit2: Clarified my question a bit. The main problem is that my receiver is never called unless I call startScan()
.
回答1:
well this code is working fine with me to inform WiFi signal strength when it changes using toast, so
My Receiver is like this
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = wifiDemo.wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d("Debug", "onReceive() message: " + message);
}
this is what in my start-up activity to register Receiver and get Wifi Info on TextView
public class WiFiDemo extends Activity {
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
@Override
public void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
i put scroll view on TextView in XML layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textStatus"
android:text="WiFi Connections :" />
</ScrollView>
</LinearLayout>
and finally my manifest is look like this
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>
<activity android:name=".WiFiDemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".WiFiScanReceiver">
<intent-filter>
<action android:name="com.rdc" />
</intent-filter>
</receiver>
still have any trouble let me know!!
回答2:
Do this in your reciver--
final List<ScanResult> results = _wifiManager.getScanResults();
//do it to get connected info..
// final WifiInfo wifiInfo = _wifiManager.getConnectionInfo();
// int connectedRssi= wifiInfo.getRssi();
if (results != null) {
for (final ScanResult result : results) {
int rssi=result.level;
}
scanResult(rssi);
}
Make an interface for scan Result and call it in your activity whereever you want to display it...
public void scanResult(int rssi){
_rssiText.setText(rssi);
}
Manifest permissions--
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Hope so this will help you...
回答3:
If you want just to monitor the current WiFi signal strength you may use IntentFilter
with WifiManager.RSSI_CHANGED_ACTION
, not the WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
来源:https://stackoverflow.com/questions/11389492/get-wifi-signal-strength-of-connected-network