获取所有可用的wifi
Get all available wifi
我尝试获取有关所有可用 wifi 的详细信息,但 BroadcastReceiver
扫描结果未被调用。
代码
Activity
wifiStatusActivity extends AppCompatActivity {
WifiManager wifi;
List<ScanResult> results;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false) {
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
wifi.startScan();
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
log.i("LOG","Scan Complete");
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
清单
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
任何建议...!
我找到了答案。
1) 需要添加两个新权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2) 我不知道为什么,但在我的测试设备中 Huawei honor P9 lite
位置必须处于活动状态。
我尝试获取有关所有可用 wifi 的详细信息,但 BroadcastReceiver
扫描结果未被调用。
代码
Activity
wifiStatusActivity extends AppCompatActivity {
WifiManager wifi;
List<ScanResult> results;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false) {
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
wifi.startScan();
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
log.i("LOG","Scan Complete");
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
清单
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
任何建议...!
我找到了答案。
1) 需要添加两个新权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2) 我不知道为什么,但在我的测试设备中 Huawei honor P9 lite
位置必须处于活动状态。