Wifi扫描结果不是广播

时间:2014-09-29 18:37:29

标签: android android-emulator android-wifi android-broadcast

我正在尝试开始扫描可用的Wifi并在扫描完成后立即触发广播接收器。

我首先添加权限并在清单中声明接收器:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

...

<receiver android:name=".WifiReceiver">   
    <intent-filter>
        <action android:name="android.net.wifi.SCAN_RESULTS" />
    </intent-filter>
</receiver>

在我的MainActivity onCreate我启动了扫描:

// Get wifi points
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
Log.i("WIFI_SCAN", "STARTED SCAN");

最后我的WifiReceiver课程:

class WifiReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        Log.i("WIFI_SCAN", "RESULTS RECEIVED");
    }

}

我没有收到任何错误,我的第一个LogSTARTED SCAN)会显示,但我从未在日志中找到RESULTS RECEIVED

我无法弄清楚为什么我的BroadcastReceiver没有收到任何东西

1 个答案:

答案 0 :(得分:2)

您的BroadcastReceiver必须公开。

class WifiReceiver extends BroadcastReceiver {

应该是

public class WifiReceiver extends BroadcastReceiver {