How to get device id

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.

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.

@wreed

I am looking for android. Can you please help.

Find Android deviceā€™s model name:

adb devices -l

Find Android deviceā€™s version no:

adb shell getprop ro.build.version.release

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

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.

1 Like

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ā€);

1 Like

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();

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.

1 Like

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

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

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:

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.

I will try it,

Thanks ! :slight_smile: