Android VOIP应用程序无法访问互联网

时间:2012-06-24 11:17:04

标签: android sip voip

我需要在两个Android设备之间开发VOIP应用程序 据我所知,有一个用于此目的的SIP协议,但它需要注册到SIP服务器并访问互联网以进行SIP信令。
有没有办法在没有互联网访问的情况下在Android中创建VOIP应用程序?

5 个答案:

答案 0 :(得分:4)

当然有可能!为什么你需要互联网?只要你们都连接到同一个网络就好了!下面是工作应用程序的java和xml。

启动时,它将为您提供自己的本地端口,例如“52022”..每次都是随机的,不幸的是无法帮助。然后我们输入另一部电话的IP地址和随机生成的端口号,然后按连接。他们完全一样,并且 BAM 你已经连接了:)这个测试应用程序显然要求你靠近交换端口号,但在我的应用程序中,我很容易就能请求每个端口号连接。希望这有帮助!

public class MainActivity extends Activity {

AudioGroup m_AudioGroup;
AudioStream m_AudioStream;

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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
      try {   
          AudioManager audio =  (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
          audio.setMode(AudioManager.MODE_IN_COMMUNICATION);
          m_AudioGroup = new AudioGroup();
          m_AudioGroup.setMode(AudioGroup.MODE_NORMAL);
          m_AudioStream = new AudioStream(InetAddress.getByAddress(getLocalIPAddress ()));
          int localPort = m_AudioStream.getLocalPort();
          m_AudioStream.setCodec(AudioCodec.PCMU);
          m_AudioStream.setMode(RtpStream.MODE_NORMAL);

          ((TextView)findViewById(R.id.lblLocalPort)).setText(String.valueOf(localPort));

          ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String remoteAddress = ((EditText)findViewById(R.id.editText2)).getText().toString();
                String remotePort = ((EditText)findViewById(R.id.editText1)).getText().toString();

                  try {
                    m_AudioStream.associate(InetAddress.getByName(remoteAddress), Integer.parseInt(remotePort));
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  m_AudioStream.join(m_AudioGroup);
            }
        });

          ((Button) findViewById(R.id.button2)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                      m_AudioStream.release();
                }
            });

      } catch (Exception e) {
       Log.e("----------------------", e.toString());
       e.printStackTrace();
      }
}

public static byte[] getLocalIPAddress () {
    byte ip[]=null;
       try {
           for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
               NetworkInterface intf = en.nextElement();
               for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                   InetAddress inetAddress = enumIpAddr.nextElement();
                   if (!inetAddress.isLoopbackAddress()) {
                    ip= inetAddress.getAddress();
                   }
               }
           }
       } catch (SocketException ex) {
           Log.i("SocketException ", ex.toString());
       }
       return ip;

}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/lblLocalPort"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/localPort" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:hint="@string/iPHint"
    android:inputType="phone" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:ems="10"
    android:hint="@string/portHint"
    android:inputType="number" >

    <requestFocus />
</EditText>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/connect" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Disconnect" />
</LinearLayout>

编辑:IP地址方法在API 22停止工作,使用以下代码:

private byte[] getLocalIPAddress() {   
    byte[] bytes = null;

    try {
        // get the string ip
        WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
        String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

        // convert to bytes
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getByName(ip);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        bytes = new byte[0];
        if (inetAddress != null) {
            bytes = inetAddress.getAddress();
        }

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this, R.string.phone_voip_incompatible, Toast.LENGTH_SHORT).show();
    }

    return bytes;
}

答案 1 :(得分:3)

实际上SIP客户端可以通过对等方式进行通信,他们只需要知道他们收听SIP消息的IP地址和UDP端口。

您可以在两台计算机上使用普通的SIP客户端(X-Lite for Windows,Twinkle for Linux,以及其他一些也存在)并尝试在它们之间建立呼叫而无需注册服务器。这很可能。

此外,您还可以在本地LAN中的某个位置运行简约SIP服务器。例如,FreeSWITCH可以最小化到非常小的空间。

答案 2 :(得分:2)

这是不可能的,因为VOIP呼叫通过互联网和通过SIP服务器。

例如。如果您想通过VOIP dailer从您所在的国家/地区拨打电话,您必须要访问互联网,因为无法通过蓝牙进行通信。

感谢。

答案 3 :(得分:2)

好的,如果你正在寻找一些对等通信,我认为无线路是可行的方式(更好的距离和速度)。如果您只针对较新版本的Android进行开发,那么WI-FI Direct即可,但这只适用于Android 4.0及以上版本

为了在4.0以下运行某些东西,你将不得不使用第三方库。我知道Qualcomm有一个名为alljoyn的库,但不确定它有多好。

答案 4 :(得分:2)

我认为您可以在包括Andriod在内的多个平台上使用JITSI进行p2p voip服务。

以下是关于这个项目的调查结果: -

  1. 不需要任何服务器或互联网连接。
  2. 用户必须位于同一网络中。
  3. 开源。
  4. Android apk可用,很可能您可以在网站上找到它的代码,或者您可以对其进行反编译。