Anshul Tyagi Anshul Tyagi - 7 months ago 48
PHP Question

How to integrate Twilio in Android

I'm trying out the

Twilio Android Quickstart
, and I've gotten to the end of the Making an outgoing call section in Java, but for some reason. I'm not getting success in Android.

I've created a php Api too for token, but that token isn't giving capability code to me. This api is on our local server i.e. localhost.

I'm giving source code of my project too

public class MonkeyPhone implements Twilio.InitListener, DeviceListener,
ConnectionListener {
private static final String TAG = "MonkeyPhone";
private Device device;
private Connection connection;
private final Context context;
private BasicConnectionListener basicConnectionListener;
private BasicDeviceListener basicDeviceListener;
private Connection pendingIncomingConnection;
InputMethodManager imm;
private boolean speakerEnabled;
private boolean muteEnabled;
static String capabilityToken;
public static final String ACCOUNT_SID = "XXXXXXXXXXXXXXXXXXXXXX";
public static final String AUTH_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXX";

public interface BasicConnectionListener {
public void onIncomingConnectionDisconnected();

public void onConnectionConnecting();

public void onConnectionConnected();

public void onConnectionFailedConnecting(Exception error);

public void onConnectionDisconnecting();

public void onConnectionDisconnected();

public void onConnectionFailed(Exception error);
}

public interface BasicDeviceListener {
public void onDeviceStartedListening();

public void onDeviceStoppedListening(Exception error);
}

public MonkeyPhone(Context context) {
this.context = context;
Twilio.initialize(context, this /* Twilio.InitListener */);
}

public void setListeners(BasicConnectionListener basicConnectionListener,
BasicDeviceListener basicDeviceListener) {
this.basicConnectionListener = basicConnectionListener;
this.basicDeviceListener = basicDeviceListener;
}

/* Twilio.InitListener method */
@Override
public void onInitialized() {
Log.d(TAG, "Twilio SDK is ready");
try {
// String capabilityToken =
// HttpHelper.httpGet("http://------/mobile/auth.php");
//capabilityToken = HttpHelper.httpGet("http:------/auth.php");
capabilityToken = HttpHelper
.httpGet("http://www.companyfoo.com/XXXXXXXXXXXXXXXXXXXXXXX");
System.out.println(capabilityToken);
device = Twilio
.createDevice(capabilityToken, null /* DeviceListener */);
} catch (Exception e) {
Log.e(TAG,
"Failed to obtain capability token: "
+ e.getLocalizedMessage());
}
}
/* Twilio.InitListener method */
@Override
public void onError(Exception e) {
Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
}
@Override
protected void finalize() {
if (device != null)
device.release();
if (connection != null)
connection.disconnect();
}
// To Make Calls
public void connect(String phoneNumber) {
Toast toast = Toast.makeText(context, "Dialing...", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("PhoneNumber", phoneNumber);
connection = device.connect(parameters, null /* ConnectionListener */);
if (connection == null)
{
Log.w(TAG, "Failed to create new connection");
// TODO Auto-generated method stub
}
try
{
// capabilityToken =
// HttpHelper.httpGet("http://------/mobile/auth.php");
// capabilityToken = HttpHelper
// .httpGet("https://api.twilio.com/2010-04- 01/Accounts/XXXXXXXXXXXXXXXXX/Calls.json");
//device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
} catch (Exception e1) {
e1.printStackTrace();
}
try {
device.disconnectAll();
} catch (Exception e) {
e.printStackTrace();
}
}
// To Disconnect Phone
public void disconnect() {
Toast toast = Toast.makeText(context, "Call Disconnected...",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
if (connection != null) {
connection.disconnect();
connection = null;

if (basicConnectionListener != null)
basicConnectionListener.onConnectionDisconnecting();
}
}
public void setSpeakerEnabled(boolean speakerEnabled) {
if (speakerEnabled != this.speakerEnabled) {
this.speakerEnabled = speakerEnabled;
updateAudioRoute();
}
}
private void updateAudioRoute() {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(speakerEnabled);
}
public void setMuteEnabled(boolean muteEnabled) {
if (muteEnabled != this.muteEnabled) {
this.muteEnabled = muteEnabled;
updateAudioRouteForMute();
}
}
private void updateAudioRouteForMute() {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMicrophoneMute(muteEnabled);
}
public State status() {
connection.getState();
State statusHere = connection.getState();
return statusHere;
}
@Override
public void onConnected(Connection arg0) {
updateAudioRoute();
updateAudioRouteForMute();
if (basicConnectionListener != null) {
basicConnectionListener.onConnectionConnected();
}
}
@Override
public void onConnecting(Connection arg0) {
if (basicConnectionListener != null) {
basicConnectionListener.onConnectionConnecting();
}
}
@Override
public void onDisconnected(Connection inConnection) {
if (inConnection == connection) {
connection = null;
if (basicConnectionListener != null)
basicConnectionListener.onConnectionDisconnected();
} else if (inConnection == pendingIncomingConnection) {
pendingIncomingConnection = null;
if (basicConnectionListener != null)
basicConnectionListener.onIncomingConnectionDisconnected();
}
}
@Override
public void onDisconnected(Connection inConnection, int arg1,
String inErrorMessage) {
if (inConnection == connection) {
connection = null;
if (basicConnectionListener != null)
basicConnectionListener
.onConnectionFailedConnecting(new Exception(
inErrorMessage));
}
}
@Override
public void onPresenceChanged(Device arg0, PresenceEvent arg1) {

}
@Override
public void onStartListening(Device arg0) {
if (basicDeviceListener != null) {
basicDeviceListener.onDeviceStartedListening();
}
}
@Override
public void onStopListening(Device arg0) {
if (basicDeviceListener != null) {
basicDeviceListener.onDeviceStoppedListening(null);
}
}
@Override
public void onStopListening(Device arg0, int arg1, String arg2) {

}
@Override
public boolean receivePresenceEvents(Device arg0) {
return false;
}
}


And php code is

<?php

require_once('Services/Twilio/Capability.php');
// Your API credentials from Account Dashboard here
$accountSid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$authToken = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
$appSid = 'APzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';

$capability = new Services_Twilio_Capability($accountSid, $authToken);
// give this app permissions
$capability->allowClientOutgoing($appSid);
// generate token that lasts for 5 minutes
$token = $capability->generateToken(300);

?>`


Any help would be greatly appreciated.

Answer

Twilio evangelist here.

It does not look like you are echoing the token out in your PHP file. If you load that PHP file in a browser, does the capability token get generated? If not, try adding echo $token after you generate the token.