I'm developing a SIP client app using Android 4.3.1+ API. I can make a outgoing call and receive a incoming call successfully by using SipDemo sample codes.
Let me show you my makecallWithSipStack method. On the phone side i can see on the screen Call Established with bla bla. At the pc side i havent any problem as well. And i cant see any problem in the logs. But i cant hear voice each side. By the way phone is caller.
Thanks for all advices.
public void makecallWithSipStack(){
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
#Override
public void onCallEstablished(SipAudioCall call) {
try {
call.startAudio();
call.setSpeakerMode(true);
updatestatus("Call Established with "+callusername+"#"+calluserdomain);
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
#Override
public void onCallEnded(SipAudioCall call) {
try {
call.endCall();
call.close();
updatestatus("Call Ended with "+callusername+"#"+calluserdomain);
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
};
try {
if (mSipManager != null && mSipProfile !=null){
call = mSipManager.makeAudioCall(mSipProfile.getUriString(), callusername+"#"+calluserdomain , listener, 30);
updatestatus("Calling "+callusername+"#"+calluserdomain);}
}catch (Exception e){
Log.e("Make Call","Error");
e.printStackTrace();
}
}
Related
I am connected to a bluetooth device using the a2dp profile. That part is all working fine, but when it comes time to disconnect all the expected code is accessed but the device does not disconnect and I'm not sure what I'm doing wrong. This is my disconnect code:
public void disconnectFromOther(BluetoothDevice device) {
BluetoothProfile.ServiceListener serviceListener =
new BluetoothProfile.ServiceListener() {
#Override
public void onServiceDisconnected(int profile) {
}
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Method disconnect;
try {
disconnect = a2dp.getClass()
.getMethod("disconnect", BluetoothDevice.class);
disconnect.setAccessible(true);
disconnect.invoke(proxy, device);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
BluetoothAdapter.getDefaultAdapter().closeProfileProxy(profile, proxy);
}
};
BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, serviceListener, BluetoothProfile.A2DP);
}
Edit: I have now also tried BluetoothGatt and I still can't disconnect my device.
Turns out that a second profile for Headset (BluetoothProfile.HEADSET) was also being opened and I spotted it in the logcat output. Closing that profile along with my a2dp profile closed the connection.
i have created mqtt connection suscribeData() as follows to receive data 24/7 from multiple devices
#RequestMapping("/suscribe")
#ResponseBody
public String suscribeData(){
connOpt= new MqttConnectOptions();
connOptPublish= new MqttConnectOptions();
boolean result=false;
int retry=0;
while(!result){
try{
result=initializeMqTTParameters(retry);
retry++;
if(!result){
Thread.sleep(1000);
}
}catch(Exception ex){
}
if(retry>10){
return "mqtterror";
}
}
suscribeReceivedData("DATA/#",2);
Calendar cal=TimeZoneJu.getCalenderInstance();
ModemOnlineStatus status=new ModemOnlineStatus();
status.setActiondate(cal.getTime());
status.setActiontime(cal.getTime());
status.setModemid("mqttreceive");
status.setRemark("online");
status.setStatus(true);
try{
service.checkAndInsertModemStatus(true, status);
}catch(Exception ex1){
}
return "ok";
}
checkAndInsertModemStatus() function is used to save the online offline state of connection i check the connection from another url with cronjobs.
and following is my initializeMqTTParameters
public boolean initializeMqTTParameters(int retry){
String clientID = M2MIO_THING;
connOpt = new MqttConnectOptions();
connOpt.setCleanSession(false);
connOpt.setUserName(M2MIO_USERNAME);
connOpt.setPassword(M2MIO_PASSWORD_MD5.toCharArray());
connOpt.setWill("WILL/mqttreceive", "mqttreceive:disconnected".getBytes(), 2, true);
try {
if(retry%2==0){
MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_1");
}else{
MQTTReceive.myClient = new MqttClient(BROKER_URL, "mqtt_virtual_received_sus_2");
}
MQTTReceive.myClient.setCallback(this);
MQTTReceive.myClient.connect(connOpt);
} catch (MqttException e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
on connectionLost i am trying update status in database and try reconnect
#Override
public void connectionLost(Throwable ex) {
System.out.println("Mqtt Received Connection Lost"+ex.getMessage());
Calendar cal=TimeZoneJu.getCalenderInstance();
ModemOnlineStatus status=new ModemOnlineStatus();
status.setActiondate(cal.getTime());
status.setActiontime(cal.getTime());
status.setModemid("mqttreceive");
status.setRemark("offline");
status.setStatus(false);
try{
service.updateModemStatus(false, status);
}catch(Exception ex1){
}
suscribeData();
}
and from the above code i got my connections were online but i stopped receving data on subscribed topics so another method i tried that i am checking connection status from another function from database like i set the connection online when i connect and offline on connectionLost and i can call to connect(suscribeData()) but it leaves me with too many open files in tomcat within few days how can i make sure previous open files should close
like System.exit(1) on connectionLost() so that current process will close and i will reconnect after checking the status
or please suggest me if there is any other logic i can use to maintain my connections
Your catch block does nothing in method connectionLost.. it should do something like this:
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
You should also disconnect your MQTT client whenever not required..i.e processing over:
myClient.disconnect();
I have two different types of messages and I am handling these message by two different listener.
I am adding listener in one class
public DrawingXmppServices(SurfaceViewCanvas surfaceView) {
chat = chatManager.createChat(BUDDY, new MyChatListener());
chat.addMessageListener(new MyChatListener());
receiver = surfaceView;
}
public void sendDrawingData(String drawingData, String buddyId) {
try {
chat.sendMessage(drawingData);
} catch (XMPPException e) {
e.printStackTrace();
}
}
And On other class
public InstantMessageXmpp(CanvasActivity canvasActivity) {
receiver = (MessageReceiver) canvasActivity;
chat = chatManager.createChat(BUDDY, this);
chat.addMessageListener(this);
}
public void sendIM(String message, String buddyID) {
try {
chat.sendMessage(message);
} catch (XMPPException e) {
e.printStackTrace();
}
}
When I call InstantMessageXmpp to send its packet but the listener of DrawingXmppServices is called. What mistake is I am doing?
Please guide me. Any suggestion can help me. Thank you
I'm writing an android app using a server. When the user exit the activity (remove it from recent activities) I want to close the connection and do some modifications in the server side before destroying the activity. So I read about the activity life cycle and I found out that I need to write the last call for closing the connections in onDestroy(). So that what I did:
Main activity:
#Override
public void onDestroy()
{
super.onDestroy();
try {
ConnectionHandler.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Connection Handler:
public static void close() throws IOException {
try {
JSONObject json = new JSONObject();
json.put("client", "close");
mConnectionHandler.new AsyncSendToServer().execute(json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
socket.close();
}
The message transfer is working, but the activity does not execute the ConnectionHandler.close() method. What should I do to execute this method when the user close the activity?
You have to write the super.onDestroy after your code.
Try:
#Override
public void onDestroy()
{
try {
ConnectionHandler.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy();
}
I am trying to implement simple logic to start/stop recording with MediaRecorder of Android.
The cycle is
connect to localSocket / set options / mRecorder.prepare();
mRecorder.start();
mRecorder.stop(); mRecorder.reset();
Then, loop between 2 and 3.
In the first cycle, 1,2,3 works fine as intended, however, I've got an error on the second start(restart) after the first stop.
com.example.app E/MediaRecorder﹕ start called in an invalid state: 1
What is the MediaRecorder state 1? What do I miss?
Thanks for your input.
if (cmd.equals("connect"))
{
try
{
sender.connect(new LocalSocketAddress(SOCKET_ADDRESS));
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setOutputFile(sender.getFileDescriptor());
mRecorder.prepare();
}
catch (IOException e)
{ e.printStackTrace(); }
}
if (cmd.equals("start"))
{
try
{
mRecorder.start();
}
catch (IllegalStateException e)
{ e.printStackTrace(); }
}
if (cmd.equals("stop"))
{
try
{
mRecorder.stop();
mRecorder.reset();
}
catch (Exception e)
{ e.printStackTrace(); }
}
I'd had the same problem. I had to make a function initRecorder that sets up and prepares the media recorder. Then I called this function each time after the start button was pressed but before start was called. recreate() after stop also works.
StartRecording.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 100);
try {
try {
initRecorder(mHolder.getSurface());
} catch (IOException e) {
e.printStackTrace();
}
mMediaRecorder.start();
Log.e("mRecorder", "Started");
} catch (RuntimeException e) {
Log.e("mRecorder", "Start Failure");
e.printStackTrace();
}
}
});
private void initRecorder(Surface surface) throws IOException {
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 1000);
if (mMediaRecorder == null) mMediaRecorder = new MediaRecorder();
// mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// mMediaRecorder.setOutputFormat(8);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
//mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
// mMediaRecorder.setVideoSize(640,480);
mMediaRecorder.setPreviewDisplay(surface);
mMediaRecorder.setOutputFile(path);
// mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
mMediaRecorder.setMaxDuration(10000); // 10 seconds
try {
mMediaRecorder.prepare();
Log.e("mRecorder", "Prepared");
} catch (IOException e) {
Log.e("mRecorder", "Prepare Failure");
e.printStackTrace();
}
mInitSuccesful = true;
}
In the scond cycle you have not called prepare, you need to call that before you can call start on media recorder
This is a self-answer, but I would not check as the answer because it's just work-around.
According to #Pulkit Sethi, state-1 means either MediaRecorder does not start properly or stop properly.
Perhaps, it's due to the local socket object sender.getFileDescriptor() as the target of setOutputFile.
So far, it's way too complicated and I could not find a way to stop gracefully enough to re-start or re-use MediaRecorder, I chose to dispose all everytime.
So
The cycle is
start localSocket/Server
connect to localSocket / set options / mRecorder.prepare();
mRecorder.start();
stop/close/release whole
This looks not the smartest way, but at least simple and stable, and I am happy with the result to start/stop/ & re-start as intended.
if (cmd.equals("stop"))
try
{
if (sender != null)
{
sender.close();
}
if (receiver != null)
{
receiver.close();
}
if (server != null)
{
server.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
sender = null;
receiver = null;
server = null;
}
for MediaRecorder
mRecorder.release();
The output file needs to be an actual file, not a socket. This is because MediaRecorder usually needs to be able to seek back in the file to update the header when the recording ends, and you can't seek in a socket.