在Android应用

时间:2016-03-30 21:19:15

标签: java android sockets udp android-broadcastreceiver

我无法在我的应用上收到任何数据包。我有一台从console-php运行的计算机这个脚本:

$broadcast_string='http://hellokitty/shownumber.html';
$port=2328;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); 
while(true){
  socket_sendto($sock, $broadcast_string, strlen($broadcast_string), 0, '255.255.255.255', $port); 
  sleep(1);
  echo "Broadcasting ...\n";
}

另一方面,我在MainActivity.java文件的phonegap应用程序上运行了此代码:

import android.os.Bundle;

import android.os.PowerManager; 
import android.os.PowerManager.WakeLock;
import android.content.Context;

import org.apache.cordova.*;

import java.io.*;
import java.net.*;
import java.util.*;
import android.content.BroadcastReceiver;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Environment;
import android.content.Context;
import android.app.DialogFragment;
import android.app.AlertDialog;
import android.view.Menu;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.provider.Settings;
import android.view.WindowManager;

import android.util.Log;
import android.content.res.AssetManager;

public class MainActivity extends CordovaActivity
{
    public static String URL = null;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Thread thread = new Thread(new Runnable(){
          @Override
          public void run() {
          try {
            DatagramSocket socket;

            //Wakelock------------------------------------------------------------------------------------------------------------------
              WakeLock mWakeLock =  null,mPartialWakeLock = null;
              PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
              // lock used to keep the processor awake.
              mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
              mPartialWakeLock.acquire();


              final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
              WifiManager.MulticastLock lock = wifi.createMulticastLock("UDP");
              lock.acquire();
            //--------------------------------------------------------------------------------------------------------------------------
            try {
              //Keep a socket open to listen to all the UDP trafic that is destined for this port
              //socket = new DatagramSocket(2328, InetAddress.getByName("0.0.0.0"));
              socket = new DatagramSocket(2328);
              socket.setBroadcast(true);
              URL = null;
              while (URL == null) {
                  System.out.println(getClass().getName() + ">>>Ready to receive broadcast packets!");

                  //Receive a packet
                  byte[] recvBuf = new byte[12000];
                  DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
                  //socket.setSoTimeout(10000);
                  socket.receive(packet);

                  //Packet received
                  System.out.println(getClass().getName() + ">>>Discovery packet received from: " + packet.getAddress().getHostAddress());
                  System.out.println(getClass().getName() + ">>>Packet received; data: " + new String(packet.getData()));
                  String message = new String(packet.getData()).trim();

                  URL=message;
                  break;
              }
          } catch (IOException ex) {
                //Nothing
          }



          } catch (Exception e) {
              e.printStackTrace();
          }
            }
        });

          thread.start(); 

          while(URL == null){
            continue;
          }
          Log.d("net_discover",URL);
          super.loadUrl(URL);
      }
}

我已经测试过网络嗅探数据包并且流量没问题。 我已经添加了wakelock部分,因为我已经读过android阻止广播数据包,但我仍然没有在我的设备上得到任何东西。

我正在开发运行Android 4.4.4的摩托罗拉

在adb logcat上,应用程序停留在msg“准备接收...”上,这就是我得到的。

有什么建议吗?

更新:

我在我的应用程序中添加了代码,以便在开始接收之前发送数据包,并且我很惊讶它确实接收了它。所以我开始从我的计算机发送数据包并嗅探网络,但我没有看到任何数据。所以做了一些研究我发现应用程序正在使用lo接口(loopback,127.0.0.1)。

所以现在我的问题是,如何让应用程序从wifi接口接收数据包?

0 个答案:

没有答案