我正在尝试创建一个返回可用Wi-Fi网络的代码。但我遇到了一个问题。 getScanResults返回为空,但我已经给出了所有可能的网络权限。
public class MainActivity extends Activity {
private static final String TAG = "My Activity";
private StringBuilder sb = new StringBuilder();
private TextView tv;
List<ScanResult> scanList;
@Override
protected void onCreate(Bundle savedInstanceState) {
final String CoarseLocation = Manifest.permission.ACCESS_COARSE_LOCATION;
final String AccessWifi = Manifest.permission.ACCESS_WIFI_STATE;
final String ChangeWifi = Manifest.permission.CHANGE_WIFI_STATE;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView2);
Button btn = (Button) findViewById(R. id. button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (checkSelfPermission(CoarseLocation) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 123);
}
if (checkSelfPermission(AccessWifi) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE}, 123);
}
if (checkSelfPermission(ChangeWifi) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE}, 123);
}
LocationManager lman = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean network_enabled = false;
try
{
network_enabled = lman.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
if (!network_enabled)
{
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
final WifiManager mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
mWifiManager.setWifiEnabled(true);
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = mWifiManager.getScanResults();
final int Amount = results.size();
Log.v(TAG, "Wifi Scan Results Count: " + Amount);
int num = 1;
while (num <= Amount)
{
Log.v(TAG, "SSID = " + results.get(num).SSID);
num = num+1;
}
}
}, filter);
mWifiManager.startScan();
}
});
}
}
更新
以下是我在启用位置服务时尝试扫描Wi-Fi时出现的错误。我只进行了一个月的android编程,所以有人可以解释这个错误意味着什么吗?
10-15 21:55:21.843 21441-21441/com.example.gytis.imlost E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gytis.imlost, PID: 21441
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 (has extras) } in com.example.gytis.imlost.MainActivity$1$1@a300383
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:935)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.gytis.imlost.MainActivity$1$1.onReceive(MainActivity.java:110)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:925)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
请帮忙。谢谢!
答案 0 :(得分:13)
如果您在使用Android 6或更高版本的设备上使用它,则必须启用位置服务才能获得扫描结果,即使您拥有正确的权限。
有关详细信息,请参阅此处:https://stackoverflow.com/a/32151901/6951450
答案 1 :(得分:1)
从Android 6开始,您必须启用位置服务才能获得所需的结果。授予权限只是完成的一半工作。
您还可以触发将用户重定向到此设置的意图:
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
位置服务是必要的,因为当您访问扫描结果时,您可以访问BSSID(接入点的地址)等信息。此信息还可用于检测设备的位置。通过要求定位服务,操作系统可确保用户在使用您的应用时了解他们的位置信息。
答案 2 :(得分:0)
我也遇到了同样的问题,有一天应用程序运行良好,第二天早上没有运行,getScanResults() 返回 0,尽管所有权限都在清单中设置,我似乎正在检查 Activity。事实证明,您现在必须检查 ACCESS_FINE_LOCATION。
因此将onclick中的代码更改为如下
@Override
public void onClick(View v)
{ if ( Build.VERSION.SDK_INT >= 23){
if (ActivityCompat.checkSelfPermission(context, Manifest.
permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED ){
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
Log.i(TAG, "User location NOT ENABLED, waiting for permission");
}else{
//Start scanning for wifi
}
}}
我的清单权限是:
<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
希望这对您或将来的某人有所帮助:)