为什么wifi连接的数量:0?

时间:2017-10-14 20:37:22

标签: java android

我想用android studio显示Wifi连接列表。我手机的wifi设置有2个连接,但我的应用程序没有看到它们。

提前致谢

当我打开调试应用程序时,我看到这句话: Wifi连接数:0

那为什么它看不到wifi连接呢? 我尝试启用wifi并禁用它时。

MainActivity.java

@data

activity_main.xml中

import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

private StringBuilder sb = new StringBuilder();
private TextView tv;
List<ScanResult> scanList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv= (TextView)findViewById(R.id.txtWifiNetworks);
    getWifiNetworksList();
}

private void getWifiNetworksList(){
    IntentFilter filter = new IntentFilter();
    filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    final WifiManager wifiManager =
            (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);;
    registerReceiver(new BroadcastReceiver(){

        @SuppressLint("UseValueOf") @Override
        public void onReceive(Context context, Intent intent) {
            sb = new StringBuilder();
            scanList = wifiManager.getScanResults();
            sb.append("\n  Number Of Wifi connections :" + " " +scanList.size()+"\n\n");
            for(int i = 0; i < scanList.size(); i++){
                sb.append(new Integer(i+1).toString() + ". ");
                sb.append((scanList.get(i)).toString());
                sb.append("\n\n");
            }

            tv.setText(sb);
        }

    },filter);
    wifiManager.startScan();}

1 个答案:

答案 0 :(得分:0)

转动设备的位置切换。从棉花糖开始,它是必须的。 确保你拥有这些权限(测试后你可能会省略其中一些权限,我从我做过的类似项目中获取了这些权限):

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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" />