Java 类android.net.wifi.WifiManager.MulticastLock 实例源码

项目:j2se_for_android    文件:HCWiFiManager.java   
/**
 * 
 * @param multicastIP for example, 224.X.X.X or 239.X.X.X
 * @param port
 * @param bs
 */
public static void multicast(final String multicastIP, final int port, final byte[] sendData){
    final WifiManager wifi = getWiFiManager();
    MulticastLock multicastLock = wifi.createMulticastLock(String.valueOf(System.currentTimeMillis()));
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();

    try{
        final MulticastSocket multicastSocket=new MulticastSocket(port);
        multicastSocket.setLoopbackMode(true);
        final InetAddress group = InetAddress.getByName(multicastIP);
        multicastSocket.joinGroup(group);

        final DatagramPacket packet=new DatagramPacket(sendData, sendData.length,group,port);

           multicastSocket.send(packet);
    }catch (final Throwable e) {
        e.printStackTrace();
    }

    if (multicastLock != null) {
        multicastLock.release();
        multicastLock = null;
    }
}
项目:mpd-control    文件:ServiceBonjour.java   
public static ServiceInfo discoverService(String service, int timeOut, Context context) throws IOException
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = manager.getConnectionInfo();

    int intaddr = wifiInfo.getIpAddress();
    byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff), (byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) };
    InetAddress addr=InetAddress.getByAddress(byteaddr);
    String hostname = InetAddress.getByName(addr.getHostName()).toString();

    MulticastLock lock = manager.createMulticastLock("mpdcontrol_bonjour");
       lock.setReferenceCounted(true);
       lock.acquire();
       JmDNS jmdns = JmDNS.create(addr, hostname);

       ServiceInfo[] infos = {};
    infos = jmdns.list(service, timeOut);

    jmdns.close();

    if (infos.length == 0) return null;
    return infos[0];
}
项目:AndroidCourses    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext, final String pMulticastLockName) {
    final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
    final MulticastLock multicastLock = wifiManager.createMulticastLock(pMulticastLockName);
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();
    return multicastLock;
}
项目:AndroidCourses    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext, final String pMulticastLockName) {
    final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
    final MulticastLock multicastLock = wifiManager.createMulticastLock(pMulticastLockName);
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();
    return multicastLock;
}
项目:tilt-game-android    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext, final String pMulticastLockName) {
    final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
    final MulticastLock multicastLock = wifiManager.createMulticastLock(pMulticastLockName);
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();
    return multicastLock;
}
项目:andRoc    文件:R2RNet.java   
public R2RNet( RocrailService rocrailService, Preferences Prefs ) {
  this.Prefs = Prefs;  

  WifiManager wifi = (WifiManager) rocrailService.getSystemService(Context.WIFI_SERVICE);
  MulticastLock lock = wifi.createMulticastLock("r2rlock");
  lock.acquire();
}
项目:NationSoccer    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext, final String pMulticastLockName) {
    final WifiManager wifiManager = WifiUtils.getWifiManager(pContext);
    final MulticastLock multicastLock = wifiManager.createMulticastLock(pMulticastLockName);
    multicastLock.setReferenceCounted(true);
    multicastLock.acquire();
    return multicastLock;
}
项目:meshchat    文件:MessageReceiver.java   
private void startThread() {
    thread = new Thread() {
        public void run() {
            WifiManager wim = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wim != null) {
                MulticastLock mcLock = wim.createMulticastLock(Globals.TAG);
                mcLock.acquire();
            }
            byte[] buffer = new byte[Globals.BUFFER_SIZE];
            DatagramPacket rPacket = new DatagramPacket(buffer, buffer.length);
            MulticastSocket rSocket;
            try {
                rSocket = new MulticastSocket(Globals.PORT);
            } catch (IOException e) {
                Log.d("MessageReceiver", "Impossible to create a new MulticastSocket on port " + Globals.PORT);
                e.printStackTrace();
                return;
            }

            while (!isInterrupted()) {
                Log.d("MessageReceiver", "Waiting for Messages...");
                try {
                    rSocket.receive(rPacket);
                } catch (IOException e1) {
                    Log.d("MessageReceiver", "There was a problem receiving the incoming message.");
                    continue;
                }
                if (isInterrupted())
                    break;
                Log.d("MessageReceiver", "Received Message! Processing...");
                byte data[] = rPacket.getData();
                try {
                    msgQueue.add(Message.deserialize(data));
                } catch (IllegalArgumentException ex) {
                    Log.d("MessageReceiver", "There was a problem processing the message " + Arrays.toString(data));
                    continue;
                }
                handler.post(processRunnable);
            }
            rSocket.close();
        }
    };
    thread.start();
}
项目:AndroidCourses    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext) {
    return WifiUtils.aquireMulticastLock(pContext, WifiUtils.MULTICASTLOCK_NAME_DEFAULT);
}
项目:AndroidCourses    文件:WifiUtils.java   
public static void releaseMulticastLock(final MulticastLock pMulticastLock) {
    if (pMulticastLock.isHeld()) {
        pMulticastLock.release();
    }
}
项目:AndroidCourses    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext) {
    return WifiUtils.aquireMulticastLock(pContext, WifiUtils.MULTICASTLOCK_NAME_DEFAULT);
}
项目:AndroidCourses    文件:WifiUtils.java   
public static void releaseMulticastLock(final MulticastLock pMulticastLock) {
    if (pMulticastLock.isHeld()) {
        pMulticastLock.release();
    }
}
项目:tilt-game-android    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext) {
    return WifiUtils.aquireMulticastLock(pContext, WifiUtils.MULTICASTLOCK_NAME_DEFAULT);
}
项目:tilt-game-android    文件:WifiUtils.java   
public static void releaseMulticastLock(final MulticastLock pMulticastLock) {
    if (pMulticastLock.isHeld()) {
        pMulticastLock.release();
    }
}
项目:NationSoccer    文件:WifiUtils.java   
public static MulticastLock aquireMulticastLock(final Context pContext) {
    return WifiUtils.aquireMulticastLock(pContext, WifiUtils.MULTICASTLOCK_NAME_DEFAULT);
}
项目:NationSoccer    文件:WifiUtils.java   
public static void releaseMulticastLock(final MulticastLock pMulticastLock) {
    if (pMulticastLock.isHeld()) {
        pMulticastLock.release();
    }
}