Peer-to-peer Networking (P2P)
6.4 Network an Android with a Desktop PC
For our first project, we’re going to network a desktop PC and an Android device, and then use the WiFi network to exchange data between them.
Wireless local area networks provide us with a high-bandwidth connection that allows us to write applications that let us interact with peers within the network in real-time. We can send fairly large data payloads without noticeable delays, making it a good choice for a diverse range of multiuser applications.
We’ll need to import the networking classes described in Section 6.2, Working with Networking Classes, on page 141 so we can exchange data over a common port, as illustrated in Figure 25, Connecting an Android to a PC, on page 146.
We’ll use oscP5, which builds on and requires Processing’s core netP5 library
6. http://processing.org/reference/libraries/#data_protocols
Chapter 6. Networking Devices with WiFi
•
144for the exchange of data. We also use the KetaiNet class look up the Android’s IP address, and the familiar KetaiSensor class to receive accelerometer data.
Before we get started, let’s make sure that both devices are connected to the same Wi-Fi network. Go ahead and check the Android (Settings → Wireless &
networks) to activate Wi-Fi. If your device is not already connected to a WiFi network, choose the same network from the list to which the PC is connected.
Once connected, write down the IP address that has been assigned to the Android device. On your desktop, check your network settings so it is connect-ed to the same network as the Android. You can use an Ethernet connection for the PC as well, as long as you are connected to the same network as the Android is.
Figure 25—Connecting an Android to a PC. The screen output shows remote mouse data from the PC on the Android screen (left), and accelerometer data from the Android in PC display window (right). The local Android IP address and the remote PC address are shown
at the bottom of the Android screen.
We’ll build this application in two steps: first we’ll write a sketch for the Android device and then for the PC.
Program the Android Device
Before you can connect your Android to the PC, we need to first figure out the IP address of the desktop computer on the local network. Make sure your PC is on the same network as the Android is connected to via WiFi.
• On a Mac, you’ll find your IP address under System Preferences→Network.
• On a PC, check the Control Panel→Network and Internet.
Chapter 6. Networking Devices with WiFi
•
146• On Linux you can go to Administration→Network Tools. My IP address looks like this:
10.0.1.31
❮
Your address most likely looks different. Write it down, as it is not very intu-itive, and we need to get this right for us to connect successfully.
We’ll first code the Android sketch, using the oscP5 NetAddress7 class to specify the destination of the OSC message. We’ll create a NetAddress object called remoteLocation, consisting of the IP address of the remote device—in this case our PC—and the port number (12000) that both devices will use to communi-cate. For this first sketch, the OSC message we send will consist of three floating point numbers, the values of X, Y, and Z axes of the accelerometer which we’ll add() to the message before it’s sent. In turn, we’ll receive three integer values from the desktop PC, consisting of the X and Y position of the mouse cursor, followed by a 0 or 1 depending if the mouse button is pressed (1), or not (0).
Now, let’s take a look at code for the sketch:
Networking/WiFiDataExchangeAndroid/WiFiDataExchangeAndroid.pde
String remoteAddress = "10.0.0.103"; // Customize!
❷
background(78, 93, 75);
text("Remote Mouse Info: \n" +
❸
"mouseX: " + x + "\n" +
"mouseY: " + y + "\n" +
"mousePressed: " + p + "\n\n" +
"Local Accelerometer Data: \n" +
"x: " + nfp(myAccelerometerX, 1, 3) + "\n" +
"y: " + nfp(myAccelerometerY, 1, 3) + "\n" +
"z: " + nfp(myAccelerometerZ, 1, 3) + "\n\n" +
"Local IP Address: \n" + myIPAddress + "\n\n" +
"Remote IP Address: \n" + remoteAddress , width/2, height/2);
}
void onAccelerometerEvent(float x, float y, float z) {
myAccelerometerX = x;
myAccelerometerY = y;
myAccelerometerZ = z;
OscMessage myMessage = new OscMessage("accelerometerData");
❻
oscP5 = new OscP5(this, 12000);
❾
remoteLocation = new NetAddress(remoteAddress, 12000);
❿
myIPAddress = KetaiNet.getIP();
⓫
}
Here’s what the sketch does:
❶ Import the Processing networking library netP5 to read and write data over the network. Import the oscP5 library to send data using the OSC protocol.
Import the Ketai networking class to look up the device’s current IP address, and the KetaiSensor class to work with the accelerometer sensor
Chapter 6. Networking Devices with WiFi
•
148❷ Set the remote IP address variable remoteAddress of the desktop to exchange data with
❸ Print all info about remote mouse position, state, and local accelerometer data. Android accelerometer data myAccelerometerX, myAccelerometerY, myAc-celerometerZ are presented with one digit left and two digits right of the decimal point, and a plus or minus number prefix using the nfp() method.
On the bottom of the screen we display our local Android IP address, fol-lowed by the remote desktop IP
❹ Check the incoming OSC message for the iii value pattern, which specifies a packet of three integer values.
❺ Once a complete OSC data package containing three integers is detected, we set x, y and p to the incoming values
❻ Create a new outgoing OSC message myMessage, with an assigned label accelerometerData, containing our local accelerometer info. OSC Labels can also be used on the receiving side to distinguish between multiple incoming messages
❼ Add the x, y and z accelerometer axes to the outgoing OSC message
❽ Send the OSC message myMessage to remoteLocation
❾ Instantiate an OSC object from the oscP5 library8, and start an OSC con-nection on port 12000
❿ Set the destination IP and port number to the remoteAddress at port number 12000, the port must be identical to successfully exchange data
⓫ Look up the Android IP address assigned by the Wi-Fi network using getIP() The oscP5 library relies on some methods from the core Network library in Processing called netP5,9 which is why we import both at beginning of the code.
To work with the accelerometer, we use the KetaiSensor class again, which is why we import the ketai.sensors package. Too look up the Android’s assigned WiFi IP address we use the getIP() method contained in the ketai.net10 package. Make sure to customize remoteAddress to match your desktop IP address.
Now we are ready on the Android side to start talking.
8. sojamo.de/oscP5
9. http://processing.org/reference/libraries/net/index.html 10. http://ketai.googlecode.com/svn/trunk/ketai/reference/index.html