ここ数日、私は 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ではスローされません。
それを修正する方法はありますか?助けてくれてありがとう!