How to get device id


#1

Hello,

Is there a way to get the device id and run few scripts. for example
if (Deviceid == 4100951cf20c1215")) {
runthismethod();
}
else
{
runAnothermetod();
}

Please help me to get the device id and store it in a variable.


#2

Do you mean the uuid? You don’t mention what platform so I’ll give you iOS:

‘instruments -s devices’

In my test framework I call this, parse the results and match the device name with the one the user put in settings.


#3

@wreed

I am looking for android. Can you please help.


#4

Find Android device’s model name:

adb devices -l

Find Android device’s version no:

adb shell getprop ro.build.version.release

#5

But how do I add this in the appium code. This I run from the terminal right?


#6

In my framework we run a command like this in the setup, and then start the server using this value. You can make a system call from whatever language you are using to adb. You will probably need to parse the results and store the uuid in a variable.


#7

Connect your android mobile to your computer using USB cable.
Then type below command in the terminal
adb devices -l

It will display all the connected device to your comaputer with device IDs.

And you can pass the device ID to your code using DesiredCapabilities
eg: if your device id is f23a5A83,

DesiredCapabilities Androidcapability=new DesiredCapabilities();
Androidcapability.setCapability(“udid”, “f23a5A83”);


#8

Below is the code I tried and works fine

java.lang.Runtime rt = java.lang.Runtime.getRuntime();
// Start a new process: UNIX command ls
java.lang.Process p = rt.exec(“adb devices”);
// Get process’ output: its InputStream
java.io.InputStream is = p.getInputStream();
java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
// And print each line
String s= (reader.readLine());

	while ((s = reader.readLine()) != null) {
	System.out.println(s);
	if(s.contains("4100951cf20c1215"))
	{
		System.out.println("+++++++++++++++00000000000000");
	}
	if(s.contains("LGH815e29d29f9"))
	{
		System.out.println("___________________0000000000000000000");
	}
	if(s.contains("TA99301VXY"))
	{
		System.out.println("xxxxxxxxxxxxxxx0000000000000000000");
	}
	
	}
	is.close();

#9

There are a couple known ways to retrieve Android device IDs. The first, most straightforward method is already listed in this thread: Spawn a child process that runs “adb devices” and then parse the results that are returned on the child’s standard out. Although this will work, I feel that it’s a bit shaky since your parent process will need a PATH environment variable that contains adb, and parsing the results is often frustrating to set up.

For Java, I think a better option is to include the same device library the Android SDK uses into your own test framework. Check out ddmlib and a ddmlib demo to get a feel for how to use it to retrieve devices. The downside is that you still have to be able to find adb, but you won’t have to deal with parsing the results.


#10

Can any one help to get the version and device id for IOS device.


#11

@Harish_pedamallu check the first reply to this topic made by wreed.


#12

Hi, Can you show me Example how you do it ? call ‘instruments -s devices’ from framwork (Im using JAVA) and, parse the results (UDID) to String Array List ?

Thanks !!! :slight_smile:


#13

Here is a tutorial on making a system call in Java:

https://www.mkyong.com/java/how-to-execute-shell-command-from-java/

The example above from @tapanagkumar is a good one.


#14

I will try it,

Thanks ! :slight_smile: