使用 android Wi-Fi Direct API 的问题:mChannel = mManager.initialize()

Posted

技术标签:

【中文标题】使用 android Wi-Fi Direct API 的问题:mChannel = mManager.initialize()【英文标题】:Problems using android Wi-Fi Direct API: mChannel = mManager.initialize() 【发布时间】:2012-10-15 12:38:25 【问题描述】:

这是我的第一篇文章,希望能做对。我正在尝试使用新的 android 技术 Wi-Fi Direct 创建一个应用程序。为此,我遵循了您可以在以下位置找到的教程:

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

确实很好用,但是我复制代码的时候,出现了问题。与第 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);
    ...

错误在线:

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

错误信息是:

Type mismatch: cannot convert from WifiPesManager.Channel to Channel

建议制作这样的演员表:

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

但是当我为此更改代码时,运行应用程序时出现另一个错误:

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

只需添加其余代码就是教程中的样子,但以防万一我要添加 Activity 和 Broadcast 类:

主要活动

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>

非常感谢您的帮助!!!

【问题讨论】:

【参考方案1】:

您正在使用来自错误包的 Channel 类。

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

而不是

import java.nio.channels.Channel;

【讨论】:

非常感谢。这就是问题所在。真是愚蠢的错误,只是为了相信 Eclipse 的提示

以上是关于使用 android Wi-Fi Direct API 的问题:mChannel = mManager.initialize()的主要内容,如果未能解决你的问题,请参考以下文章

Android Wi-Fi Direct 在连接前发送数据

Android Wi-Fi Direct P2P 连接多个设备

使用 android Wi-Fi Direct API 的问题:mChannel = mManager.initialize()

Android Wi-Fi Direct:onPeersAvailable

使用蓝牙/wi-fi direct/BLE api 或任何其他方式与 android 设备通信 ios 设备

无法在 Android 11 (Wi-Fi Direct) 上使用 WifiP2pManager.setDeviceName