I work with Twilio android SDK, when I press the hangup button, always get an error, can not disconnect the call after dialing?

I have successfully integrated the Twilio SDK for Android, but when I try to make a call with it, it does not connect and does not show an error message in logcat, I get the following errors:

1) Failed to hangup call due to error code: 70015, message: pjsua_call_hangup(): Object already exists (PJ_EEXISTS) 2) Connection disconnected with error code 31000 and message Generic error this both errors occurs when i am calling disconnect method if i comment this method then my calling is working fine and i make call but if i call this method phone.disconnect(), i am getting error shown as above. This is my methods which i am using to make calls and to disconnect the calls. 

////// Make calls

 public void connect(String phoneNumber) { Map parameters = new HashMap(); parameters.put("PhoneNumber", phoneNumber); connection = device.connect(parameters, null /* ConnectionListener */); if (connection == null) Log.w(TAG, "Failed to create new connection"); } 

// Turn off the phone

 public void disconnect() { if (connection != null) { connection.disconnect(); connection = null; // will null out in onDisconnected() if (basicConnectionListener != null) basicConnectionListener.onConnectionDisconnecting(); } } 

and in my OnClick event:

 public void onClick(View view) { if (view.getId() == R.id.dialButton) Toast.makeText(getApplicationContext(), "Dialing...", Toast.LENGTH_LONG).show(); phone.connect("PHONE NUMBER"); if (view.getId() == R.id.hangupButton) Toast.makeText(getApplicationContext(), "Call Disconnected...", Toast.LENGTH_LONG).show(); phone.disconnect(); Please suggest me and help me because i tried all possible thing for hangout but still not able to solve it.Thanks in advance. ![enter image description here][1] 
+4
source share
1 answer

I am sharing my working class on twilio SDK setup .

These are challenges that work great for me. I would like to share my class so that anyone can reference it.

This is the base class that handles all twillio Calling and Disconnecting call events.

  import java.util.HashMap; import java.util.Map; import android.content.Context; import android.media.AudioManager; import android.util.Log; import android.view.Gravity; import android.view.inputmethod.InputMethodManager; import android.widget.Toast; import com.twilio.client.Connection; import com.twilio.client.Connection.State; import com.twilio.client.ConnectionListener; import com.twilio.client.Device; import com.twilio.client.DeviceListener; import com.twilio.client.PresenceEvent; import com.twilio.client.Twilio; 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; 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"); String capabilityToken = HttpHelper.httpGet("http:------/auth.php"); 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); String capabilityToken; try { // capabilityToken = // HttpHelper.httpGet("http://------/mobile/auth.php"); capabilityToken = HttpHelper.httpGet("http://------/mobile/auth.php"); device = Twilio.createDevice(capabilityToken, null /* DeviceListener */); } catch (Exception e1) { e1.printStackTrace(); } try { device.disconnectAll(); } catch (Exception e) { e.printStackTrace(); } connection = device.connect(parameters, null /* ConnectionListener */); if (connection == null) { Log.w(TAG, "Failed to create new connection"); } } // 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; // will null out in onDisconnected() 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; } } 

You can use this class, for example:

  private MonkeyPhone phone; //On create phone = new MonkeyPhone(getApplicationContext()); //On Call Button Click @Override public void onClick(View view) { if (view.getId() == R.id.btnCallHere) { phone.connect(newContact); } else if (view.getId() == R.id.btnDisconnectHere) { phone.disconnect(); } 
+6
source

Source: https://habr.com/ru/post/1494503/


All Articles