如何在Android中为WiFi-Direct(WiFi-P2P)获取对等设备的IP地址?

Mar*_*tin 5 android wifi-direct

我正在尝试WiFi-Direct(WiFi-P2P)在Android中实现.我参考了示例代码samples\android-19\legacy\WiFiDirectDemo.

我安装WiFiDirectDemo.apkphone-A并运行它.在phone-B上转WiFi-Direct(WiFi-P2P)Android Setting.

phone-A连接到电话-B ,它显示出对以下信息phone-A.

在此输入图像描述

代码如下:

@Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {
        Log.d(WifiP2P.TAG, "onConnectionInfoAvailable----------- " + info);
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        this.info = info;
        this.getView().setVisibility(View.VISIBLE);

        // The owner IP is now known.
        TextView view = (TextView) mContentView.findViewById(R.id.group_owner);
        view.setText(getResources().getString(R.string.group_owner_text)
                + ((info.isGroupOwner == true) ? getResources().getString(R.string.yes)
                        : getResources().getString(R.string.no)));

        // InetAddress from WifiP2pInfo struct.
        view = (TextView) mContentView.findViewById(R.id.device_info);
        view.setText("Group Owner IP - " + info.groupOwnerAddress.getHostAddress());

        // After the group negotiation, we assign the group owner as the file
        // server. The file server is single threaded, single connection server
        // socket.
        if (info.groupFormed && info.isGroupOwner) {
            new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text))
                    .execute();
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case, we enable the
            // get file button.
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            ((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources()
                    .getString(R.string.client_text));
        }

        // hide the connect button
        mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);                 
    }
Run Code Online (Sandbox Code Playgroud)

phone-AGroup Owner.我想从发送TCP数据phone-Aphone-B.

1.我如何获得phone-BIP地址.?

2. Group Owner IP手段IP address是什么?Phone-A

小智 0

2、是的,就是Phone-A的IP地址。

1、通过这个地址Phone-B可以向Phone-A发送消息。在 Phone-A 设备上,当 Phone-A 捕获 Phone-B 的消息后,您可以从套接字读取发送者(Phone-B)的 IP 地址。