2014-10-15 14 views
5

Sto cercando di implementare il WiFi-Direct(WiFi-P2P) in Android. Ho riferimento il codice di esempio in samples\android-19\legacy\WiFiDirectDemo.Come ottenere l'indirizzo IP del dispositivo peer in Android per WiFi-Direct (WiFi-P2P)?

Installare il WiFiDirectDemo.apk su phone-A ed eseguirlo. Lo phone-B accendere lo WiFi-Direct(WiFi-P2P) in Android Setting.

Dopo phone-A connettersi allo telefono-B, mostra le seguenti informazioni su phone-A.

enter image description here

E il codice è simile al seguente:

@Override 
    public void onConnectionInfoAvailable(final WifiP2pInfo info) { 
     Log.d(WifiP2P.TAG, "onConnectionInfoAvailable----------- " + 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)) 
        .execute(); 
     } else if (info.groupFormed) { 
      // The other device acts as the client. In this case, we enable the 
      // get file button. 
      mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE); 
      ((TextView) mContentView.findViewById(R.id.status_text)).setText(getResources() 
        .getString(R.string.client_text)); 
     } 

     // hide the connect button 
     mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);     
    } 

Il phone-A è Group Owner. E voglio inviare i dati TCP da phone-A a phone-B.

1. Come ottengo l'indirizzo IP di phone-B. ?

2. È il Group Owner IP significa che il, sì, cioè Phone-Un indirizzo IP IP address di Phone-A

+0

Eventuali duplicati di [Come ottenere l'indirizzo IP di ogni dispositivo Wi-Fi scenario diretto?] (Http://stackoverflow.com/questions/10053385/how-to-get-each-devices-ip- address-in-wi-fi-direct-scenario) –

+0

riferirsi alla mia risposta qui: http://stackoverflow.com/a/43437529/3260008 – Amos

+0

Per favore mandami il codice completo. –

risposta

0

2.

1, con questo indirizzo Phone-B può inviare un messaggio a Phone-A. Sul telefono: un dispositivo, è possibile leggere l'indirizzo IP del mittente (Telefono-B) dal socket, dopo il messaggio Telefono-A di Phone-A rilevato.

0

Per ottenere l'indirizzo IP, è necessario utilizzare il seguente metodo.

public static String getIpAddress() { 
    try { 
     List<NetworkInterface> interfaces = Collections 
       .list(NetworkInterface.getNetworkInterfaces()); 
     /* 
     * for (NetworkInterface networkInterface : interfaces) { Log.v(TAG, 
     * "interface name " + networkInterface.getName() + "mac = " + 
     * getMACAddress(networkInterface.getName())); } 
     */ 

     for (NetworkInterface intf : interfaces) { 
      if (!getMACAddress(intf.getName()).equalsIgnoreCase(
        Globals.thisDeviceAddress)) { 
       // Log.v(TAG, "ignore the interface " + intf.getName()); 
       // continue; 
      } 
      if (!intf.getName().contains("p2p")) 
       continue; 

      Log.v(TAG, 
        intf.getName() + " " + getMACAddress(intf.getName())); 

      List<InetAddress> addrs = Collections.list(intf 
        .getInetAddresses()); 

      for (InetAddress addr : addrs) { 
       // Log.v(TAG, "inside"); 

       if (!addr.isLoopbackAddress()) { 
        // Log.v(TAG, "isnt loopback"); 
        String sAddr = addr.getHostAddress().toUpperCase(); 
        Log.v(TAG, "ip=" + sAddr); 

        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 

        if (isIPv4) { 
         if (sAddr.contains("192.168.49.")) { 
          Log.v(TAG, "ip = " + sAddr); 
          return sAddr; 
         } 
        } 

       } 

      } 
     } 

    } catch (Exception ex) { 
     Log.v(TAG, "error in parsing"); 
    } // for now eat exceptions 
    Log.v(TAG, "returning empty ip address"); 
    return ""; 
} 

public static String getMACAddress(String interfaceName) { 
     try { 
      List<NetworkInterface> interfaces = Collections 
        .list(NetworkInterface.getNetworkInterfaces()); 

      for (NetworkInterface intf : interfaces) { 
       if (interfaceName != null) { 
        if (!intf.getName().equalsIgnoreCase(interfaceName)) 
         continue; 
       } 
       byte[] mac = intf.getHardwareAddress(); 
       if (mac == null) 
        return ""; 
       StringBuilder buf = new StringBuilder(); 
       for (int idx = 0; idx < mac.length; idx++) 
        buf.append(String.format("%02X:", mac[idx])); 
       if (buf.length() > 0) 
        buf.deleteCharAt(buf.length() - 1); 
       return buf.toString(); 
      } 
     } catch (Exception ex) { 
     } // for now eat exceptions 
     return ""; 
     /* 
     * try { // this is so Linux hack return 
     * loadFileAsString("/sys/class/net/" +interfaceName + 
     * "/address").toUpperCase().trim(); } catch (IOException ex) { return 
     * null; } 
     */ 
    } 
Problemi correlati