Search code examples
androidandroid-broadcastreceiverwifip2p

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


I've been following Google's WIFIDirect demo pretty much to the letter, but upon clicking the acitionBar button responsible for calling discoverPeers(), I get the message telling me "WifiDirect is not enabled". This is tied to a boolean that's checked by the broadcastReceiver upon the onReceive snippet below:

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

So naturally it's not reading my WIFI_P2P_STATE_ENABLED on my LG G3, but it other WifiDirect apps on the market seem to work? I've manually set the boolean to true, in which case it works and discovers/connects to peers without issue.

Could it actually be my android version (4.4.2) that's the problem?

Thank you


Solution

  • Based on an educated guess and something I have forgotten a million times as well. The problem here is an incomplete addition to the manifest.

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

    Above you see an example of how this should look like.