خیلی خب این کد ظاهرا برای بدست آوردن IP تمام اتصالات شبکه ای اندروید جواب میده:
حالا من میتونم این اسمها رو بخونم و به USB و بلوتوث تفسیر کنم، اما چیزی که نمیدونم و مطمئن نیستم اینه که آیا این اسمها در تمام دستگاه های اندروید یکسانه یا نه. اگر یکسان نباشه که نمیشه با اطمینان ازشون استفاده کرد و باید دنبال راه مطمئن تری گشت.
public static void listAllIfaces() {
try{
Enumeration<NetworkInterface> theIntfList = NetworkInterface.getNetworkInterfaces();
List<InterfaceAddress> theAddrList = null;
NetworkInterface theIntf = null;
InetAddress theAddr = null;
while(theIntfList.hasMoreElements()) {
theIntf = theIntfList.nextElement();
if(!theIntf.isUp()) continue;
Logger.d("--------------------");
Logger.d(" " + theIntf.getDisplayName());
Logger.d(" name: " + theIntf.getName());
Logger.d(" loopback?: " + theIntf.isLoopback());
Logger.d(" ptp?: " + theIntf.isPointToPoint());
Logger.d(" virtual?: " + theIntf.isVirtual());
Logger.d(" up?: " + theIntf.isUp());
theAddrList = theIntf.getInterfaceAddresses();
Logger.d(" int addrs: " + theAddrList.size() + " total.");
int addrindex = 0;
for(InterfaceAddress intAddr : theAddrList)
{
addrindex++;
theAddr = intAddr.getAddress();
if(theAddr.getClass().getSimpleName().equals("Inet6Address")) continue;
Logger.d(" " + addrindex + ").");
//Logger.d(" host: " + theAddr.getHostName());
//Logger.d(" class: " + theAddr.getClass().getSimpleName());
//Logger.d(" ip: " + theAddr.getHostAddress() + "/" + intAddr.getNetworkPrefixLength());
//InetAddress inetAddr=theAddr.getAddress();
Logger.d(" ip: " + theAddr.toString());
//Logger.d(" bcast: " + intAddr.getBroadcast().getHostAddress());
//int maskInt = Integer.MIN_VALUE >> (intAddr.getNetworkPrefixLength()-1);
//Logger.d(" mask: " + toIPAddrString(maskInt));
//Logger.d(" chost: " + theAddr.getCanonicalHostName());
//Logger.d(" sitelocal?: " + theAddr.isSiteLocalAddress());
//Logger.d("");
}
}
}
catch (SocketException e) { e.printStackTrace(); }
}
ولی موضوع اینه که متد getDisplayName برای اینترفیس مثلا wifi اسم wlan0 رو چاپ میکنه، برای اتصال USB اسم dis0، برای اتصال بلوتوث bnep0.حالا من میتونم این اسمها رو بخونم و به USB و بلوتوث تفسیر کنم، اما چیزی که نمیدونم و مطمئن نیستم اینه که آیا این اسمها در تمام دستگاه های اندروید یکسانه یا نه. اگر یکسان نباشه که نمیشه با اطمینان ازشون استفاده کرد و باید دنبال راه مطمئن تری گشت.
