Android WIFI direct P2P

WIFI direct 分为2种模式。

1. 直接连接。
	正常的wifi direct,需要2个设备都支持这个功能。2个设备中会有一个随机成功group owner,也可以在连接的时候指定。

2. 兼容模式。
	一个带有直连功能的设备,开启兼容模式。其它设备可以通过wifi direct直接和这个设备连接,如果其他设备不支持wifi direct功能,也可以连接,
通过正常的wifi连接,把开启兼容模式的设备当做AP,在wifi扫描列表中可以看到。当然你要知道它的ssid和密码。
	用这个方法来开启兼容模式。
	createGroup(Channel c, ActionListener listener)
 


mP2pManager = (WifiP2pManager) context.getSystemService(Context.WIFI_P2P_SERVICE);

mChannel = mP2pManager.initialize(context, context.getMainLooper(), null);

//注册一些广播,扫描到P2P,连接状态等等。

private class wifiBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            switch (state) {
                case WifiManager.WIFI_STATE_ENABLED:
                    Log.i(TAG, "WIFI_STATE_ENABLED");
                    mP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                        @Override
                        public void onSuccess() {
                            Log.i(TAG, "discover peers success");
                        }

                        @Override
                        public void onFailure(int reason) {
                            Log.i(TAG, "discover peers fail, reason: " + reason);
                        }
                    });
                    break;
                default:
                    break;
            }
        } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {

        } else if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {

        } else if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            // Check to see if Wi-Fi is enabled and notify appropriate activity
            Log.i(TAG, "WIFI_P2P_STATE_CHANGED_ACTION");
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            // Call WifiP2pManager.requestPeers() to get a list of current peers
            Log.i(TAG, "WIFI_P2P_PEERS_CHANGED_ACTION");
		//扫描到的列表
            mP2pManager.requestPeers(mChannel, mPeerListListener);
        } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            // Respond to new connection or disconnections
            Log.i(TAG, "WIFI_P2P_CONNECTION_CHANGED_ACTION");

            mP2pManager.requestConnectionInfo(mChannel, mConnectionInfoListener);
        } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            // Respond to this device's wifi state changing
            Log.i(TAG, "WIFI_P2P_THIS_DEVICE_CHANGED_ACTION");

        } else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
            int state = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, 0);
            switch (state) {
                case WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED:
                    Log.i(TAG, "WIFI_P2P_THIS_DEVICE_CHANGED_ACTION   started");
                    ((SetupWifiActivity) mContext).notifyDiscoveryStarted();
                    break;
                case WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED:
                    Log.i(TAG, "WIFI_P2P_THIS_DEVICE_CHANGED_ACTION  stopped");
                    ((SetupWifiActivity) mContext).notifyDiscoveryStopped();
                    break;
                default:
                    break;
            }

        }
    }
}



//开启p2p,让别的设备可以发现,大约2-3分钟可以执行。
public void discoverPeers() {
    mP2pManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i(TAG, "discover peers success");
        }

        @Override
        public void onFailure(int reason) {
            Log.i(TAG, "discover peers fail, reason: " + reason);
        }
    });
}
//重命名设备的名字,反射的方法.
public void reNameP2p() {
    try {
        Method method = mP2pManager.getClass().getMethod("setDeviceName", WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);
        method.invoke(mP2pManager, mChannel, "Pando", new WifiP2pManager.ActionListener() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "name success");
            }

            @Override
            public void onFailure(int reason) {
                Log.i(TAG, "name success");
            }
        });

    } catch (Exception e) {
        Log.i(TAG, "Exception " + e.getMessage());
        e.printStackTrace();
    }
}

public void release() {
    mP2pManager.removeGroup(mChannel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i(TAG, " Remove group success");
        }

        @Override
        public void onFailure(int reason) {
            Log.i(TAG, " Remove group fail " + reason);
        }
    });
    mP2pManager.stopPeerDiscovery(mChannel, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i(TAG, " stop discovery success");
        }

        @Override
        public void onFailure(int reason) {
            Log.i(TAG, " stop discovery fail " + reason);
        }
    });
}

//连接到指定的p2p设备
public void connectDevice(WifiP2pDevice device) {
    WifiP2pConfig config = new WifiP2pConfig();
    config.deviceAddress = device.deviceAddress;
    config.wps.setup = WpsInfo.PBC;
    config.groupOwnerIntent = 0;
    mP2pManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
        @Override
        public void onSuccess() {
            Log.i(TAG, "do connect success");
        }

        @Override
        public void onFailure(int reason) {
            Log.i(TAG, "do connect fail " + reason);
        }
    });
}


展开阅读全文

没有更多推荐了,返回首页