简体   繁体   English

WiFi Direct设备与其他Android设备的连接

[英]WiFi Direct device connection with other Android devices

Can I connect a WiFi Direct enabled device to any other device which doesn't have WiFi Direct feature but supports WiFi hotspot connection? 我是否可以将支持WiFi Direct的设备连接到任何其他没有WiFi Direct功能但支持WiFi热点连接的设备? Does WiFi direct uses specialized hardware to be present on both devices? WiFi直接使用专用硬件是否存在于两个设备上? Will network discovery work in such cases? 在这种情况下网络发现会起作用吗?

It is possible. 有可能的。 Code taken from a talk I gave at Droidcon UK 2013. 代码取自我在Droidcon UK 2013上发表的演讲。

You need to call the createGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener) method of the WifiP2pManager class. 您需要调用WifiP2pManager类的createGroup createGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)方法。 This will create a Wi-Fi Direct group that supports legacy Wi-Fi connections. 这将创建一个支持传统Wi-Fi连接的Wi-Fi Direct组。

Prior to the call, you need to register a broadcast receiver similar to this: 在通话之前,您需要注册类似于此的广播接收器:

BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals
            (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)){
            wifiP2pManager.requestGroupInfo(channel,
                new WifiP2pManager.GroupInfoListener() {
                @Override
                public void onGroupInfoAvailable(WifiP2pGroup group) {
                    if(group != null){
                        // clients require these
                        String ssid = group.getNetworkName(),
                        String passphrase = group.getPassphrase() 
                    }
                }
            });
        }
    }
};

The other devices can then use Wi-Fi to connect to the Wi-Fi Direct device, once they have the ssid and passphrase. 然后,其他设备可以使用Wi-Fi连接到Wi-Fi Direct设备,一旦他们有ssid和密码短语。

Stephen's answer is great, but I found it's better to get the group info at 斯蒂芬的答案很棒,但我发现最好把团体信息告诉他们

WIFI_P2P_CONNECTION_CHANGED_ACTION WIFI_P2P_CONNECTION_CHANGED_ACTION

...

if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
    NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
    WifiP2pInfo wifiP2pInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_INFO);
    if (networkInfo.isConnected() && wifiP2pInfo.groupFormed) {
            if (wifiP2pInfo.isGroupOwner) {
                wifiP2pManager.requestGroupInfo(channel, new WifiP2pManager.GroupInfoListener() {
                    @Override
                    public void onGroupInfoAvailable(final WifiP2pGroup wifiP2pGroup) {
                        if (wifiP2pGroup != null) {
                            // clients require these
                            String ssid = wifiP2pGroup.getNetworkName();
                            String passphrase = wifiP2pGroup.getPassphrase();
                            ...
                        }
                    }
                }
            }
        }
    }
}
...

Because this can make sure the access point is created and current device is the group owner (GO). 因为这可以确保创建接入点,并且当前设备是组所有者(GO)。

It is possible according to the WiFi Direct documentation, as mentiond here. 根据WiFi Direct文档,这是可能的, 如此处所述。

If I re-phrase the documentation, 如果我重新说明文档,

Create a p2p group with the current device as the group owner.This essentially creates an access point that can accept connections from legacy clients as well as other p2p devices 创建一个p2p组,将当前设备作为组所有者。这实质上创建了一个接入点,可以接受来自旧客户端以及其他p2p设备的连接

but the guides are for a pretty narrow scope. 但指南的范围很窄。 you'll have to do a bit of research to find out how to implement! 你需要做一些研究才能找到实施方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM