View unanswered posts | View active topics It is currently Wed Jul 23, 2014 10:31 am






Reply to topic  [ 3 posts ] 
Android Bluetooth Remote Control 
Author Message
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Android Bluetooth Remote Control
Hi everyone,

I've created an application using App Inventor for Android and am using it to control a segway.

However, I'm stuck on a major issue, reading the bluetooth message from the android. The android successfully sends the gyroscopic measurements but I have little idea of how to process single 'ubytes' (pretty much the same as bytes in RobotC)
and converting them to an int or float.

Could anyone please help?
This is the current code for reading the message. It converts the bytes into a string and then uses the atoi function to convert it into an int. However, this has some issues.

To make it simpler, I changed the app to send a message of 11 to both mailbox 2 and 3 and the robotc code writes all the read information into a text file (after using the function below). The result is either the numbers 0, -11100, or -1100. I have no idea of what is going on.

Code:
int readMessage(TMailboxIDs mailbox){

  float returnedNumber = 0;
  string strInfo = "";
  int i = 0;
  int messageSize = cCmdMessageGetSize(mailbox);
  ubyte data[4];
  if(messageSize>4){
    messageSize=4;
  }
  if(messageSize > 0){
   
    for(i=0; i < messageSize; i++){
      cCmdMessageRead(data[i], messageSize, mailbox);
      strInfo+=(char)data[i];     
    }
    wait1Msec(10);   
    returnedNumber = atoi(strInfo);
  }
  return returnedNumber;
}


Thu Jul 12, 2012 2:50 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Android Bluetooth Remote Control
I've been deep into this kind of thing lately and I can probably help you out. One thing I finally realized, that with some effort, I did not need to go the string to number conversion route, however it can and should work.

Can you post the code snippet of how the data is transmitted? I will have to look at app inventor a bit to see what its doing, but I believe I can find that myself. Are you using any sort of pre-written library (for NXT or Bluetooth) on the android side of it?

I don't know if you saw it, but I recently also did an android to nxt project. it used LCP commands to transmit motor power based on the gyroscope. Have a look to see if it helps any; http://www.spiked3.com/?p=253

The portion that does the bluetooth was unmodified from a LEGO supplied application, and the source is included in the files.

update: OK - i looked at app inventor a little, that's kind of nice. Unfortunately for me, I just uninstalled java a little earlier today, so I can not really run it fully. it appears that it is using standard nxt messaging though.

In my code, I do the read a little differently;
Code:
ubyte slaveBuf[58];
task BlueToothSlave()
{
    while (true)
    {
        while (bBTBusy) wait1Msec(10);
        int i = cCmdMessageGetSize(mailbox1);
        if (i > 0)
        {
            cCmdMessageRead(slaveBuf, sizeof(slaveBuf), mailbox1);
            SlaveAction();
            while (nBluetoothCmdStatus==ioRsltCommPending) wait1Msec(2);
        }
        wait1Msec(250); // 4/second
    }
}


I'm wondering if in your code data[i], is causing a problem? in C with pointers, I might expect either to work, but in RobotC I've found to expect the unexpected. I'm pretty sure you also do not need to loop for the message size, one read and you should have it.
simply point atoi to the read message and it should be good.

so your code may end up looking like
Code:
int readMessage(TMailboxIDs mailbox)
{
    ubyte data[58];
    int returnedNumber = -9999;
    int messageSize = cCmdMessageGetSize(mailbox);

    if(messageSize>0)
    {
        cCmdMessageRead(data, sizeof(data), mailbox);
        returnedNumber = atoi(data);
    }
    return returnedNumber;
}


try that and let us know if it works.

_________________
Mike aka Spiked3
http://www.spiked3.com


Thu Jul 12, 2012 5:27 am
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: Android Bluetooth Remote Control
Works great! Thanks. I never realized the whole message could be read at once (now it seems stupid that I didn't try).


Fri Jul 13, 2012 6:39 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.