Android Bluetooth Remote Control
Page 1 of 1

Author:  brianchen11 [ Thu Jul 12, 2012 2:50 am ]
Post subject:  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.

int readMessage(TMailboxIDs mailbox){

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

Author:  Spiked3 [ Thu Jul 12, 2012 5:27 am ]
Post subject:  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;

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;
ubyte slaveBuf[58];
task BlueToothSlave()
    while (true)
        while (bBTBusy) wait1Msec(10);
        int i = cCmdMessageGetSize(mailbox1);
        if (i > 0)
            cCmdMessageRead(slaveBuf, sizeof(slaveBuf), mailbox1);
            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
int readMessage(TMailboxIDs mailbox)
    ubyte data[58];
    int returnedNumber = -9999;
    int messageSize = cCmdMessageGetSize(mailbox);

        cCmdMessageRead(data, sizeof(data), mailbox);
        returnedNumber = atoi(data);
    return returnedNumber;

try that and let us know if it works.

Author:  brianchen11 [ Fri Jul 13, 2012 6:39 pm ]
Post subject:  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).

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group