未收到扫描结果
No scan results received
我不知道我做错了什么。我已经注册了 BroadcastReceiver。我定期调用 onReceive,但 WifiManager 的扫描结果为 0。
当我将 wifi 状态打印到日志中时,它总是变得 WIFI_STATE_UNKNOWN。当我检查 wifi 设置中的结果时,我有很多扫描结果。我什至连接到一个。
接收者:
public class NetworkManager extends BroadcastReceiver {
private final static String TAG = NetworkManager.class.getSimpleName();
private WifiManager access;
private static NetworkManager manager;
private List<WifiScanCallback> callbackList;
public static NetworkManager getManager(Context context) {
if (manager == null) {
manager = new NetworkManager();
manager.init(context);
}
return manager;
}
public void init(Context context){
access = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
callbackList = new ArrayList<>();
}
public NetworkManager() {
manager = this;
}
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
Log.d(TAG, "onReceive: "+intent);
debugWifiState(wifiState);
if(!isAccessNull()){
List<ScanResult> result = access.getScanResults();
if(callbackList != null && result != null && result.size() > 0 && callbackList.size() > 0){
for(ScanResult currResult : result){
Log.d(TAG, "onReceive: "+currResult.SSID);
for(WifiScanCallback callback : callbackList){
String ssid = currResult.SSID;
callback.onResult(ssid, null);
}
}
}
}
scan(true);
}
}
主要活动
@Override
protected void onResume() {
super.onResume();
networkManager = NetworkManager.getManager(this);
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(networkManager, filter);
networkManager.scan(true);
}
清单
<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"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
从 Android 6.0 开始,您必须在运行时请求权限,这个答案对我有帮助:
我不知道我做错了什么。我已经注册了 BroadcastReceiver。我定期调用 onReceive,但 WifiManager 的扫描结果为 0。
当我将 wifi 状态打印到日志中时,它总是变得 WIFI_STATE_UNKNOWN。当我检查 wifi 设置中的结果时,我有很多扫描结果。我什至连接到一个。
接收者:
public class NetworkManager extends BroadcastReceiver {
private final static String TAG = NetworkManager.class.getSimpleName();
private WifiManager access;
private static NetworkManager manager;
private List<WifiScanCallback> callbackList;
public static NetworkManager getManager(Context context) {
if (manager == null) {
manager = new NetworkManager();
manager.init(context);
}
return manager;
}
public void init(Context context){
access = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
callbackList = new ArrayList<>();
}
public NetworkManager() {
manager = this;
}
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
Log.d(TAG, "onReceive: "+intent);
debugWifiState(wifiState);
if(!isAccessNull()){
List<ScanResult> result = access.getScanResults();
if(callbackList != null && result != null && result.size() > 0 && callbackList.size() > 0){
for(ScanResult currResult : result){
Log.d(TAG, "onReceive: "+currResult.SSID);
for(WifiScanCallback callback : callbackList){
String ssid = currResult.SSID;
callback.onResult(ssid, null);
}
}
}
}
scan(true);
}
}
主要活动
@Override
protected void onResume() {
super.onResume();
networkManager = NetworkManager.getManager(this);
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(networkManager, filter);
networkManager.scan(true);
}
清单
<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"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
从 Android 6.0 开始,您必须在运行时请求权限,这个答案对我有帮助: