Created
November 18, 2017 01:20
-
-
Save pavanbachu0604/0e3d9f6a06fe455bea403e9f04cda2db to your computer and use it in GitHub Desktop.
UDID using java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final int IOS_UDID_LENGTH = 40; | |
String commandforUDID = "system_profiler SPUSBDataType | sed -n -E -e '/(iPhone|iPad|iPod)/" | |
+ ",/Serial/s/ *Serial Number: *(.+)/\\1/p'"; | |
String testDevice = "idevice_id -l"; | |
List<String>iOSUDID = new LinkedList<String>(); | |
public List<String> getUDIDS(){ | |
try { | |
int startPos = 0; | |
int endPos = IOS_UDID_LENGTH - 1; | |
Optional<String> getIOSDeviceID = Optional.of(executeTerminalCommand(commandforUDID)); | |
while (endPos < getIOSDeviceID.get().length()) {iOSUDID.add(getIOSDeviceID.get().substring(startPos, endPos + 1) .replace("\n", "")); | |
startPos += IOS_UDID_LENGTH; | |
endPos += IOS_UDID_LENGTH; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
throw new IllegalStateException("Failed to fetch iOS device connected"); | |
} | |
public String executeTerminalCommand(String command) throws IOException{ | |
BufferedReader br = executeCommandonTerminal(command); | |
String line; | |
String allLine = ""; | |
while ((line = br.readLine()) != null) { | |
allLine = allLine.trim() + "" + line.trim() + "\n"; | |
} | |
return allLine.trim(); | |
} | |
public BufferedReader executeCommandonTerminal(String command) throws IOException { | |
List<String> commands = new ArrayList<>(); | |
commands.add("/bin/sh"); | |
commands.add("-c"); | |
commands.add(command); | |
ProcessBuilder builder = new ProcessBuilder(commands); | |
final Process process = builder.start(); | |
InputStream is = process.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(is); | |
return new BufferedReader(isr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment