如何在 wifi direct 中获取 Wifip2pInfo 对象
How to get Wifip2pInfo object in wifi direct
它给出了一个空指针 exception.I 想获取通过 wifi 连接的设备的 IP 地址 direct.How 要做到这一点 谁能解释一下?附上 Screen 看看。
提前致谢。
您可以使用下面的代码片段,当WiFiP2pInfo有连接时它不是空的,但是当没有连接或连接丢失时它会是空的。
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION
.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
manager.requestConnectionInfo(channel,
new ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(
WifiP2pInfo info) {
if (info != null) {
activity.setConnectionInfo(info); // When connection is established with other device, We can find that info from wifiP2pInfo here.
}
}
}
);
} else {
activity.resetData(); // When connection lost then we can reset data w.r.t that connection.
}
}
以下代码有助于查找客户和所有者的地址,
public static String getDestinationDeviceIpAddress(WifiP2pInfo wifiP2pInfo) {
String destinationAddress;
if (wifiP2pInfo.isGroupOwner) {
destinationAddress = WifiDirectUtil.getIPFromMac();
} else {
destinationAddress = wifiP2pInfo.groupOwnerAddress.getHostAddress();
}
return destinationAddress;
}
public static String getIPFromMac() {
BufferedReader br = null;
boolean isFirstLine = true;
String ipAddress = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue;
}
String[] splitted = line.split(" +");
Log.d(TAG, "** length **" + splitted.length);
if (splitted.length >= 4) {
String device = splitted[5];
Log.d(TAG, device);
if (device.contains("p2p")) {
ipAddress = splitted[0];
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ipAddress;
}
并在manifest.xml中添加权限读取外部存储。
它给出了一个空指针 exception.I 想获取通过 wifi 连接的设备的 IP 地址 direct.How 要做到这一点 谁能解释一下?附上 Screen 看看。 提前致谢。
您可以使用下面的代码片段,当WiFiP2pInfo有连接时它不是空的,但是当没有连接或连接丢失时它会是空的。
if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION
.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
manager.requestConnectionInfo(channel,
new ConnectionInfoListener() {
@Override
public void onConnectionInfoAvailable(
WifiP2pInfo info) {
if (info != null) {
activity.setConnectionInfo(info); // When connection is established with other device, We can find that info from wifiP2pInfo here.
}
}
}
);
} else {
activity.resetData(); // When connection lost then we can reset data w.r.t that connection.
}
}
以下代码有助于查找客户和所有者的地址,
public static String getDestinationDeviceIpAddress(WifiP2pInfo wifiP2pInfo) {
String destinationAddress;
if (wifiP2pInfo.isGroupOwner) {
destinationAddress = WifiDirectUtil.getIPFromMac();
} else {
destinationAddress = wifiP2pInfo.groupOwnerAddress.getHostAddress();
}
return destinationAddress;
}
public static String getIPFromMac() {
BufferedReader br = null;
boolean isFirstLine = true;
String ipAddress = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
if (isFirstLine) {
isFirstLine = false;
continue;
}
String[] splitted = line.split(" +");
Log.d(TAG, "** length **" + splitted.length);
if (splitted.length >= 4) {
String device = splitted[5];
Log.d(TAG, device);
if (device.contains("p2p")) {
ipAddress = splitted[0];
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ipAddress;
}
并在manifest.xml中添加权限读取外部存储。