Android/BroadcastReceiver如何判断Wifi P2P是否开启?

How does Android/BroadcastReceiver determine if Wifi P2P is enabled?

我一直在关注 Google 的 WIFIDirect 演示,但在单击负责调用 discoverPeers() 的 acitionBar 按钮后,我收到消息告诉我 "WifiDirect is not enabled"。这与 broadcastReceiver 根据下面的 onReceive 片段检查的布尔值相关联:

if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){

    //Update UI to show WifiP2p status
    int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);

    if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){
        //Wifi Direct Enabled
        //we keep the WifiP2pEnabled boolean within the activity,
        //So it wont change, and can be followed
        activity.setIsWifiP2pEnabled(true);
    }else{
        activity.setIsWifiP2pEnabled(false);
        activity.resetData();
    }
    Log.d(WIFIDirectActivity.TAG,"P2P state changed" +state);

所以它自然不会在我的 LG G3 上读取我的 WIFI_P2P_STATE_ENABLED,但市场上的其他 WifiDirect 应用程序似乎可以正常工作?我已经手动将布尔值设置为 true,在这种情况下它可以正常工作并且 discovers/connects 可以毫无问题地传递给同行。

难道真的是我的 android 版本 (4.4.2) 有问题吗?

谢谢

基于有根据的猜测和我也忘记了一百万次的事情。这里的问题是对清单的补充不完整。

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">

      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
             <!-- BOOT_COMPLETED can be any action you might want to receive -->
         </action>
      </intent-filter>

   </receiver>
</application>

您在上面看到了一个示例。