如何获取Android中WiFi Direct(WiFi-P2P)对等设备的IP地址?
我正在尝试在Android中实现WiFi Direct(WiFi-P2P)。我在如何获取Android中WiFi Direct(WiFi-P2P)对等设备的IP地址?,android,wifi-direct,Android,Wifi Direct,我正在尝试在Android中实现WiFi Direct(WiFi-P2P)。我在samples\android-19\legacy\WiFiDirectDemo中引用了示例代码 我在phone-A上安装了WiFiDirectDemo.apk,并运行它。phone-B在Android设置中打开WiFi-Direct(WiFi-P2P) 在phone-A连接到phone-B后,它会在phone-A上显示以下信息 代码如下所示: @Override public void onConnect
samples\android-19\legacy\WiFiDirectDemo
中引用了示例代码
我在phone-A
上安装了WiFiDirectDemo.apk
,并运行它。phone-B
在Android设置中打开WiFi-Direct(WiFi-P2P)
在phone-A
连接到phone-B后,它会在phone-A
上显示以下信息
代码如下所示:
@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);
}
phone-A
是组所有者
。我想把TCP数据从phone-A
发送到phone-B
1。如何获取电话B的IP地址强>
2。是组所有者IP
指电话-A的IP地址
2,是的,即电话-A IP地址
1,使用此地址,Phone-B可以向Phone-a发送消息。在Phone-a设备上,在Phone-a捕获Phone-B的消息后,您可以从套接字读取发件人(Phone-B)的IP地址。要获取IP地址,必须使用以下方法
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; }
*/
}
public静态字符串getIpAddress(){
试一试{
列表接口=集合
.list(NetworkInterface.getNetworkInterfaces());
/*
*对于(NetworkInterface NetworkInterface:interfaces){Log.v(TAG,
*“接口名称”+networkInterface.getName()+“mac=”+
*getMACAddress(networkInterface.getName());}
*/
用于(网络接口intf:接口){
如果(!getMACAddress(intf.getName()).equalsIgnoreCase(
Globals.thisDeviceAddress){
//Log.v(标记“忽略接口”+intf.getName());
//继续;
}
如果(!intf.getName().包含(“p2p”))
继续;
Log.v(标签,
intf.getName()+“”+getMACAddress(intf.getName());
列表地址=Collections.List(intf
.getInetAddresses());
用于(InetAddress地址:地址){
//日志v(标签“内部”);
如果(!addr.isLoopbackAddress()){
//Log.v(标记“isnt loopback”);
字符串sAddr=addr.getHostAddress().toUpperCase();
Log.v(标签,“ip=”+SADD);
布尔isIPv4=InetAddressUtils.isIPv4Address(SADD);
如果(isIPv4){
如果(附件包含(“192.168.49”)){
Log.v(标签,“ip=”+SADD);
返回sAddr;
}
}
}
}
}
}捕获(例外情况除外){
Log.v(标记“解析错误”);
}//目前,请注意例外情况
Log.v(标记“返回空ip地址”);
返回“”;
}
公共静态字符串getMACAddress(字符串接口名){
试一试{
列表接口=集合
.list(NetworkInterface.getNetworkInterfaces());
用于(网络接口intf:接口){
if(interfaceName!=null){
如果(!intf.getName().equalsIgnoreCase(interfaceName))
继续;
}
字节[]mac=intf.getHardwareAddress();
如果(mac==null)
返回“”;
StringBuilder buf=新的StringBuilder();
for(intidx=0;idx0)
buf.deleteCharAt(buf.length()-1);
返回buf.toString();
}
}捕获(例外情况除外){
}//目前,请注意例外情况
返回“”;
/*
*试试{//这就是Linux黑客的回归
*loadFileAsString(“/sys/class/net/”+interfaceName+
*“/address”).toUpperCase().trim();}catch(IOException ex){return
*空;}
*/
}
可能重复的请参阅我的回答:请将完整代码发送给我。什么是全局。此设备地址
?那是从哪里来的?