1

在过去的几天里,我试图让 JAIN SIP 在 Android 上运行。我采用了一些在 Java 应用程序上运行良好的代码并将其移植到 Android 应用程序。

我检查了:

  • 清单中的权限 (INTERNET / ALL)
  • JAIN SIP API 工作正常(stackoverflow 上的其他线程重点关注)。
  • 通信在自己的线程上运行,而不是 MainThread。
  • IP 有效。
  • 如果我不使用 Android 作为平台,而是使用 Ubuntu,则该代码有效。
  • 我尝试了 Android 4.0.3 及更高版本。

我的代码:

public class MainActivity extends Activity implements SipListener {

private static final String LOG = "JAIN";
private static final int PORT = 5060;
private SipFactory mSipFactory;
private AddressFactory mAddressFactory;
private MessageFactory mMessageFacory;
private HeaderFactory mHeaderFactory;
private SipStack mSipStack;
private SipProvider mSipProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable(){

        @Override
        public void run() {
            try {
                // Create factories
                mSipFactory = SipFactory.getInstance();
                mAddressFactory = mSipFactory.createAddressFactory();
                mMessageFacory = mSipFactory.createMessageFactory();
                mHeaderFactory = mSipFactory.createHeaderFactory();

                // Create the SipStack
                Properties properties = new Properties();
                properties.setProperty("javax.sip.STACK_NAME","Stack");
                mSipStack = mSipFactory.createSipStack(properties);

                // Create the SipProvider 
                String localIP = InetAddress.getLocalHost().getHostAddress();
                ListeningPoint listeningPoint = mSipStack
                        .createListeningPoint(localIP, PORT, "udp");
                mSipProvider = mSipStack.createSipProvider(listeningPoint);
                mSipProvider.addSipListener(MainActivity.this);

                // Create addresses and via header for the request
                Address fromToAddress = mAddressFactory
                        .createAddress("sip:192.168.0.198");
                Address contactAddress = mAddressFactory
                        .createAddress("sip:me@192.168.0.195");
                ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
                ViaHeader myViaHeader = mHeaderFactory
                        .createViaHeader("me.bla.com", PORT, "udp", "tlf64");
                viaHeaders.add(myViaHeader);

                // Build the request
                final Request request = mMessageFacory.createRequest(
                        mAddressFactory.createAddress("sip:192.168.0.195:9876").getURI(), 
                        "REGISTER",
                        mHeaderFactory.createCallIdHeader("12345678"),
                        mHeaderFactory.createCSeqHeader(1234l, "REGISTER"), 
                        mHeaderFactory.createFromHeader(fromToAddress, "sdf6"),
                        mHeaderFactory.createToHeader(fromToAddress, null), 
                        viaHeaders, 
                        mHeaderFactory.createMaxForwardsHeader(70));

                // Add the contact header
                request.addHeader(mHeaderFactory.createContactHeader(contactAddress));

                // Print the request
                System.out.println(request.toString());

                // Send the request --- triggers an IOException
                mSipProvider.sendRequest(request);
            } catch (Exception e) {
                Log.d(LOG, Log.getStackTraceString(e));
            }
        }}).start();
}

正如您在 StackTrace 中看到的,发生了 IOException:

javax.sip.SipException: IO Exception occured while Sending Request
at gov.nist.javax.sip.SipProviderImpl.sendRequest(SipProviderImpl.java:722)
at com.example.jainsiptest.MainActivity$1.run(MainActivity.java:97)
at java.lang.Thread.run(Thread.java:856)
 Caused by: java.net.SocketException: sendto failed: EINVAL (Invalid argument)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
at libcore.io.IoBridge.sendto(IoBridge.java:475)
at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:182)
at java.net.DatagramSocket.send(DatagramSocket.java:284)
at gov.nist.javax.sip.stack.UDPMessageChannel.sendMessage(UDPMessageChannel.java:724)
at gov.nist.javax.sip.stack.MessageChannel.sendMessage(MessageChannel.java:222)
at gov.nist.javax.sip.SipProviderImpl.sendRequest(SipProviderImpl.java:711)
... 2 more
 Caused by: libcore.io.ErrnoException: sendto failed: EINVAL (Invalid argument)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:151)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
at libcore.io.IoBridge.sendto(IoBridge.java:473)
... 7 more

java.net.SocketException: sendto failed: EINVAL (Invalid argument)

不会以相同的参数在 Ubuntu 上抛出。

有任何想法如何解决它?非常感谢您的帮助!

4

2 回答 2

1

似乎 Android 操作系统处理给定字符串的方式与 Ubuntu 操作系统不同。我不知道为什么。


毕竟,我通过使用 MjSip 而不是 JAIN SIP 解决了这个问题。

因为我需要 RTP、SDP 和 SIP 旁边的其他东西,所以我决定修改使用 MjSip 1.6 的开源 Android 应用程序Sipdroid,直到它符合我的需求。

  1. 为此,首先获取代码

  2. 将项目导入 Eclipse:File -> Import -> Existing Android Code Into Workspace。

  3. 考虑到 API 级别等,创建一个满足您需求的新 Android 项目。

  4. 添加<uses-permission android:name="android.permission.INTERNET" />到项目的清单中。

  5. 将 Sipdroid 项目中除以下内容之外的所有包复制到您的项目中:

    • org.sipdroid.codecs 和 org.sipdroid.media (我还不需要,也许以后需要)
    • org.sipdroid.sipua 和子包。
  6. 删除或评论损坏的引用。

  7. 请参阅下面的代码将 REGISTER 发送到服务器。

  8. android.util.Log通过使用而不是给定代码来修复日志机制引发的异常。最后,删除或注释触发 NullPointerException 的刷新命令。

就是这样。

import org.zoolu.sip.address.NameAddress;
import org.zoolu.sip.message.Message;
import org.zoolu.sip.message.MessageFactory;
import org.zoolu.sip.provider.SipProvider;
import org.zoolu.sip.provider.SipStack;
import org.zoolu.sip.transaction.TransactionClient;
import org.zoolu.sip.transaction.TransactionClientListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity implements TransactionClientListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread(new Runnable(){

        @Override
        public void run() {
            try{
                SipStack.init();

                SipProvider sipProvider = new SipProvider(
                        "192.168.0.198", 5060, new String[]{"udp"}, null);
                NameAddress toAddress = new NameAddress(
                        "sip:192.168.0.195:9876");
                NameAddress fromAddress = new NameAddress(
                        "sip:192.168.0.198:9876");

                Message message = MessageFactory.createRegisterRequest(
                        sipProvider, 
                        toAddress, 
                        fromAddress, 
                        fromAddress, 
                        null, 
                        null);

                TransactionClient t = new TransactionClient(
                        sipProvider, 
                        message,
                        MainActivity.this);

                t.request();

            }catch(Exception e){
                Log.d("MYSIP", Log.getStackTraceString(e));
            }
        }}).start();
}

// Interface methods

}
于 2013-01-08T15:52:16.273 回答
0

而不是获取localIPAddress为:

String localIP = InetAddress.getLocalHost().getHostAddress();

NetworkInterface使用或获取 IP 地址WifiManager

于 2013-06-02T01:41:58.327 回答