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(); }
/** 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()); } }
/** 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()); } }
public void stopSearch() { Log.d(TAG, "Stop search"); mStarted = false; mStopped = true; leaveGroup(mMulticastAddress); NetWorkUtil.releaseWifiMultiCastLock(); closeSocket(); mInstance = null; }
@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(); }
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(); } } } } }