BayK BayK - 30 days ago 10
Android Question

Resolving Android Studio's WIFI P2P implementation

I'm new to android and currently designing an app with WIFI P2P functionality. I'm using the site's guide on this:

I have two issues in my receiver class which I am unable to resolve.
Receiver code:

package com.example.bilent.remote_connect;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

private WifiP2pManager manager;
private WifiP2pManager.Channel channel;
private Connect activity; //YES - from MAIN (Connect)

public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
Connect activity) {
this.manager = manager; = channel;
this.activity = activity;

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Determine if Wifi P2P mode is enabled or not, alert
// the Activity.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
else {
activity.setIsWifiP2pEnabled(false);} }

else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing

DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(


There's an error on:


where setIsWifiP2pEnabled is highlighted. I have all the relevant permissions and my main class name is Connect.

The other error is on

DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()

Where I'm not sure what DeviceListFragment is, what ID to use, and both of where there are errors.

I realize these questions might be really basic, and I have looked through many relevant P2P related examples, but I wasn't sure what extent of knowledge I'd need or which guides to go through to solve this.
The documentation hasn't been much help either. If you could give a few pointers, it would be great.

Edit: Here's my Main (Connect) class with related layouts:

package com.example.bilent.remote_connect;

import android.content.*;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.view.Menu;
import android.content.IntentFilter;
import android.view.MenuItem;

public class Connect extends ActionBarActivity {

private final IntentFilter intentFilter = new IntentFilter();
Channel mChannel;
WiFiDirectBroadcastReceiver receiver;
WifiP2pManager mManager;

protected void onCreate(Bundle savedInstanceState) {

// Indicates a change in the Wi-Fi P2P status.

// Indicates a change in the list of available peers.

// Indicates the state of Wi-Fi P2P connectivity has changed.

// Indicates this device's details have changed.

mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == {
return true;

return super.onOptionsItemSelected(item);


Here's the layout & fragment:

<fragment xmlns:android=""
xmlns:tools="" android:id="@+id/fragment"
tools:layout="@layout/fragment_connect" android:layout_width="match_parent"
android:layout_height="match_parent" />

<RelativeLayout xmlns:android=""
xmlns:tools="" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ConnectFragment">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Answer Source

Ok the first problem is that you have not defined a setIsWifiP2pEnabled() method on your Connect activity. You will need to add something along the lines of the following method to your Connect activity;

public class Connect extends ActionBarActivity {

    // Other methods omitted for brevity

    public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
         // Do something in response to the boolean you are supplied


Relating to your fragment issue I would strongly encourage you to take a look at Creating a Fragment. This is the minimum example to creating fragments and how you can access them.

If you want, the code used for the WiFi Direct demo is available in the android source code. The code for the DeviceListFragment is here.