Android / BroadcastReceiver如何确定是否启用了Wifi P2P?

时间:2016-12-28 16:14:34

标签: android android-broadcastreceiver wifip2p

我一直在关注Google的WIFIDirect演示,但在点击负责调用discoverPeers()的acitionBar按钮后,我收到消息告诉我“WifiDirect未启用”。这与broadReceiver在下面的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,在这种情况下它可以正常工作并发现/连接到对等体而没有问题。

它的问题实际上是我的Android版本(4.4.2)吗?

谢谢

1 个答案:

答案 0 :(得分:1)

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

<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>

上面你会看到一个如何显示它的例子。