Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:21:12+00:00 2026-06-13T12:21:12+00:00

This is my first post, hope to do it right. I’m trying to create

  • 0

This is my first post, hope to do it right. I’m trying to create an application using the new android technology Wi-Fi Direct. To do so I was following the tutorial you can find in:

http://developer.android.com/guide/topics/connectivity/wifip2p.html

It’s really useful, but when I copy the code, there is something wrong. Exactly with step number 3:

WifiP2pManager mManager;
Channel mChannel;
BroadcastReceiver mReceiver;
...
@Override
protected void onCreate(Bundle savedInstanceState){
    ...
    mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    mChannel = mManager.initialize(this, getMainLooper(), null);
    mReceiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
    ...
}

The error is on line:

mChannel = mManager.initialize(this, getMainLooper(), null);

And the error message is:

Type mismatch: cannot convert from WifiPesManager.Channel to Channel

The recommendation is to make a cast like this:

mChannel =  (Channel) mManager.initialize(this, getMainLooper(), null);

But when I change my code for that, I have another error while running the application:

10-25 12:08:34.845: E/AndroidRuntime(26634): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.nacho.WifiDirect/android.nacho.WifiDirect.WifiDirect}: java.lang.ClassCastException: android.net.wifi.p2p.WifiP2pManager$Channel cannot be cast to java.nio.channels.Channel

Just add the rest of the code is exactly how it appear in the tutorial, but just in case I’m going to add the Activity and also the Broadcast class:

Main Activity

package android.nacho.WifiDirect;

import java.nio.channels.Channel;

import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.view.Menu;

public class WifiDirect extends Activity {

    
    WifiP2pManager mManager;
    Channel mChannel;
    BroadcastReceiver mReceiver;
    
    IntentFilter mIntentFilter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_direct);
        
        
        //To register the BroadastReceiver
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel =  (Channel) mManager.initialize(this, getMainLooper(), null); //It was necessary to make a cast (Channel)
        mReceiver = new WiFiBroadcastReceiver(mManager, mChannel, this, this);
        
        
        //To define the filter in the BroadcastReceiver
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        
       
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_wifi_direct, menu);
        return true;
    }
   
    //
    
    
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);
    }
    
   // unregister the broadcast receiver
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }
    
}

WiFiBroadcastReceiver

package android.nacho.WifiDirect;

import java.nio.channels.Channel;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;



/**
 * A BroadcastReceiver that notifies of important Wi-Fi p2p events.
 */

public class WiFiBroadcastReceiver extends BroadcastReceiver {

    private WifiP2pManager manager;
    private Channel channel;
    private WifiDirect activity;
    //For toast, add also context
    private Context context;

    public WiFiBroadcastReceiver(WifiP2pManager manager, Channel channel, WifiDirect activity, Context context) {
        super();
        this.manager = manager;
        this.channel = channel;
        this.activity = activity;
        this.context= context;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        
        String action = intent.getAction();
        
        
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            
            // Check to see if Wi-Fi is enabled and notify appropriate activity
             int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
             if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
                            
                Toast.makeText(context, "Wi-Fi Direct is enable", Toast.LENGTH_LONG).show();
                
             } else {
                 
                Toast.makeText(context, "Wi-Fi Direct is not enable", Toast.LENGTH_LONG).show();
             }      
            
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
        }
    }
}

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.nacho.WifiDirect"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="15" />
    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

   

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".WifiDirect"
            android:label="@string/title_activity_wifi_direct" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you very much for your help!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:21:12+00:00Added an answer on June 13, 2026 at 12:21 pm

    You are using a Channel class coming from the wrong package.

    Do import android.net.wifi.p2p.WifiP2pManager.Channel;

    instead of

    import java.nio.channels.Channel;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My first post on this site with huge hope:: I am trying to understand
this is my first post so I hope I am posting to the right
this is my first post here so I hope I provide all the right
First post - hope I'm doing it right! I have a file, lexicon.plist, containing
This is my first post on the forum, hope all of you guys are
this is my first post here on stackoverflow, so I hope I'm doing this
First post on stackoverflow. Hope everything is right! I'm thinking of attaching an ID
first post, hope I'm doing it right :-) I'm after running into difficulties with
This is my first post, so I hope I am going about it the
This is my first post so I hope I am not violating any rules.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.