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:
http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

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;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;


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) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}


@Override
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
activity.setIsWifiP2pEnabled(true);}
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()
.findFragmentById(R.id.fragment_connect);
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

}
}
}


There's an error on:

activity.setIsWifiP2pEnabled(true);


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()
.findFragmentById(R.id.fragment_connect);


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.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.content.IntentFilter;
import android.view.MenuItem;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;


public class Connect extends ActionBarActivity {

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

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);

// Indicates a change in the Wi-Fi P2P status.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);

// Indicates a change in the list of available peers.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);

// Indicates the state of Wi-Fi P2P connectivity has changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);

// Indicates this device's details have changed.
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_connect, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

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

return super.onOptionsItemSelected(item);
}

}


Here's the layout & fragment:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.bilent.remote_connect.ConnectFragment"
tools:layout="@layout/fragment_connect" android:layout_width="match_parent"
android:layout_height="match_parent" />



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_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.