我需要开发两个安卓设备之间的VOIP应用程序。
据我所知,有一个SIP协议可用于此目的,但它需要在SIP服务器上注册并访问互联网进行SIP信令。
是否有方法可以在安卓设备上创建没有互联网访问权限的VOIP应用程序?
我需要开发两个安卓设备之间的VOIP应用程序。
据我所知,有一个SIP协议可用于此目的,但它需要在SIP服务器上注册并访问互联网进行SIP信令。
是否有方法可以在安卓设备上创建没有互联网访问权限的VOIP应用程序?
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>
</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;
}
实际上,SIP客户端可以进行点对点通信,只需要知道彼此的IP地址和UDP端口,以便监听SIP消息。
您可以在两台计算机上使用普通的SIP客户端(如Windows上的X-Lite、Linux上的Twinkle等)进行测试,尝试在不进行服务器注册的情况下建立通话。这是完全可能的。
此外,您可以在本地局域网中的某个位置运行一个极简的SIP服务器。例如,FreeSWITCH可以被最小化到非常小的占用空间。
如果你正在寻找一些点对点通信,我认为wifi是最好的选择(更好的距离和速度)。如果你只想开发新版本的Android应用程序,那么Wi-Fi Direct是最好的选择,但这只适用于Android 4.0及以上版本。
如果你想在低于4.0的设备上运行应用程序,你需要使用第三方库。我知道高通有一个叫做AllJoyn的库,但不确定它的质量如何。
这是不可能的,因为VOIP电话通过互联网和SIP服务器进行传递。
例如,如果您想通过VOIP拨号程序从您所在的国家拨打电话,那么您必须需要互联网接入,因为无法通过蓝牙通信。
谢谢。