Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: String +DatagramPacket+ Android + Arduino

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String +DatagramPacket+ Android + Arduino

    what I want to do is, if I use send.setOnClickListener(new OnClickListener) to send stringdata1 + stringdata1 over the DatagramSocket to an Arduino Board, but I donīt know how to convert the two Strings and send them
    please help me
    thanks in Advance
    ////////////////////////////////////////////////////////////
    Code 1
    ////////////////////////////////////////////////////////////
    send.setOnClickListener(new OnClickListener() {
     
    			public void onClick(View v) {
    					SendSlcValue.setintData();
    					SendSlcValue.updateArduinoValues();
    					;
    ////////////////////////////////////////////////////////////
    Code 2
    ////////////////////////////////////////////////////////////
    public class SendSlcValue {
    	static DatagramSocket socket;
    	  static byte data[];
     
    	  static {
    	    data = new byte[700];
    	    try {
    	      socket = new DatagramSocket();
    	    } catch (SocketException e1) {
    	      e1.printStackTrace();
    	    }
    	  }
     
    	  public static void updateArduinoValues() {
    	    Thread t = new Thread(new Runnable() {
     
    	      @Override
    	      public void run() {
    	        try {
    	          InetAddress serverIP = InetAddress
    	              .getByName("255.255.255.255");
     
    	          DatagramPacket out = new DatagramPacket(data, data.length,
    	              serverIP, 50025);
     
    	          socket.send(out);
    	        } catch (IOException e) {
    	          e.printStackTrace();
    	        }
     
    	      }
    	    });
    	    t.start();
    	  }
     
    	  public static void setintData(byte val_onoff) {
     
     
     
     
    		    String stringdata1 = "0020 0020";
    		    byte[] byteData1 = stringdata1.getBytes();
     
                        String stringdata2 = "0000 0071 0000 000c 0020 0020 0020 0020 0020 0020 0020 0020 0020 0020 0040 0040 0020 0020 0020 0020 0020 0020 0040 0020 0020 0040 0020 0ca0";
    		    byte[] byteData2 = stringdata2.getBytes();
    Last edited by mani3321; March 19th, 2014 at 07:41 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String +DatagramPacket+ Android + Arduino

    how to convert the two Strings
    Can you explain what the problem is? The byte arrays can go in a DatagramPacket for sending.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String +DatagramPacket+ Android + Arduino

    if I send the byteData1 instead of data I don`t see anything on the arduino Serial Monitor
    but if I use this code
    ////////////////////
    Code 1
    ////////////////////
    public class MainActivity extends Activity {
    	public Button send; 
    	public TextView tv;
    	public TextView etcode;
    	String test = new String("test");
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		send = (Button) findViewById(R.id.Senden);
     
     
     
    		////////////////////////////
    		send.setOnClickListener(new OnClickListener() {
     
    			public void onClick(View v) {
    					SendSlcValue.setintData((byte)1);
    					SendSlcValue.updateArduinoValues();
    					;	
    			}
    		});
    	}
    }
    ///////////////////////
    code 2
    ///////////////////////
    public class SendSlcValue {
    	static DatagramSocket socket;
    	  static byte data[];
     
    	  static {
    	    data = new byte[700];
    	    try {
    	      socket = new DatagramSocket();
    	    } catch (SocketException e1) {
    	      e1.printStackTrace();
    	    }
    	  }
     
    	  public static void updateArduinoValues() {
    	    Thread t = new Thread(new Runnable() {
     
    	      @Override
    	      public void run() {
    	        try {
    	          InetAddress serverIP = InetAddress
    	              .getByName("255.255.255.255");
     
    	          byte[] byteData = null;
    			DatagramPacket out = new DatagramPacket(byteData, data.length,
    	              serverIP, 50025);
     
    	          socket.send(out);
    	        } catch (IOException e) {
    	          e.printStackTrace();
    	        }
     
    	      }
    	    });
    	    t.start();
    	  }
     
    public static void setintData(byte intensity) {
    		data[5] = intensity;
     
     
    }

    //////////////////////

    ///////////////////
    I see the 6 on the arduino Serial Monitor


    My Problem is I don`t know how I have to connect the stringdata1 and stringdata2 with data to see anything on the Serial Monitor
    Last edited by mani3321; March 19th, 2014 at 07:46 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: String +DatagramPacket+ Android + Arduino

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Sorry, I don't understand the little bits of code that was posted. Too many things are not defined.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: July 6th, 2013, 08:17 PM
  2. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  3. Java netbeans to Arduino (Serial Data)
    By philli3 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: March 10th, 2012, 08:06 AM
  4. [SOLVED] Java program to create CRC on sending and receiving UDP datagram
    By Koren3 in forum Java Networking
    Replies: 3
    Last Post: April 21st, 2009, 10:28 AM