Problem with BT communication

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Problem with BT communication

Postby madtroll » Mon Mar 07, 2011 1:33 pm

I didn't have any problems with communication PC->Brick, but i want to have communication in both ways
currently my code looks like that:

PC
Code: Select all
DataInputStream dis;
DataOutputStream dos;
NXTComm nxtComm;
NXTInfo nxtInfo;
static int state = 0;

   String address = "00:16:53:0A:5C:22";
   
   /**
    * Sets up connection
    */
   public RobotController() {
      
      try {
         // If there is a problem with this, we can change 'NXTCommFactory.BLUETOOTH' to '2'.
         nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      } catch (NXTCommException e) {
         System.out.println("constructor");
         System.out.println(e);
      }
      
      // If there is a problem with this, we can change 'NXTCommFactory.BLUETOOTH' to '2'.
      nxtInfo = new NXTInfo(NXTCommFactory.BLUETOOTH, name, address);
      
      try {
         nxtComm.open(nxtInfo);
      } catch (NXTCommException e) {
         System.out.println("constructor");
         System.out.println(e);
      }
      
      InputStream is = nxtComm.getInputStream();
      OutputStream os = nxtComm.getOutputStream();
      
      dis = new DataInputStream(is);
      dos = new DataOutputStream(os);
         
   }
   
//*** BT Listener - THREADED   
   public void run() {
      
      System.out.println("Scanner On ");      
      while (true) {
      //read
      try {
         
         state = dis.readInt();
      } catch (IOException error) {
         continue;
      }         
      System.out.println("robot state : " + state);
      }
   }

   
   /**
    * Sends command to Robot (i.e. Robot.nxj)
    *
    * @param number, the opcode to be sent to the NXT brick
    */
   public void sendCommand(int number) {
         
      try {
         dos.writeInt(number);
         dos.flush();
      } catch (IOException error) {
         System.out.println("Error in sending command to robot: " + error);
      }   
   }


and Brick
Code: Select all

class BTSender
{
private static DataOutputStream dataOutStr;

static public void create()
{
dataOutStr = DataExchange.conn.openDataOutputStream();
}


static public void send(int state)
{
try{
dataOutStr.writeInt(state);
} catch (Exception e)
{
LCD.drawString("error in sending", 0, 5);
}

}


class BTReader extends Thread
{

public DataInputStream dataInStr;

public void run() {

int command=0;      

dataInStr = DataExchange.conn.openDataInputStream();
      
      if (dataInStr == null) {
      LCD.clear();
      LCD.drawString("NoDataStream", 0, 0);
      }

while (true)
{

try {   
command = dataInStr.readInt();
LCD.drawInt(command, 0, 2);
} catch (Exception ioe)
{
      LCD.clear();
      LCD.drawString("CantReadFromStream", 0, 0);
}
PilotWrapper.commands.add(command);
}
}
}

sending commands to brick works, but i can't send things back, or probably i can't read them
because sender on brick doesn't return any exception so probably it's works
but listener on pc looks like it stuck and it not doing anything
can anyone tells me what i;m doing wrong?
regards
madtroll
New User
 
Posts: 4
Joined: Mon Jan 17, 2011 6:32 pm

Re: Problem with BT communication

Postby Thorsten » Mon Mar 07, 2011 2:12 pm

Hi,
have you tried to flush the output stream, like you do it when sending from the PC? At least this helped when I had a similar issue.
Regards,
Thorsten
Thorsten
New User
 
Posts: 4
Joined: Mon Mar 07, 2011 2:07 pm

Re: Problem with BT communication

Postby madtroll » Mon Mar 07, 2011 2:27 pm

Thorsten wrote:Hi,
have you tried to flush the output stream, like you do it when sending from the PC? At least this helped when I had a similar issue.
Regards,
Thorsten

thanks
worked, i forgot to flush stream
madtroll
New User
 
Posts: 4
Joined: Mon Jan 17, 2011 6:32 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff