WiFi Direct设备与其他Android设备的连接
我可以将启用了WiFi Direct的设备连接到其他没有WiFi Direct功能但支持WiFi热点连接的设备吗? WiFi是否直接使用专用硬件在两个设备上都存在?在这种情况下,网络发现会起作用吗?
Can I connect a WiFi Direct enabled device to any other device which doesn't have WiFi Direct feature but supports WiFi hotspot connection? Does WiFi direct uses specialized hardware to be present on both devices? Will network discovery work in such cases?
有可能。代码摘自我在Droidcon UK 2013上的一次演讲。
It is possible. Code taken from a talk I gave at Droidcon UK 2013.
您需要调用 createGroup(WifiP2pManager.Channel c,WifiP2pManager.ActionListener listener) WifiP2pManager类的
方法。这将创建一个支持旧版Wi-Fi连接的Wi-Fi Direct组。
You need to call the createGroup(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener)
method of the WifiP2pManager class. This will create a Wi-Fi Direct group that supports legacy Wi-Fi connections.
在通话之前,您需要注册一个类似于以下内容的广播接收器:
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()
}
}
});
}
}
};
其他设备随后可以使用Wi-Fi连接到Wi-Fi Direct设备有ssid和密码。
The other devices can then use Wi-Fi to connect to the Wi-Fi Direct device, once they have the ssid and passphrase.