将 iPhone 连接到 Android 的 Wifi Direct 软 AP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12684709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:25:43  来源:igfitidea点击:

connect iPhone to Android's Wifi Direct soft AP

androidiphonenetworkingwifiwifi-direct

提问by AlcubierreDrive

I know that Wifi Direct works by creating a Soft AP (software access point) in one of the devices. I also know that many Androids support Wifi Direct, but iPhones do not.

我知道 Wifi Direct 的工作原理是在其中一台设备中创建一个软 AP(软件接入点)。我也知道许多 Android 支持 Wifi Direct,但 iPhone 不支持。

My question is: is it possible to create a device-to-device wifi link that is Wifi Direct on the Android side, but regular wifi on the iPhone side? Where the Android's Wifi Direct would be presenting a soft AP, which the iPhone would see as indistinguishable from a regular AP and be able to associate to.

我的问题是:是否可以在 Android 端创建 Wifi Direct 的设备到设备 wifi 链接,但在 iPhone 端创建常规 wifi?Android 的 Wifi Direct 将呈现一个软 AP,iPhone 会认为它与常规 AP 没有区别,并且能够与之关联。

Imagine that this is out in the wilderness where no router AP is available. Also, neither user has a tethering plan.

想象一下,这是在没有路由器 AP 可用的荒野中。此外,两个用户都没有网络共享计划。

This link would be used by a Bump-like app to transfer files.

类似 Bump 的应用程序将使用此链接来传输文件。

采纳答案by Nils Erik

Depending on your phone you can just set up your Android phone as a portable hotspot and connect to that with the iPhone. From there it would be application specific to get data transferred.

根据您的手机,您可以将 Android 手机设置为便携式热点,然后与 iPhone 连接。从那里获取数据将是特定于应用程序的。

However you can also use the Androids WiFi-Direct libraries. In that case you would use them to set up the Android phone to create a "Group owner", which basically is the same as it being a portable hotspot. Check out:

但是,您也可以使用 Androids WiFi-Direct 库。在这种情况下,您可以使用它们来设置 Android 手机以创建“组所有者”,这与作为便携式热点基本相同。查看:

http://developer.android.com/guide/topics/connectivity/wifip2p.html

http://developer.android.com/guide/topics/connectivity/wifip2p.html

I'll give you a code example to help you get started.

我会给你一个代码示例来帮助你开始。

public class WifiDirectAPtestActivity extends Activity 
{
private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;

public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
    this.isWifiP2pEnabled = isWifiP2pEnabled;
}
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 // add necessary intent values to be matched.

    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    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, getMainLooper(), null);
}
/** register the BroadcastReceiver with the intent values to be matched */
@Override
public void onResume() {
    super.onResume();
    receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
    registerReceiver(receiver, intentFilter);
    createGroup();
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
    manager.removeGroup(channel, new ActionListener() {

        @Override
        public void onFailure(int reasonCode) {
            Log.d("WifiDirectAptestActivity", "Disconnect failed. Reason :" + reasonCode);

        }

        @Override
        public void onSuccess() {
            Log.d("WifiDirectAptestActivity", "Should have been sucessfully removed");
        }

    });
}
public void createGroup()
{
    manager.createGroup(channel, new ActionListener() {

        @Override
        public void onSuccess() {
            // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
            Log.d("WifiDirectAPtestActivity", "Group creating request successfully send");
        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(WifiDirectAPtestActivity.this, "Connect failed. Retry.",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

In addition you'll need the broadcast receiver, look at the WiFi-Direct demo and it should be clear to you.

此外,您还需要广播接收器,查看 WiFi-Direct 演示,您应该很清楚。

Note that line manager.createGroup(channel, new ActionListener() is the codeline of interest, it is this line that actually sets up the device as a portable hotspot.

请注意,行 manager.createGroup(channel, new ActionListener() 是感兴趣的代码行,正是这一行实际将设备设置为便携式热点。

Hope this clarifies things, I don't really know how detailed explanation you need. Comment if some things are not clear.

希望这能澄清事情,我真的不知道你需要多详细的解释。如果有些事情不清楚,请发表评论。