Time to put it all together... using your Quickstart skills, you've already initiated an outgoing connection, and received an incoming connection. Now, let's call from one client to another.
For this example, we'll make a call between the Android Emulator and your device. To accomplish this, we'll need to:
<Dial> verbFirst we'll change the auth code to register as "tommy" for the app in the Android Emulator, and "jenny" for the app on your Android device.
In MonkeyPhone's onInitialized() method, add some code to figure out if
we're running in the emulator or not:
public class MonkeyPhone implements Twilio.InitListener, DeviceListener
{
/* ... other methods ... */
/* Twilio.InitListener method */
@Override
public void onInitialized()
{
Log.d(TAG, "Twilio SDK is ready");
try {
// the Emulator has a somewhat unique "product" name
boolean isEmulator = Build.PRODUCT.equals("sdk") || Build.PRODUCT.equals("google_sdk");
String clientName = isEmulator ? "tommy" : "jenny";
String capabilityToken = HttpHelper.httpGet("http://companyfoo.com/auth.php?clientName=" + clientName);
device = Twilio.createDevice(capabilityToken, this);
Intent intent = new Intent(context, HelloMonkeyActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
device.setIncomingIntent(pendingIntent);
} catch (Exception e) {
Log.e(TAG, "Failed to obtain capability token: " + e.getLocalizedMessage());
}
}
/* ... other methods ... */
}
Next we'll change the TwiML PHP file to connect your two clients together. So,
change the file hello-client-monkey-twiml.php to:
<?php
header('Content-type: text/xml');
$number = $_REQUEST["PhoneNumber"];
// put a phone number you've verified with Twilio to use as a caller ID number
$callerId = "XXXXXXXXXX";
?>
<Response>
<Dial callerId="<?php echo $callerId ?>">
<?php if (preg_match("/^[\d\(\)\-\+ ]+$/", $number)) { ?>
<Number><?php echo $number;?></Number>
<?php } else { ?>
<Client><?php echo $number;?></Client>
<?php } ?>
</Dial>
</Response>
What the above PHP program does is simply to check whether the PhoneNumber
is only digits, and if so, write that out within the
<Number> noun (which then
will call a regular phone), otherwise write out a
<Client> noun instead,
which will try and connect to a named Twilio Client app (a browser or another
mobile client).
So now build, install, and run your app on both your Android device and the Android Emulator. Then have one of the platforms call the other (e.g. by typing "tommy" into the Android device's app). Once the connection has been established you should be able to talk between your phone and your laptop!
You can also dial regular phone numbers from your Android app as long as you
replace the $callerId value in the PHP above with a phone number that's
verified with your Twilio Account. See the "Verified Numbers" section under
the Numbers
tab in your Twilio Account.
That's it for this Quickstart Guide!