PC和Android之间的Wifi-Direct消息

时间:2017-05-22 06:51:31

标签: java android wifi-direct pc

我正在尝试编写一个应用程序,允许我从我的Android手机向我的PC上的控制台发送消息。我已经有代码使用套接字将消息从一台PC发送到另一台PC。现在我正在尝试按照本教程在Android手机上实现Wifi-Direct:

https://developer.android.com/guide/topics/connectivity/wifip2p.html#creating-app

我有2个java文件:

MainActivity.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity{
    WifiP2pManager mManager;
    WifiP2pManager.Channel mChannel;
    BroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;

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

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

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

        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.d("Peers Discovered", "SUCCEEDED");
            }

            @Override
            public void onFailure(int reasonCode) {
                Log.d("Peers Discovered", "FAILURE");
            }
        });
    }

    /* register the broadcast receiver with the intent values to be matched */
    @Override
    protected void onResume(){
        super.onResume();
        registerReceiver(mReceiver, mIntentFilter);
    }

    /* unregister the broadcast receiver */
    @Override
    protected void onPause(){
        super.onPause();
        unregisterReceiver(mReceiver);
    }
}

WifiDirectBroadcastReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.util.Log;
import android.widget.Toast;

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

class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private MainActivity mActivity;

    public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, MainActivity activity) {
        super();
        this.mManager = manager;
        this.mChannel = channel;
        this.mActivity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        WifiP2pManager.PeerListListener myPeerListListener = new WifiP2pManager.PeerListListener() {
            @Override
            public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
                for (WifiP2pDevice d: wifiP2pDeviceList.getDeviceList()){
                    Log.d("WIFIP2PDEVICE", "Device d's address: " + d.deviceAddress);
                    if (d.deviceAddress==""){
                        //obtain a peer from the WifiP2pDeviceList
                        WifiP2pConfig config = new WifiP2pConfig();
                        config.deviceAddress = d.deviceAddress;
                        mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {

                            @Override
                            public void onSuccess() {
                                //success logic

                            }

                            @Override
                            public void onFailure(int reason) {
                                //failure logic
                            }
                        });
                    }
                }
            }
        };

        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) {
                // Wifi P2P is enabled
                Toast.makeText(context, "Wi-Fi Direct is enabled", Toast.LENGTH_SHORT).show();
            } else {
                // Wi-Fi P2P is not enabled
                Toast.makeText(context,"Wifi is disabled, please enable to continue", Toast.LENGTH_SHORT);
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers

            // request available peers from the wifi p2p manager. This is an
            // asynchronous call and the calling activity is notified with a
            // callback on PeerListListener.onPeersAvailable()
            if (mManager != null) {
                mManager.requestPeers(mChannel, myPeerListListener);
                mManager.onPeersAvailable(myPeerListListener); //ERROR HAPPENS HERE

            }

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

我目前遇到的问题是我收到错误:

Cannot resolve method 'onPeersAvailable(android.net.wifi.p2p.WifiP2pManager.PeerListListener)

为什么我的代码不识别onPeersAvailable(...)的实现?

1 个答案:

答案 0 :(得分:0)

您正试图直接致电onPeersAvailable(myPeerListListener)。这不是它的工作原理(如果你回顾一下你所遵循的教程,你会发现它们也没有这样做)。您所需要做的就是:

  • 使用WifiP2pManager
  • 注册侦听器回调
  • 然后,只要准备好(异步),管理员就会调用你的监听器

删除违规行,事情应该没问题。