Getting the UDID or serial number of a device from Appium

I am using Appium on a Mac, with iOS devices.

I have a web service which needs to be notified when I begin running tests on my device. To send this request the web service (from my Mac), I require the UDID (of the iOS device) for the web service to recognize it.

To do this, there are 2 ways, I believe.

Method 1:
Some way to get the plugged in devices UDID from within the Appium code. Is there a command for this? I don’t know if this is possible but I’m hoping it is.

Method 2:
I already have my own iOS app which can get the UDID/serial number but I have no way of transferring this to Appium. How about saving this number to the devices clipboard via my app, and then can Appium get the contents of the clipboard as a String? Or how about getting my iOS app to save the UDID using Shared User Defaults. Can Appium access these key value pairs?

Any advice would be great.

Edit:

To be clear, I wish to get the UDID of the device in which the test case is currently running on, via the Appium source code. I do not wish to get the UDID’s of all the devices connected to the Mac. Is this possible?

To get the udid of the connected physical device I wrote my short script that parses it from from mac system_profiler command and passes as a parameter to the appium startup script:

udid=$(system_profiler SPUSBDataType | grep "Serial Number: " | tail -c 41)

if [ "$udid" == "" ]; then
	echo "No connected device found"
	exit 7
fi

fullname=$(instruments -s devices | grep $udid)	

echo "found device with full name $fullname using it to startup the Appium server"

/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js --address 127.0.0.1 --bootstrap-port 4725 --local-timezone --log-level info --force-iphone --log-timestamp --native-instruments-lib --log-no-colors --udid $udid

Thanks for your answer. I would like to know, should I execute this in the terminal?

Also, this will return all the connected iOS devices, and their UDID’s correct? Is there anyway, from within the source code of Appium, to get the UDID of the device in which the test case is running?

As well as I know there is no API for that, your method 2 seems more realistic - you can pass the device udid as an accessibility value/label/identifier of the one of the app’s ui controls

Thanks, so if I pass the UDID to a label in my app (which is separate from the app that Appium is testing), will Appium be able to access the label in that other app?

I don’t sure if the switch between the applications will work, you probably will need to reconnect to your appium server with a new capabilities set if you want to do that. Isn’t preferable to embed this code into the application under the test?

Isn’t preferable to embed this code into the application under the test?

Can you embed Objective C code into Appium tests?

If you meant embedding this code into the app that I’m testing, that cannot be done as that apps source code cannot be modified.

I opened an issue in appium source repository, https://github.com/appium/appium/issues/4734

@vladisx, How to use this script inside my script. Could you please elaborate about it.

Got it!!

@donga, Please try this.

public String deviceUDID() throws IOException{
		ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "/usr/local/bin/idevice_id -l");
		builder.redirectErrorStream(true);
		Process p = builder.start();
		BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line;
		line = r.readLine();
		System.out.println(line);
		return line;
	}

But what about windows?