【问题标题】:Resolving Android Studio's WIFI P2P implementation解决Android Studio的WIFI P2P实现
【发布时间】:2015-06-27 08:51:42
【问题描述】:

我是 android 新手,目前正在设计一个具有 WIFI P2P 功能的应用程序。我正在使用该网站的指南: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html

我的接收器类中有两个我无法解决的问题。 收货人代码:

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

    }
  }
}

出现错误:

activity.setIsWifiP2pEnabled(true);

其中 setIsWifiP2pEnabled 突出显示。我拥有所有相关权限,我的主要班级名称是 Connect。

另一个错误是打开

DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                .findFragmentById(R.id.fragment_connect);

我不确定 DeviceListFragment 是什么,使用什么 ID,以及两者都存在错误。

我意识到这些问题可能非常基础,并且我查看了许多与 P2P 相关的示例,但我不确定我需要多大程度的知识或通过哪些指南来解决这个问题。 文档也没有太大帮助。如果你能给一些指点,那就太好了。

编辑:这是我的主(连接)类和相关布局:

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

}      

这是布局和片段:

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

【问题讨论】:

  • 您看到的错误是什么?它们是运行时、编译器还是 IDE 相关的?
  • “无法解析方法/符号”错误。在编译之前,这些区域以红色突出显示。那么,我猜是 IDE 吗?
  • 啊,好吧,第一个错误表明您在 Connect 活动中没有名为 setIsWifiP2pEnabled() 的公共方法。您介意分享您的 Connect 活动的来源和该活动的布局吗?

标签: android android-fragments android-wifi wifi-direct


【解决方案1】:

好的,第一个问题是您没有在 Connect 活动上定义 setIsWifiP2pEnabled() 方法。您需要按照以下方法向您的 Connect 活动添加一些内容;

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
    }

} 

关于您的片段问题,我强烈建议您查看Creating a Fragment。这是创建片段以及如何访问它们的最小示例。

如果需要,用于 WiFi Direct 演示的代码可在 android 源代码中找到。 DeviceListFragment 的代码是here

【讨论】:

  • 非常感谢!但是,DeviceListFragment 仍然高亮显示为错误?
  • DeviceListFragment 是你自己的类吗?我假设您已经从示例代码中获取了这一点。在这种情况下,您应该查看如何实例化创建片段。
  • 是的,我只是从示例代码中获取的,没有提供额外的类。我想我必须创建一个新类并在我的主类中实例化它,对吧?
  • 另外,您能否指出可能有助于 android-wise 的资源的方向?网上有很多东西,但我不确定其中有多少有用或相关。
  • 为您指出了相应的 Android 培训指南,这可能是我能在短时间内想到的最简单的最小噪音示例。请参阅我的链接答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多