簡體   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