【问题标题】:Wi-Fi Direct Android无线直连安卓
【发布时间】:2013-05-04 09:25:43
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

    标签: android serversocket wifi-direct wifip2p


    【解决方案1】:

    您可以通过反射删除所有组,但这有点小技巧,以后班级成员可能会改变

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • GO 是指拥有群组所有凭据的群组所有者。GO 会持续发送信标,以便任何新设备发现它。愿意加入群组的新设备应经过身份验证和关联过程组所有者 (GO)。
      • 如何将设备的ip地址发送到GO?
      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多