客戶端連接到創建的接入點後,您可以獲得接入點IP地址,如下所示。
public static String getAccessPointIpAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
try {
String ip = InetAddress.getByAddress(ipAddress).getHostAddress();
return ip.replace("/", "");
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
private static byte[] convert2Bytes(int hostAddress) {
byte[] addressBytes = {(byte) (0xff & hostAddress),
(byte) (0xff & (hostAddress >> 8)),
(byte) (0xff & (hostAddress >> 16)),
(byte) (0xff & (hostAddress >> 24))};
return addressBytes;
}
創建接入點的發送者/接收者將其IP地址作爲主機名啓動ServerSocket。但客戶端需要一個端口號碼,您可以對其進行硬編碼或對SSID進行編碼以在其上嵌入端口號。
而且我已經做出了嘗試編寫一個名爲SHAREthem庫,幫助文件共享&轉讓或者換句話說,它模擬SHAREit是如何工作的。還有this博客解釋實施細節 希望它有幫助。
可能會使用UDP廣播並找出數據報的IP地址 – aksappy