通过Wi-fi Direct连接两台设备

时间:2012-05-28 10:21:30

标签: android wifi-direct

我正在尝试通过Wi-Fi Direct连接2台Android设备。 在我的应用程序中,我正在硬编码其他设备的MAC地址并调用方法connect。我假设两台设备都启用了Wi-Fi Direct。这是我正在使用的代码:

package com.abc;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WpsInfo;
import android.net.wifi.p2p.WifiP2pConfig;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.widget.Toast;

public class WiFiDirectActivity extends Activity {
/** Called when the activity is first created. */
protected WifiP2pManager manager;
protected Channel channel;
public WifiP2pConfig config ;
protected final IntentFilter intentFilter = new IntentFilter();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    intentFilter.addAction   (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, this.getMainLooper(), null);
    config = new WifiP2pConfig();
    config.deviceAddress = "78:d6:f0:ab:d9:da";
    config.groupOwnerIntent = 0;
    config.wps.setup = WpsInfo.PBC;

    manager.connect(channel, config, new WifiP2pManager.ActionListener(){
        @Override
        public void onSuccess() {
            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG);

        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG);

        }
    });




}

}

但它没有连接。我的实施有什么问题?

2 个答案:

答案 0 :(得分:2)

我有类似的代码工作,主要区别是:

  • 我之前调用设备地址给发现对等方(如果这样做,那么你必须将WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION添加到意图过滤器组)
  • 我没有设置config.groupOwnerIntent

WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = this.address;
config.wps.setup = WpsInfo.PBC;

答案 1 :(得分:1)

在onResume()中注册BroadcastReceiver并覆盖它。记得在onPause()

中取消注册
private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

然后尝试先调用discoverPeers()

mWifiP2pManager.discoverPeers(Channel mChannel, ActionListener mActionListener);

如果discoverPeers()确实找到了对等方,则会触发操作WIFI_P2P_PEERS_CHANGED_ACTION。

我们可以在BroadcastReceiver的WIFI_P2P_PEERS_CHANGED_ACTION中调用requestPeers()

mWifiP2pManager.requestPeers(Channel mChannel, WifiP2pManager.PeerListListener);

所以我们的BroadcastReceiver现在看起来像这样

private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            mWifiP2pManager.requestPeers(mChannel , pl);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

实现WifiP2pManager.PeerListListener,你需要覆盖onPeersAvailable(WifiP2pDeviceList peers)

在onPeersAvailable()中,参数wifiP2pDeviceList表示您发现的对等项

我们需要一个UI对象让我们选择要连接的设备,所以我在这里使用spinner。

你也可以使用listView或其他东西。

private List<WifiP2pDevice> mPeers = new ArrayList<WifiP2pDevice>();
spinnerAdapter = new WiFiPeerListAdapter(this, R.layout.row_devices, mPeers);
...
@Override
public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {
    mPeers.clear();
    mPeers.addAll(wifiP2pDeviceList.getDeviceList());
    spinnerAdapter.notifyDataSetChanged();
}

最后我们可以连接到设备

WifiP2pDevice device = spinnerAdapter.getItem((int) mSpinner.getSelectedItemId());
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
mWifiP2pManager.connect(mChannel, config, mActionListener);

两台设备连接后,将触发BroadcastReceiver操作WIFI_P2P_CONNECTION_CHANGED_ACTION。所以我们可以在这里做点什么。

我们的BroadcastReceiver现在看起来像

private class WiFiDirectBroadcastReceiver extends android.content.BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            //TODO
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            mWifiP2pManager.requestPeers(mChannel , pl);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
            if (networkInfo != null) {
                Log.d(TAG,networkInfo.toString());
                if (networkInfo.isConnected()) {
                    mWifiP2pManager.requestConnectionInfo(mChannel, WifiP2pManager.ConnectionInfoListener);
                }
            }
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //TODO
        }
    }
}

顺便说一下,登录操作WIFI_P2P_CONNECTION_CHANGED_ACTION会得到这样的结果

NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, simId: 0

现在我们需要实现WifiP2pManager.ConnectionInfoListener并覆盖它的抽象方法onConnectionInfoAvailable(WifiP2pInfo info)for requestConnectionInfo()

private WifiP2pInfo p2pInfo;
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
    p2pInfo = info;
    mWifiP2pManager.requestGroupInfo(mChannel, WifiP2pManager.GroupInfoListener);
}

我们需要实现WifiP2pManager.GroupInfoListener并覆盖onGroupInfoAvailable(WifiP2pGroup组)

@Override
public void onGroupInfoAvailable(WifiP2pGroup wifiP2pGroup) {
    String log;
    if(wifiP2pGroup.isGroupOwner()) {
        log = "I am GO";
    }else{
        log = "I am not GO";
    }
    Log.d(TAG, log);
}

现在我们几乎得到了关于这两个设备的所有信息

享受它