繁体   English   中英

Wi-Fi Direct Android

[英]Wi-Fi Direct Android

我想通过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>

感谢帮助。

要发送数据,您需要知道接收器的IP地址(而不是设备地址)。 对于P2P客户端, group_owner的IP地址在WifiP2pInfo变量中可用,因此它可以使用它将数据发送给组所有者。 如果组所有者知道它想要发送数据的P2P客户端的IP地址,那么它也可以发送文件。 这可以通过两种方式实现。

  1. 组所有者将IP地址分配给客户端并存储有关它的信息。
  2. 每个新添加的客户端在加入组时都会将其IP地址发送给组所有者。

您可以通过反射删除所有组,但这有点像黑客攻击,类成员可能会在以后更改

 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();
    }
}

暂无
暂无

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

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