在过去的几天里,我试图让 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 上抛出。
有任何想法如何解决它?非常感谢您的帮助!