Android VOIP application without access to internet

后端 未结 5 577
我寻月下人不归
我寻月下人不归 2020-12-14 04:57

I need to develop the VOIP application between 2 android devices.
As I know there is a SIP protocol used for this purpose but it requires registation to SIP server and a

5条回答
  •  独厮守ぢ
    2020-12-14 05:55

    Of course it is possible! Why you would need the internet? As long as you are both connected to the same network that is fine! Below is the java and xml for a working app.

    On start up it will provide you with your own local port, for example "52022".. this is random every time and unfortunately that can't be helped. We then enter the IP address of the other phone and THEIR randomly generated port number and press connect. They do exactly the same and hooray you're connected! This test app obviously requires you to be close by to exchange port numbers, but in my proper app I was easily able to request each port number before connecting. Hope this helps!

    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 en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                   NetworkInterface intf = en.nextElement();
                   for (Enumeration 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;
        }
    }
    

    Layout file:

    
    
        
    
        
    
        
    
            
        
    
        
    
            

    EDIT: The IP address method stopped working at API 22, use below code:

    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;
    }
    

提交回复
热议问题