두 피어의 로컬 IP 주소를 가져 와서 그룹 소유자 IP와 비교할 수 있습니다. 당신은 이미 당신이 쉽게 코드 줄에 그룹 소유자 IP를 얻을 수 있습니다다시피 :
WifiP2pInfo.info.groupOwnerAddress.getHostAddress();
로컬 IP를 들어 당신은 단순히 이것을 사용할 수 있습니다 :
localIp = getDottedDecimalIP(getLocalIPAddress());
를 아래의 관련 방법과 :
private byte[] getLocalIPAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
if (inetAddress instanceof Inet4Address) {
return inetAddress.getAddress();
}
}
}
}
} catch (SocketException ex) {
// Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
} catch (NullPointerException ex) {
// Log.e("AndroidNetworkAddressFactory", "getLocalIPAddress()", ex);
}
return null;
}
private String getDottedDecimalIP(byte[] ipAddr) {
if (ipAddr != null) {
String ipAddrStr = "";
for (int i = 0; i < ipAddr.length; i++) {
if (i > 0) {
ipAddrStr += ".";
}
ipAddrStr += ipAddr[i] & 0xFF;
}
return ipAddrStr;
} else {
return "null";
}
}
답변을 얻었습니까 ???? 그렇다면 공유하십시오, 필요합니다. –