Bạn có thể tìm nạp địa chỉ IP cục bộ của cả hai đồng nghiệp và so sánh chúng với IP của chủ sở hữu nhóm. Như bạn đã biết bạn có thể dễ dàng có được chủ sở hữu nhóm IP với dòng mã này:
WifiP2pInfo.info.groupOwnerAddress.getHostAddress();
Đối với IP địa phương bạn chỉ có thể sử dụng này:
localIp = getDottedDecimalIP(getLocalIPAddress());
với các phương pháp có liên quan dưới đây:
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";
}
}
bạn có nhận được câu trả lời ???? nếu có, xin vui lòng chia sẻ, tôi cần nó. –