Util.java
Go to the documentation of this file.
00001 package com.ros.turtlebot.apps.rocon;
00002 
00003 import java.lang.reflect.Field;
00004 import java.net.InetAddress;
00005 import java.net.NetworkInterface;
00006 import java.net.SocketException;
00007 import java.util.Collection;
00008 import java.util.Collections;
00009 import java.util.List;
00010 
00011 import org.ros.exception.RosRuntimeException;
00012 
00013 import android.annotation.SuppressLint;
00014 import android.content.Context;
00015 import android.net.wifi.WifiInfo;
00016 import android.net.wifi.WifiManager;
00017 import android.os.Build;
00018 import android.util.Log;
00019 
00020 import com.google.common.collect.Lists;
00021 
00022 public class Util {
00023 
00024         // Hex help
00025                 private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
00026                                 (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
00027                                 (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
00028                                 (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };
00029                                 
00030                 public Util() {
00031                         // TODO Auto-generated constructor stub
00032                 }
00033                 
00034                 public static String getHexString(byte[] raw, int len) {
00035                         byte[] hex = new byte[3 * len];
00036                         int index = 0;
00037                         int pos = 0;
00038 
00039                         for (byte b : raw) {
00040                                 if (pos >= len)
00041                                         break;
00042 
00043                                 pos++;
00044                                 int v = b & 0xFF;
00045                                 hex[index++] = HEX_CHAR_TABLE[v >>> 4];
00046                                 hex[index++] = HEX_CHAR_TABLE[v & 0xF];
00047                                 hex[index++] = ' ';
00048                         }
00049 
00050                         return new String(hex);
00051                 }
00052                 
00053                 public static byte[] concat(byte[]... arrays) {
00054                 int length = 0;
00055                 for (byte[] array : arrays) {
00056                         length += array.length;
00057                 }
00058                 
00059                 byte[] result = new byte[length];
00060                 int pos = 0;
00061                 for (byte[] array : arrays) {
00062                         System.arraycopy(array, 0, result, pos, array.length);
00063                         pos += array.length;
00064                 }
00065                 
00066                 return result;
00067             }
00068                 
00069                 public static String getWifiAddress(WifiManager wifiManager, long timeout ) {
00070                         int interval = 500 ;            // ms
00071                         int count = (int) timeout / interval ;
00072                         int ip = 0 ;
00073                         for(int i = 0 ; i <= count ; i++)
00074                         {
00075                                 WifiInfo wInfo = wifiManager.getConnectionInfo() ;
00076                                 ip = wInfo.getIpAddress() ;
00077                                 if(ip == 0) {
00078                                         try {
00079                                                 Thread.sleep(interval);
00080                                         } catch (InterruptedException e) { e.printStackTrace();}
00081                                 }
00082                                 else
00083                                         break ;
00084                         }
00085                         
00086                         String ipString = String.format("%d.%d.%d.%d", (ip & 0xff), (ip>>8 & 0xff), (ip>>16 & 0xff), (ip>>24 & 0xff));
00087                         return  ipString ;
00088                 }
00089                 
00090 
00091             private static Collection<InetAddress> getAllInetAddresses() {
00092                 List<NetworkInterface> networkInterfaces;
00093                 try {
00094                   networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
00095                 } catch (SocketException e) {
00096                   throw new RosRuntimeException(e);
00097                 }
00098                 List<InetAddress> inetAddresses = Lists.newArrayList();
00099                 for (NetworkInterface networkInterface : networkInterfaces) {
00100                   inetAddresses.addAll(Collections.list(networkInterface.getInetAddresses()));
00101                 }
00102                 return inetAddresses;
00103               }
00104             
00105             public static InetAddress getSiteLocalAddress() {
00106                 for(InetAddress address : getAllInetAddresses()) {
00107                         if( !address.isLoopbackAddress() && !address.isLinkLocalAddress() && address.isSiteLocalAddress())
00108                                 return address ;
00109                     }
00110                 
00111                 return null ;
00112             }
00113             
00114             @SuppressLint("NewApi")
00115                 public static String getAndroidVersionName() {
00116                 String name = "";
00117                 int version = -1 ;
00118                 for(Field field : Build.VERSION_CODES.class.getFields()) {
00119                         try {
00120                                         version = field.getInt(new Object());
00121                                 } catch (IllegalArgumentException e) { e.printStackTrace(); } 
00122                         catch (IllegalAccessException e) {      e.printStackTrace(); }
00123                         
00124                         if(version == Build.VERSION.SDK_INT) {
00125                                 name = field.getName() ;
00126                                 Log.d("Util", "Android Version Name = " + name);
00127                                 return name ;                   
00128                         }
00129                 }
00130                 Log.d("Util", "Cannot find Android Version Name");
00131                 return name ;
00132             }
00133                 
00134                 
00135 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28