Data transfer to iOS devices from PC using USB cable (using USB Multiplex Daemon)

Posted By : Ankush Gulati | 25-Oct-2012

There are several ways if a developer wants to make an app to transfer data from a PC to iOS devices like using iCloud or making a web service etc. However, if transfer is to take place through the dock cable(USB cable) of iDevice, then only 1 option stands out from the rest and that is to enable 'UI FILE SHARING' after which, users can see the app in iTunes when device is attached and can manually drag and drop files to be transferred.
But, say if an app requires an automatic syncing to a particular folder on pc over USB cable whenever the device is connected (No internet or ad hoc connection), it becomes a cumbersome task to do so.

There is a daemon called USBMUXD(USB Multiplexer Daemon). iTunes itself makes use of this daemon to communicate to iDevice over usb cable. USBMUXD uses the process of Port Forwarding. It basically directs the data sent over a local port to a remote port on iDevice over the USB cable.Below is the explained process of how to use USBMUXD to communicate to iDevice app built in Titanium:

1. Download the latest version from this repo http://cgit.sukimashita.com/usbmuxd.git/
It is basically a python script for communication over USB. So, python must be installed and configured properly on your system in order to use this script. Download the latest .zip or .tar file.

2. Extract the contents of downloaded folder and navigate to 'python-client' subfolder. You will see 2 python scripts 'tcprelay.py' and 'usbmux.py'.

3. Assuming that pc has Windows OS and python is configured, next step is to open the command prompt and 'cd' into 'python-client' folder. Similar steps on other OS as well.

4. Now type this command and press enter.
python tcprelay.py -t 20000:40000
Now, connect the iDevice to your pc. You will see the information regarding the iDevice in the command prompt.

Note: The script 'tcprelay.py' forwards the local port 40000 to remote port 20000. which means any data directed over to local port 40000 will be directed to iDevice's 20000 port, if connected.

5. These port numbers can be changed according to requirements. Now, we have forwarded the local port to remote port but in order to make data transfer, we'll a need a program on pc that sends the data to local port no. say '40000' and accordingly, we'll need the iOS app that will listen to the port no. say '20000' at the same time.

6. For demonstration purpose, you can write a simple java program which will transmit the contents of a pre-defined folder to port no. '40000' byte by byte.

7. As mentioned previously, iOS app will need to 'listen' to port no. '20000'. an example of socket listener in Titanium is as follows:

		function pumpCallback(e) {
		if (e.bytesProcessed == -1) {// EOF
			e.source.close();
			removeSocket(e.source);
			 
		} else if (e.errorDescription == null || e.errorDescription == "") {//when actual data is being transferred
			//Place the actual processing code here…
			//what to do with the received data

		} else {
			alert("READ ERROR: " + e.errorDescription);
		}
	}

	var acceptedCallbacks = {
		error : function(e) {
			Ti.UI.createAlertDialog({
				title : "Socket error: " + e.socket.host,
				message : e.error
			}).show();
			removeSocket(e.socket);
		}
	};

	var socket = Titanium.Network.Socket.createTCP({
		host : 'localhost',
		port : 20000,		//as defined in ur command prompt's command

		accepted : function(e) {
			var sock = e.inbound;
			connectedSockets.push(sock);

			Ti.Stream.pump(sock, pumpCallback, 4096, true);
			socket.accept(acceptedCallbacks);
		},
		closed : function(e) {
			alert("Closed listener");
		},
		error : function(e) {
			Ti.UI.createAlertDialog({
				title : "Listener error: " + e.errorCode,
				message : e.error
			}).show();
		}
	   });
        

Note: The above code snippet can be divided into three tasks. The function pumpCallback() receives the actual data and defines what to do with this data. The function acceptedCallbacks() defines what to do in case of errors encountered and the last one actually creates the network socket, defines it's properties such as port number, max. size of a packet etc. 

After everything explained above is done, you just have to follow these steps:

  • Start the python script as explained in step 4
  • Connect your iDevice to system and launch the app containing socket listener.
  • Now, start your java program to write the data to local port previously defined.
  • Now, you will notice that the data is received by the iPad app.
  • Always make sure that the port numbers defined are not already in use.

Hope it helps :)
Ankush Gulati [email protected]
http://www.oodlestechnologies.com

About Author

Author Image
Ankush Gulati

Request for Proposal

Name is required

Comment is required

Sending message..