Send data by bluetooth

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Send data by bluetooth

Postby Thanakin » Thu Aug 14, 2014 8:42 am

Hey Guys,

I would like to send data by Bluetooth from PC to EV3. I try to send values from 0 to 99. From a moment, EV3 receives the same value twice then it receives 0 and 1 between sent values. This phenomenon seems random because it is never the same value which receives twice.

Here is the code which runs on PC

Code: Select all
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.DiscoveryListener;

import lejos.pc.comm.NXTComm;
import lejos.pc.comm.NXTCommException;
import lejos.pc.comm.NXTConnector;

public class BlueToothPC {
   
   private NXTConnector n;

   private OutputStream out = null;
   
   private InputStream in = null;
   
   public void init(String name) throws NXTCommException{
      n = new NXTConnector();
      n.connectTo(name);
      in = n.getInputStream();
      out = n.getOutputStream();         
   }
   
   public void close() throws IOException{
      in.close();
      out.close();
      n.close();
   }
   
   public void write(int envoi) throws IOException{
      out.write(envoi);
      out.flush();
      
   }
   
   public int read() throws IOException{
      return in.read();
   }

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {
      
      BlueToothPC bt = new BlueToothPC();
      try{
         bt.init("EV3");
      
         for(int i = 0 ; i < 100 ; i++){
            bt.write(i);
            int r = bt.read();
            System.out.println(r);
         }
      
      bt.close();
      }
      catch(Exception e){
         e.printStackTrace();
      }
   }

}


Here is the code which runs on EV3 :
Code: Select all
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;


import lejos.hardware.Bluetooth;
import lejos.hardware.Button;
import lejos.hardware.ev3.LocalEV3;
import lejos.remote.nxt.NXTConnection;



public class BluetoothEV3 {
   
   
private lejos.remote.nxt.NXTConnection connection = null;
   
   private DataInputStream dataIn;
   
   private OutputStream dataOut;
   
   public void init(){
      connection = Bluetooth.getNXTCommConnector().waitForConnection(100000,NXTConnection.PACKET);
      dataOut = connection.openOutputStream();
      dataIn = connection.openDataInputStream();
   }
   
   public void write(int envoi) throws IOException{
      dataOut.write(envoi);
      dataOut.flush();
   }
   
   public int read() throws IOException{
      return dataIn.read();
   }
   
   public void close() throws IOException{
      dataOut.close();
      dataIn.close();
      connection.close();
   }
   
   /**
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException {
      BluetoothEV3 b = new BluetoothEV3();
      b.init();
      int v ;
      for (int i = 0 ; i < 100 ; i++){
         v= b.read();
         System.out.println(v);
         b.write(v);
      }

      b.close();
      while(!Button.ESCAPE.isDown()){
         
      }
   }

}



Do you have a idea to right this phenomenon ?
Thanakin
New User
 
Posts: 4
Joined: Thu Aug 14, 2014 6:57 am

Re: Send data by bluetooth

Postby gloomyandy » Thu Aug 14, 2014 10:32 am

You are using the methods intended to talk to an NXT, I don't think they are really complete at the moment. To talk to the EV3 from a PC just create a PAN connection and use standard TCP/IP, methods.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Send data by bluetooth

Postby Thanakin » Thu Aug 14, 2014 11:51 am

Does this method work to connect several EV3 to PC and send data ?
Thanakin
New User
 
Posts: 4
Joined: Thu Aug 14, 2014 6:57 am

Re: Send data by bluetooth

Postby gloomyandy » Thu Aug 14, 2014 12:22 pm

It does but you will need to change the IP address used by each of the additional EV3s (search the forum for how to do this). Alternately take a look at the code and try and work out what is going wrong, it should not be hard to fix. Also what libraries are you linking with on the PC side? Are you using the NXT PC side libraries? If so I'm not sure if this has ever been tested.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Send data by bluetooth

Postby Thanakin » Thu Aug 14, 2014 1:11 pm

Ok, thanks. I go to try it.
Thanakin
New User
 
Posts: 4
Joined: Thu Aug 14, 2014 6:57 am

Re: Send data by bluetooth

Postby Thanakin » Mon Aug 18, 2014 11:56 am

I has tried to connect one EV3 to PC with TCP/IP methods and it works ! :D but I cannot connect several EV3 to PC in the same time. When I want to connect a second EV3 to PC, PC disconnect the first EV3 :( The two EV3 are different IP.

Do you have any ideas to connect several EV3 to PC with Bluetooth only ?
Thanakin
New User
 
Posts: 4
Joined: Thu Aug 14, 2014 6:57 am

Re: Send data by bluetooth

Postby gloomyandy » Mon Aug 18, 2014 2:19 pm

Hi,
looks like this is a limitation of the Windows Bluetooth stack. Looks like you are stuck with this at the moment. If you are familiar with the Linux command line and tools then you could do this by allowing your other EV3s to connect to the EV3 connected to your PC as clients of the PAN (see: http://bluez.sourceforge.net/contrib/HOWTO-PAN), if none of this makes any sense then you may have to wait until we add this capability to leJOS. What exactly is it you are trying to do? Have you considered using WiFi rather then Bluetooth? If you need this capability and the above suggestions do not work for you then I think you will need to take a look at the code used to talk to an NXT and try and work out what was going wrong and fix it, that is probably the easy short term fix.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to EV3 Software

Who is online

Users browsing this forum: fhbc2003 and 1 guest

more stuff