Input an IP address in realtime

edited December 2013 in Android Mode

Hi folks,

Just wondering if there's any way to input an IP address from within a sketch, while it is running on Android, that would set and change the current address that it's sending on

If it is possible, I'd be very grateful for a code example

Tagged:

Answers

  • anyone able to help with this?

  • edited December 2013

    hey there, i think i know what you are trying to achieve, but honestly your question is kind of difficult to understand without any more specifics on whats sending to whom etc.

    so here is what i think your question is:

    you want to be able to input an ip address into a running android sketch, and then use it to establish some kind of connection.

    to achive this you could write some simple touch keypad to enter the ip. then verify the input and use it to establish your connection. of course you would have to catch all the possible exceptions that could be thrown, to avoid a crash of the sketch. i might be able to post some example code later.

  • here the quick code example how to input an ip/string during the runtime of the sketch. not android specific, but the general concept should be clear. you can then use the string to establish your new connection.

    /**
     * Small Example how to implement a simple TouchKeypad
     *   x: deletes last char
     *   r: clears input
     *   e: enters input
     **/
    
    TouchKeypad kp = new TouchKeypad();
    
    void setup(){
      size(200,200);
      kp.xpos = (200-30*3)/2;
      kp.ypos = 30;
    }
    
    void draw(){
      background(0);
      kp.mouseMove(mouseX,mouseY);
      kp.draw();
      textAlign(CENTER, CENTER);
      text(kp.ip,200/2,10);
    }
    
    void mouseClicked(){
      kp.press();
    }
    
    
    class TouchKeypad{
      int xpos, ypos = 0;
      int activeKey = -1;
      int keySize = 30;
      char[] keys = {'7','8','9',
                     '4','5','6',
                     '1','2','3',
                     '.','0','x',
                     'r','e',' ',};
      String ip = "";
    
      TouchKeypad(){
    
      }
      public void draw(){
        pushMatrix();
        translate(xpos,ypos);
        for(int x = 0; x < 3; x++){
          for(int y = 0; y < 5; y++ ){
            int key = y*3+x;
            fill(100);
            if(activeKey == key){
              fill(255,0,0);
            }
            rect(x*keySize,y*keySize,keySize,keySize);
            fill(255);
            textAlign(CENTER, CENTER);
            text(keys[key],x*keySize+keySize/2,y*keySize+keySize/2);
          }
        }
        popMatrix();
      }
    
      public void mouseMove(int mx, int my){
        mx = mx -xpos;
        my = my -ypos;
        activeKey = -1;
        for(int x = 0; x < 3; x++){
          for(int y = 0; y < 5; y++ ){
            if(mx > x*keySize && mx < (x+1)*keySize && my > y*keySize && my < (y+1)*keySize){
              int key = y*3+x;
              activeKey = key;
            } 
         }
        }
      }
    
      public void press(){
        if(activeKey != -1){
          switch(keys[activeKey]){
            case 'e':
              println(" enterd ip: "+ ip);
              break;
            case 'x':
              if(ip.length() > 0)
                ip = ip.substring(0,ip.length()-1);
              break;
            case 'r':
              if(ip.length() > 0)
                ip = "";
              break;
            case ' ':
              break;
            default:
              ip = ip + keys[activeKey];
          }
        }
      }
    }
    
Sign In or Register to comment.