3

Wi-Fi Direct 経由で 2 つのデバイス間でファイルを転送したいと考えています。

WifiDirectDemo と同じことをしたかったのですが、グループの所有者から他のデバイスにデータを転送できないので、これを試しました: デバイスの 1 つが接続をクリックするたびに、他のデバイスがグループとして設定されます。したがって、各接続で接続を要求するデバイスは常にクライアントであり、データを送信できます。

これに関する問題は、Android が最初に作成されたグループを常に記憶しているため、そのグループ所有者であることです。つまり、設定に移動して最初の接続で作成されたグループを忘れない限り、私が行ったことは初めて機能します。

切断ボタンを使用すると Wi-Fi グループが削除されることはわかっていますが、Android システムはそれを記憶されたグループに入れ、新しい接続が確立されるときにその設定 (グループ所有者のネゴシエーション) を使用します。

2 番目に試みたのは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>

手伝ってくれてありがとう。

4

2 に答える 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();
    }
}
于 2014-09-04T11:50:47.340 に答える
3

データを送信するには、受信者の IP アドレス (デバイス アドレスではない) を知る必要があります。P2P クライアントの場合、の IP アドレスがgroup_owner変数WifiP2pInfoで使用できるため、これを使用してグループ所有者にデータを送信できます。グループの所有者が、データの送信先の P2P クライアントの IP アドレスを知っている場合は、ファイルも送信できます。これは 2 つの方法で実現できます。

  1. グループ所有者はクライアントに IP アドレスを割り当て、それに関する情報を保存します。
  2. 新しく追加されたすべてのクライアントは、グループへの参加時にグループ所有者に IP アドレスを送信します。
于 2013-06-05T11:01:46.870 に答える