通常javaだとInetAddressを利用するそうですが
例外が発生しうまく取得できなかったので以下のコマンドで取得する方法を試みました。
他にいい方法をご存知の方がいらっしゃいましたら、ご教示お願い致します。
public static String getLocalIpAddr() {
return SotaUtils.getLocalIpAddr("wlan0");
}
public static String getLocalIpAddr(String interfaceName) {
try {
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec("ip route");
InputStream is = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String b = null;
int i = 0;
while ( ( b = reader.readLine() ) != null ) {
if( i == 0) { i++; continue; }
if( b.contains(interfaceName) ) {
String[] split = b.split( " src ", 0);
if( split.length >= 1 ) {
return split[ split.length - 1 ];
}
}
i++;
}
} catch (IOException ex) {
}
return null;
}