/** * * @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; } }
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]; }
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; }
public R2RNet( RocrailService rocrailService, Preferences Prefs ) { this.Prefs = Prefs; WifiManager wifi = (WifiManager) rocrailService.getSystemService(Context.WIFI_SERVICE); MulticastLock lock = wifi.createMulticastLock("r2rlock"); lock.acquire(); }
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(); }
public static MulticastLock aquireMulticastLock(final Context pContext) { return WifiUtils.aquireMulticastLock(pContext, WifiUtils.MULTICASTLOCK_NAME_DEFAULT); }
public static void releaseMulticastLock(final MulticastLock pMulticastLock) { if (pMulticastLock.isHeld()) { pMulticastLock.release(); } }