wifi p2p peer更改了未触发的意图

时间:2018-01-16 10:05:37

标签: android android-intent wifi-direct android-broadcastreceiver wifip2p

我正在开发一个wifi p2p文件共享应用程序。但我无法发现同行

以下是我的活动代码

    package com.neeraj8le.majorproject.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.neeraj8le.majorproject.R;
import com.neeraj8le.majorproject.WiFiDirectBroadcastReceiver;

import java.util.ArrayList;
import java.util.List;

public class SelectPeerActivity extends AppCompatActivity{

    WifiP2pManager mManager;
    WifiP2pManager.Channel mChannel;
    BroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;
    ArrayAdapter<String> wifiP2pArrayAdapter;
    ListView listView;


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

        listView = findViewById(R.id.list_view);

        wifiP2pArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        listView.setAdapter(wifiP2pArrayAdapter);

        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 = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);



        search();


    }

    public void search()
    {
        mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Toast.makeText(SelectPeerActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(int reasonCode) {
                Toast.makeText(SelectPeerActivity.this, "FAILURE", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void displayPeers(WifiP2pDeviceList peerList)
    {
        wifiP2pArrayAdapter.clear();

        for(WifiP2pDevice peer : peerList.getDeviceList())
            wifiP2pArrayAdapter.add(peer.deviceName + "\n" + peer.deviceAddress);

    }


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

}

这是我的broadcastreceiver的代码

package com.neeraj8le.majorproject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
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;

import com.neeraj8le.majorproject.activity.SelectPeerActivity;

import java.util.ArrayList;
import java.util.List;


public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = ":::::::::::::::::::::";
    private WifiP2pManager mManager;
    private WifiP2pManager.Channel mChannel;
    private SelectPeerActivity mActivity;
    List<WifiP2pDevice> mPeers;
    List<WifiP2pConfig> mConfigs;

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

    @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(mActivity, "Wifi Direct Enabled", Toast.LENGTH_SHORT).show();
                // Wifi P2P is enabled
            } else {
                Toast.makeText(mActivity, "Wifi Direct Disabled", Toast.LENGTH_SHORT).show();
                // Wi-Fi P2P is not enabled
            }

        }
        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()
            mPeers = new ArrayList<>();
            mConfigs = new ArrayList<>();

            if (mManager != null) {

                mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {
                            @Override
                            public void onPeersAvailable(WifiP2pDeviceList peerList) {
                                mPeers.clear();
                                mPeers.addAll(peerList.getDeviceList());
                                mActivity.displayPeers(peerList);

                                for (int i = 0; i < peerList.getDeviceList().size(); i++) {
                                    WifiP2pConfig config = new WifiP2pConfig();
                                    config.deviceAddress = mPeers.get(i).deviceAddress;
                                    mConfigs.add(config);
                                }
                            }

                        });

            }

            if (mPeers.size() == 0) {
                Log.d("::::::::::::::::::::::", "No devices found");
                return;
            }

        }
        else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            Log.d(TAG, "wifi p2p connection changed action");
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
            Log.d(TAG,"network info available");
            // Respond to new connection or disconnections
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state
            Log.d(TAG, "wifi p2p this device changed action");
//            notifyDeviceUpdate();
            WifiP2pDevice thisDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
            Log.d(TAG,"this device acquired");
        }
    }
}

我从here

获得了大部分代码

我调试了应用程序,除了

之外,所有3个意图过滤器都被触发了

WIFI_P2P_PEERS_CHANGED_ACTION

我尝试在我的手机附近保留另一个Android设备以查看它是否检测到对等方并且一次ID确实触发了WIFI_P2P_PEERS_CHANGED_ACTION意图..但后来它说对等列表大小为0因此我无法找到对等方。

非常感谢任何有关此问题的帮助。

2 个答案:

答案 0 :(得分:0)

我想问题是你试图发现的设备不是“可被发现的”。类似于WiFi P2P中的蓝牙发现,它也必须被启用,不幸的是它不像BT那样明确地发生。根据我的经验,为了能够发现其他设备,您必须:

  1. 导航至WiFi - &gt;设置 - &gt; Wi-Fi Direct(在Android 6.0上可能因版本而异)。只需留在菜单中即可完成工作。
  2. 也可以在其他设备上启动发现过程。
  3. 这有点棘手,但我找不到任何其他解决方案。 Android WifiP2p的文档也没有提及它。请参阅我的answer,我在那里做了更多解释。

答案 1 :(得分:-1)

要能够通过直接Wi-Fi检测其他设备,必须激活每部手机上的位置。