private void tryIP(final String ip, final String location, int timeout) throws IOException { final String description = Networker.doNetwork(location, "GET", "", timeout).getBody(); // Parsing with RegEx allowed here because the output format is fairly strict final String modelName = Util.quickMatch("<modelName>(.*?)</modelName>", description); // Check from description if we're dealing with a hue bridge or some other device if (modelName.toLowerCase().contains("philips hue bridge")) { try { final boolean access = HueService.userExists(ip, Util.getDeviceIdentifier(LinkActivity.this)); final String name = Util.quickMatch("<friendlyName>(.*?) \\([0-9.]+\\)</friendlyName>", description); final String mac = Util.quickMatch("<serialNumber>(.*?)</serialNumber>", description); bridgesList.post( new Runnable() { @Override public void run() { Bridge b = new Bridge(ip, mac, name, access); bridges.add(b); bridgesAdapter.add(b); } }); } catch (Exception e) { // Do nothing, this basically serves as an extra check to see if it's really a hue bridge Log.w("hue2", "Found device that identifies as Hue bridge, but isn't one? (" + ip + ")"); e.printStackTrace(); } } }
@SuppressWarnings("unchecked") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_link); // Set up layout bridgesList = (ListView) findViewById(R.id.link_bridges); bridgesAdapter = new BridgeAdapter(this); bridgesList.setAdapter(bridgesAdapter); bridgesList.setEmptyView(findViewById(R.id.link_empty)); // Set up loading UI elements ActionBar ab = getActionBar(); ab.setCustomView(R.layout.loader); ab.setDisplayShowCustomEnabled(true); ab.setDisplayShowHomeEnabled(false); RelativeLayout loadingLayout = (RelativeLayout) ab.getCustomView(); loadingSpinner = (ProgressBar) loadingLayout.findViewById(R.id.loader_spinner); refreshButton = (ImageButton) loadingLayout.findViewById(R.id.loader_refresh); setEventHandlers(); // Restore state or start over if (savedInstanceState == null) { Bridge lastBridge = Util.getLastBridge(this); if (lastBridge != null) { // This will be set again after a successful connection, connection failure leaves the last // bridge null Util.setLastBridge(this, null); // Try connecting to last bridge connectToLastBridge(lastBridge); } else { // Start searching for bridges and add them to the results startSearching(true); } } else { bridges = (ArrayList<Bridge>) savedInstanceState.getSerializable("bridges"); for (Bridge b : bridges) { bridgesAdapter.add(b); } // If a search was running, continue without removing existing results if (savedInstanceState.getBoolean("searching")) { startSearching(false); } } }
public void startLinkChecker(final Bridge b, final BridgeLinkDialog dialog) { final String username = Util.getDeviceIdentifier(this); linkChecker = new Timer(); linkChecker.scheduleAtFixedRate( new TimerTask() { @Override public void run() { try { boolean pressed = HueService.createUser( b.getIp(), Util.ensureMaxLength(Util.getDeviceName()), username); if (pressed) { // User created! dialog.dismiss(); linkChecker.cancel(); connectToBridge(b); } } catch (ApiException e) { // Ignore, it's because link button hasn't been pressed yet } catch (IOException e) { dialog.dismiss(); linkChecker.cancel(); bridgesList.post( new Runnable() { @Override public void run() { ErrorDialog.show( getFragmentManager(), R.string.dialog_bridge_lost_title, R.string.dialog_network_error); } }); } } }, 0, LINK_INTERVAL); }