使用Android-7.1.1,SDK-25.3.0
我遇到了WiFi广播接收器的问题。在Android-7.0.x之前,一切运行良好。但现在,已经更新到Android-7.1.1。凭借其最新的SDK,我的WiFi广播接收器不再返回任何值。
List<ScanResult> resultList = wifi.getScanResults();
返回0(而不是在某些有价值的值之前)。为什么是这样 ??? Android更新可能导致什么或我的代码中有任何错误?任何帮助表示赞赏!
以下是我的代码:
在片段内设置WiFi广播接收器(不是完整片段但是例外......):
public class ScanTeachingFragment extends Fragment implements MyWifiReceiver.OnWiFiResultListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set up WiFi Manager
this.wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
// register Broadcast-Receiver for WiFi
receiverWifi = new MyWifiReceiver(this.context);
IntentFilter filterWiFi = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
getActivity().registerReceiver(receiverWifi, filterWiFi);
}
// setListener necessary otherwise null-pointer occurs in WiFiReceiver
receiverWifi.setListener(this);
}
private void ScanWifi() {
if (!IndoorNavHelpers.inEmulator()) {
wifiManager.setWifiEnabled(true);
wifiManager.startScan();
}
}
}
这是WiFi广播接收器实现:
package com.xxxx.yyyy.broadcast_receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import java.util.List;
public class MyWifiReceiver extends BroadcastReceiver {
private Context context;
OnWiFiResultListener resultListener;
// ScanFragment must implement this interface
public interface OnWiFiResultListener {
void scanWiFireceived(List<ScanResult> resultList);
}
// create setListener class (otherwise null-pointer occurs)
public void setListener(OnWiFiResultListener listener) {
this.resultListener = listener;
}
public MyWifiReceiver(Context context)
{
this.context = context;
}
// This method call when number of wifi connections changed
public void onReceive(final Context context, Intent intent) {
WifiManager wifi = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> resultList = wifi.getScanResults();
// ???????????????????????????????????????????????????????????
// Here resultList returns 0, WHY ????????????????????????????
// ???????????????????????????????????????????????????????????
// append newly found WiFi-scanResults to WiFiSniffing_File by calling callback-method in MainActivity
if(resultListener != null) {
resultListener.scanWiFireceived(resultList);
}
}
}
这是我的gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.xxxx.yyyy"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "0.0.2(26)"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:design:25.3.0'
compile files('libs/ftp4j-1.7.2.jar')
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.github.johnkil.print:print:1.2.2'
compile 'com.github.bmelnychuk:atv:1.2.+'
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.yyyy">
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_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.android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/yyyy_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:largeHeap="true">
<activity
android:name=".activity.MainActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETATCHED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter" />
</activity>
</application>
</manifest>
答案 0 :(得分:0)
我终于找到了解决方案:如果我出厂重置我的Nexus 5X并重新安装Android Nougat 7.1.1,那么我的代码再次运行!不知道在此期间损坏移动安装的是什么?因此上面的代码现在可以使用了!
答案 1 :(得分:0)
打开GPS ..
对于Android 7,需要权限ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION来获取Wi-Fi扫描结果。您还必须在设备上打开位置。