private void startSearching(boolean clearResults) { stopSearching(); if (clearResults) { bridges.clear(); bridgesAdapter.clear(); } bridgeSearchTask = new BridgeSearchTask(); bridgeSearchTask.execute(); setSearchIndicator(true); }
private void stopSearching() { if (bridgeSearchTask != null) { bridgeSearchTask.cancel(true); bridgeSearchTask = null; bridgesList.post( new Runnable() { @Override public void run() { setSearchIndicator(false); } }); } }
private void searchBridgesUPnP(BridgeSearchTask task) throws IOException { // Search bridges on local network using UPnP String upnpRequest = "M-SEARCH * HTTP/1.1\nHOST: 239.255.255.250:1900\nMAN: ssdp:discover\nMX: 8\nST:SsdpSearch:all"; DatagramSocket upnpSock = new DatagramSocket(); upnpSock.setSoTimeout(100); upnpSock.send( new DatagramPacket( upnpRequest.getBytes(), upnpRequest.length(), new InetSocketAddress("239.255.255.250", 1900))); HashSet<String> ipsDiscovered = new HashSet<String>(); // Add any existing results from bruteforce search and previous searches for (Bridge b : bridges) { ipsDiscovered.add(b.getIp()); } long start = System.currentTimeMillis(); long nextBroadcast = start + BROADCAST_INTERVAL; while (true) { // Send a new discovery broadcast once a second if (System.currentTimeMillis() > nextBroadcast) { upnpSock.send( new DatagramPacket( upnpRequest.getBytes(), upnpRequest.length(), new InetSocketAddress("239.255.255.250", 1900))); nextBroadcast = System.currentTimeMillis() + BROADCAST_INTERVAL; } byte[] responseBuffer = new byte[1024]; DatagramPacket responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length); try { upnpSock.receive(responsePacket); } catch (SocketTimeoutException e) { if (System.currentTimeMillis() - start > SEARCH_TIMEOUT || task.isCancelled()) { break; } else { continue; } } final String ip = responsePacket.getAddress().getHostAddress(); final String response = new String(responsePacket.getData()); if (!ipsDiscovered.contains(ip)) { Matcher m = Pattern.compile("LOCATION: (.*)", Pattern.CASE_INSENSITIVE).matcher(response); if (m.find()) { tryIP(ip, m.group(1), 1000); } // Ignore subsequent packets ipsDiscovered.add(ip); } } }