通过Wifi将Android应用程序传送到PC应用程序

时间:2013-04-04 11:57:12

标签: java android android-wifi

我正在制作一个Android应用程序,它将通过Wifi发送UDP广播。并且有一个PC程序可以接收它。这是代码:

这是AndroidManifest.xml

中的用户权限
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

DeviceManagerWindow.java

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;

public class DeviceManagerWindow extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_manager_window);
        WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
        if(wifi != null)
        {
            WifiManager.MulticastLock lock = wifi.createMulticastLock("WifiDevices");
            lock.acquire();
        }
        Thread sendMulticast = new Thread(new MultiCastThread());
        sendMulticast.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.device_manager_window, menu);
        return true;
    }

}

此文件发送多播数据包:

MultiCastThread.java

public class MultiCastThread implements Runnable
{
    MulticastSocket s;
    DatagramPacket pack;
    public MultiCastThread()
    {
        try
        {
            s = new MulticastSocket(WifiConstants.PORT_NO);
            s.joinGroup(InetAddress.getByName(WifiConstants.GROUP_ADDR));
        }
        catch(Exception e)
        {
            Log.v("Socket Error: ",e.getMessage());
        }
    }
    @Override
    public void run()
    {
        try
        {
            pack = new DatagramPacket(WifiConstants.WHO_IS.getBytes(),WifiConstants.WHO_IS.getBytes().length, InetAddress.getByName(WifiConstants.GROUP_ADDR), WifiConstants.PORT_NO);
            s.setTimeToLive(WifiConstants.TIME_TO_LIVE);
            s.send(pack);
        }
        catch(Exception e)
        {
            Log.v("Packet Sending Error: ",e.getMessage());
        }
    }
}

WifiConstants.java

此文件记录Wifi Interaction的常量。

public class WifiConstants
{
    public static final int PORT_NO = 5432;
    public static final String GROUP_ADDR = "225.4.5.6";
    public static final int DGRAM_LEN = 1024;
    public static final String WHO_IS = "Who is?";
    public static final int TIME_TO_LIVE = 100;
}

在我的系统上,我正在运行一个java代码来接收数据包并将其打印在控制台中。

ListenerDevice.java

package Receiver;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;


public class ListenerDevice
{
    public static final int PORT_NO = 5432;
    public static final String GROUP_ADDR = "225.4.5.6";
    public static final int DGRAM_LEN = 1024;
    public static final String WHO_IS = "Who is?";
    public static final int TIME_TO_LIVE = 1;
    public static void main(String[] args)
    {
        MulticastSocket socket = null;
        DatagramPacket inPacket = null;
        byte[] inBuf = new byte[DGRAM_LEN];
        try
        {
          //Prepare to join multicast group
          socket = new MulticastSocket(PORT_NO);
          InetAddress address = InetAddress.getByName(GROUP_ADDR);
          socket.joinGroup(address);

              while(true)
              {
                    System.out.println("Listening");
                    inPacket = new DatagramPacket(inBuf, inBuf.length);
                    socket.receive(inPacket);
                    String msg = new String(inBuf, 0, inPacket.getLength());
                    System.out.println("From :" + inPacket.getAddress() + " Msg : " + msg);
              }
        }
        catch(Exception ioe)
        {
            System.out.println(ioe);
        }
      }
}

这是发送数据,但不是在PC端接收。两个设备都连接到同一网络。

我还创建了一个用于在移动设备上接收数据包的类。它正在接收正在发送的数据包。

我应该怎么做才能连接它。

1 个答案:

答案 0 :(得分:0)

两台设备都应连接到同一个网络。当您将数据包从仿真器发送到PC时,它驻留在同一系统上。因此,您没有遇到任何问题。但是,当您将数据包从PC发送到移动设备时,这两个设备应共享同一网络。

如果两个设备都连接到unfirewalled网络,那么它将更加高效。因为防火墙可能会在发送数据包时造成麻烦。

这是我得出的结论。