Posts

Showing posts from February, 2017

#CodeSnippet - Get android system IP Address

//Create an instance of WifiManager (API for managing all aspects of Wi-Fi connectivity) WifiManager wifiManager= (WifiManager) getSystemService(WIFI_SERVICE); //and in case of fragments WifiManager wifiManager= (WifiManager) getActivity().getSystemService(WIFI_SERVICE); //Returns a string in the canonical IPv4 format String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()); A warning says : "The method formatIpAddress(int) from the type Formatter is deprecated". This method was deprecated in API level 12. Use getHostAddress(), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses. as per android docs Android Docs So use formatIpAddress() because it works and add @SuppressWarnings("deprecation") to get rid of the warning. //Create an instance of WifiManager (API for managing all aspects of Wi-Fi connectivity) WifiManager wifiManager= (WifiManager) getSystemService(WIFI_SERVICE); //WifiI...

#CodeSnippet - Reverse a byte array in android

Reverse a byte array using the following code public static void reverse(byte[] reverseArray) { // if array is empty return null if (reverseArray == null) { return; } //start index of array int i = 0; // get the length int j = reverseArray.length - 1; byte tmp; //for storing values for exchange purpose while (j > i) { tmp = array[j]; array[j] = array[i]; array[i] = tmp; j--; i++; } }