Wi-Fi Direct Android

3

我想通过Wi-Fi Direct在两台设备之间传输文件。

我想做与WifiDirectDemo相同的事情,但无法从群主传输数据到另一台设备,所以我尝试了这个方法:每次其中一个设备点击连接时,另一台设备被设置为群主,因此在每次连接时,请求连接的设备始终是客户端并可以发送数据。

问题在于Android始终记住第一个创建的群组和其群主。换句话说,我所做的只有第一次有效,除非我进入设置并忘记第一个连接创建的群组。

我知道通过使用断开按钮可以删除Wi-Fi组,但Android系统会将其放入已记住的群组中,并在需要进行新连接时使用其设置(群主协商)。

我尝试的第二件事是在每个设备上创建一个ServerSocket(在另一个端口),这样群主和其他设备都将同时充当客户端和服务器。我不知道群主是否可以被设置为客户端,但我不能在两个设备上都创建ServerSocket

以下是我的代码:

<pre>
  @Override
    public void onConnectionInfoAvailable(final WifiP2pInfo 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),8988)
                    .execute();
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            Log.d(WiFiDirectActivity.TAG, "serveur8988cree");
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case, we enable the
            // Get file button.
            // In this case we create a server socket on another port
            new FileServerAsyncTask(getActivity(), mContentView.findViewById(R.id.status_text),8987)
            .execute();
            mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
            Log.d(WiFiDirectActivity.TAG, "serveur8987cree");
            ((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources()
                    .getString(R.string.client_text));
        }
</pre>

感谢您的帮助。
2个回答

3

您可以通过反射删除所有组,但这有点像黑客行为,而且类成员以后可能会更改。

 private void deletePersistentInfo() {
    try {

        Class persistentInterface = null;

        //Iterate and get class PersistentGroupInfoListener
        for (Class<?> classR : WifiP2pManager.class.getDeclaredClasses()) {
            if (classR.getName().contains("PersistentGroupInfoListener")) {
                persistentInterface = classR;
                break;
            }

        }

        final Method deletePersistentGroupMethod = WifiP2pManager.class.getDeclaredMethod("deletePersistentGroup", new Class[]{Channel.class, int.class, ActionListener.class});




        //anonymous class to implement PersistentGroupInfoListener which has a method, onPersistentGroupInfoAvailable
        Object persitentInterfaceObject =
                java.lang.reflect.Proxy.newProxyInstance(persistentInterface.getClassLoader(),
                        new java.lang.Class[]{persistentInterface},
                        new java.lang.reflect.InvocationHandler() {
                            @Override
                            public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {
                                String method_name = method.getName();

                                if (method_name.equals("onPersistentGroupInfoAvailable")) {
                                    Class wifiP2pGroupListClass =  Class.forName("android.net.wifi.p2p.WifiP2pGroupList");
                                    Object wifiP2pGroupListObject = wifiP2pGroupListClass.cast(args[0]);

                                    Collection<WifiP2pGroup> wifiP2pGroupList = (Collection<WifiP2pGroup>) wifiP2pGroupListClass.getMethod("getGroupList", null).invoke(wifiP2pGroupListObject, null);
                                    for (WifiP2pGroup group : wifiP2pGroupList) {
                                        deletePersistentGroupMethod.invoke(wifiP2pManager, channel, (Integer) WifiP2pGroup.class.getMethod("getNetworkId").invoke(group, null), new ActionListener() {
                                            @Override
                                            public void onSuccess() {
                                                //All groups deleted
                                            }

                                            @Override
                                            public void onFailure(int i) {

                                            }
                                        });
                                    }
                                }

                                return null;
                            }
                        });

        Method requestPersistentGroupMethod =
                WifiP2pManager.class.getDeclaredMethod("requestPersistentGroupInfo", new Class[]{Channel.class, persistentInterface});

        requestPersistentGroupMethod.invoke(wifiP2pManager, channel, persitentInterfaceObject);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

3
为了发送数据,您需要知道接收方的IP地址(而不是设备地址)。对于P2P客户端,group_owner的IP地址可以在WifiP2pInfo变量中获得,因此它可以使用这个IP地址将数据发送给群主。如果群主知道要发送数据的P2P客户端的IP地址,则也可以发送文件。有两种方法可以实现这一点。
  1. 群主分配IP地址给客户端并存储相关信息。
  2. 每个新加入群组的客户端在加入时向群主发送其IP地址。

1
GO指的是组所有所有组凭证。GO会不断地发出信标以便新设备能够发现它。一个想要加入该组的新设备应该经过认证和与组所有者(GO)关联的过程。 - Chait
你如何将设备的IP地址发送给GO? - AndroGeek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接