예제 #1
0
 public void startSearch() {
   Log.d(TAG, "Start search.");
   if (mStarted) {
     Log.d(TAG, "startSearch() ignore, search is already started.");
     return;
   }
   mStarted = true;
   NetWorkUtil.acquireWifiMultiCastLock(mContext);
   Thread searchThread = new Thread(this);
   searchThread.start();
 }
예제 #2
0
 /** Leave broadcast group. */
 private void leaveGroup(InetAddress groupAddr) {
   try {
     // leave broadcast group.
     mReceiveSocket.leaveGroup(groupAddr);
   } catch (Exception e) {
     Log.e(TAG, "leave group fail. " + e.toString());
   }
 }
예제 #3
0
 /** Join broadcast group. */
 private void join(InetAddress groupAddr) {
   try {
     // Join broadcast group.
     mReceiveSocket.joinGroup(groupAddr);
   } catch (Exception e) {
     Log.e(TAG, "Join group fail. " + e.toString());
   }
 }
예제 #4
0
  public void stopSearch() {
    Log.d(TAG, "Stop search");
    mStarted = false;
    mStopped = true;
    leaveGroup(mMulticastAddress);
    NetWorkUtil.releaseWifiMultiCastLock();

    closeSocket();
    mInstance = null;
  }
예제 #5
0
  @Override
  public void run() {
    try {
      mReceiveSocket = new MulticastSocket(Search.MULTICAST_RECEIVE_PORT);
      mReceiveSocket.setSoTimeout(Search.TIME_OUT);
      mMulticastAddress = InetAddress.getByName(Search.MULTICAST_IP);
    } catch (Exception e) {
      Log.e(TAG, "Create mReceiveSocket fail." + e);
    }

    join(mMulticastAddress);
    startListenServerMessage();
  }
예제 #6
0
    public void run() {
      DatagramPacket inPacket;

      while (!mStopped) {
        try {
          inPacket = new DatagramPacket(new byte[1024], 1024);
          mReceiveSocket.receive(inPacket);
          byte[] data = inPacket.getData();

          SearchProtocol.decodeSearchLan(data, mListener);
        } catch (Exception e) {
          if (e instanceof SocketTimeoutException) {
            // time out, search again.
            Log.d(TAG, "GetPacket time out. search again.");
          } else {
            Log.e(TAG, "GetPacket error," + e.toString());
            if (mListener != null) {
              mListener.onSearchStop();
            }
          }
        }
      }
    }